aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Vandenberg2010-11-12 21:05:35 -0800
committerAdam Vandenberg2011-03-12 11:55:03 -0800
commit65f840fb0649dbdee9af01f7f2446af97924f80a (patch)
treeb0ff836c7d978f8cce5a53a98007a6c1bf87abbc
parentfaaa1ad4b1c4e069dd6e6809391b999b9bfef586 (diff)
downloadhomebrew-65f840fb0649dbdee9af01f7f2446af97924f80a.tar.bz2
Move string extensions
-rw-r--r--Library/Homebrew/extend/string.rb25
-rw-r--r--Library/Homebrew/test/test_inreplace.rb15
-rw-r--r--Library/Homebrew/utils.rb25
3 files changed, 34 insertions, 31 deletions
diff --git a/Library/Homebrew/extend/string.rb b/Library/Homebrew/extend/string.rb
index 23fea8b2c..57630a701 100644
--- a/Library/Homebrew/extend/string.rb
+++ b/Library/Homebrew/extend/string.rb
@@ -3,3 +3,28 @@ class String
gsub(/^.{#{slice(/^ +/).length}}/, '')
end
end
+
+# used by the inreplace function (in utils.rb)
+module StringInreplaceExtension
+ # Looks for Makefile style variable defintions and replaces the
+ # value with "new_value", or removes the definition entirely.
+ def change_make_var! flag, new_value
+ new_value = "#{flag}=#{new_value}"
+ gsub! Regexp.new("^#{flag}[ \\t]*=[ \\t]*(.*)$"), new_value
+ end
+
+ # Removes variable assignments completely.
+ def remove_make_var! flags
+ flags.each do |flag|
+ # Also remove trailing \n, if present.
+ gsub! Regexp.new("^#{flag}[ \\t]*=(.*)$\n?"), ""
+ end
+ end
+
+ # Finds the specified variable
+ def get_make_var flag
+ m = match Regexp.new("^#{flag}[ \\t]*=[ \\t]*(.*)$")
+ return m[1] if m
+ return nil
+ end
+end
diff --git a/Library/Homebrew/test/test_inreplace.rb b/Library/Homebrew/test/test_inreplace.rb
index 520b41ad6..a128b63c9 100644
--- a/Library/Homebrew/test/test_inreplace.rb
+++ b/Library/Homebrew/test/test_inreplace.rb
@@ -1,11 +1,12 @@
require 'testing_env'
require 'utils'
+require 'extend/string'
class InreplaceTest < Test::Unit::TestCase
def test_change_make_var
# Replace flag
s1="OTHER=def\nFLAG = abc\nFLAG2=abc"
- s1.extend(HomebrewInreplaceExtension)
+ s1.extend(StringInreplaceExtension)
s1.change_make_var! "FLAG", "def"
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
end
@@ -13,7 +14,7 @@ class InreplaceTest < Test::Unit::TestCase
def test_change_make_var_empty
# Replace empty flag
s1="OTHER=def\nFLAG = \nFLAG2=abc"
- s1.extend(HomebrewInreplaceExtension)
+ s1.extend(StringInreplaceExtension)
s1.change_make_var! "FLAG", "def"
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
end
@@ -21,7 +22,7 @@ class InreplaceTest < Test::Unit::TestCase
def test_change_make_var_empty_2
# Replace empty flag
s1="FLAG = \nmv file_a file_b"
- s1.extend(HomebrewInreplaceExtension)
+ s1.extend(StringInreplaceExtension)
s1.change_make_var! "FLAG", "def"
assert_equal "FLAG=def\nmv file_a file_b", s1
end
@@ -29,7 +30,7 @@ class InreplaceTest < Test::Unit::TestCase
def test_change_make_var_append
# Append to flag
s1="OTHER=def\nFLAG = abc\nFLAG2=abc"
- s1.extend(HomebrewInreplaceExtension)
+ s1.extend(StringInreplaceExtension)
s1.change_make_var! "FLAG", "\\1 def"
assert_equal "OTHER=def\nFLAG=abc def\nFLAG2=abc", s1
end
@@ -37,7 +38,7 @@ class InreplaceTest < Test::Unit::TestCase
def test_change_make_var_shell_style
# Shell variables have no spaces around =
s1="OTHER=def\nFLAG=abc\nFLAG2=abc"
- s1.extend(HomebrewInreplaceExtension)
+ s1.extend(StringInreplaceExtension)
s1.change_make_var! "FLAG", "def"
assert_equal "OTHER=def\nFLAG=def\nFLAG2=abc", s1
end
@@ -45,7 +46,7 @@ class InreplaceTest < Test::Unit::TestCase
def test_remove_make_var
# Replace flag
s1="OTHER=def\nFLAG = abc\nFLAG2 = def"
- s1.extend(HomebrewInreplaceExtension)
+ s1.extend(StringInreplaceExtension)
s1.remove_make_var! "FLAG"
assert_equal "OTHER=def\nFLAG2 = def", s1
end
@@ -53,7 +54,7 @@ class InreplaceTest < Test::Unit::TestCase
def test_remove_make_vars
# Replace flag
s1="OTHER=def\nFLAG = abc\nFLAG2 = def\nOTHER2=def"
- s1.extend(HomebrewInreplaceExtension)
+ s1.extend(StringInreplaceExtension)
s1.remove_make_var! ["FLAG", "FLAG2"]
assert_equal "OTHER=def\nOTHER2=def", s1
end
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb
index 6dedf764b..e478670fb 100644
--- a/Library/Homebrew/utils.rb
+++ b/Library/Homebrew/utils.rb
@@ -169,36 +169,13 @@ def archs_for_command cmd
archs.extend(ArchitectureListExtension)
end
-# String extensions added by inreplace below.
-module HomebrewInreplaceExtension
- # Looks for Makefile style variable defintions and replaces the
- # value with "new_value", or removes the definition entirely.
- def change_make_var! flag, new_value
- new_value = "#{flag}=#{new_value}"
- gsub! Regexp.new("^#{flag}[ \\t]*=[ \\t]*(.*)$"), new_value
- end
- # Removes variable assignments completely.
- def remove_make_var! flags
- flags.each do |flag|
- # Also remove trailing \n, if present.
- gsub! Regexp.new("^#{flag}[ \\t]*=(.*)$\n?"), ""
- end
- end
- # Finds the specified variable
- def get_make_var flag
- m = match Regexp.new("^#{flag}[ \\t]*=[ \\t]*(.*)$")
- return m[1] if m
- return nil
- end
-end
-
def inreplace path, before=nil, after=nil
[*path].each do |path|
f = File.open(path, 'r')
s = f.read
if before == nil and after == nil
- s.extend(HomebrewInreplaceExtension)
+ s.extend(StringInreplaceExtension)
yield s
else
s.gsub!(before, after)