diff --git a/Library/Homebrew/extend/string.rb b/Library/Homebrew/extend/string.rb index 23fea8b2c9..57630a7010 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 520b41ad6b..a128b63c99 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 6dedf764b9..e478670fb4 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)