diff options
| author | Adam Vandenberg | 2009-12-17 12:41:54 -0800 |
|---|---|---|
| committer | Max Howell | 2010-01-13 11:23:07 +0000 |
| commit | 446be8cdd7aeb26d40bf47fb77997472ccd79e86 (patch) | |
| tree | d0dd4c92c0a31bb49a38871eff861610d6896e5d | |
| parent | 5bcffbb5e37ae4624f24bfbae0d94cfc5a1f23f2 (diff) | |
| download | brew-446be8cdd7aeb26d40bf47fb77997472ccd79e86.tar.bz2 | |
Add Makefile var and multi-replace support inreplace.
| -rw-r--r-- | Library/Homebrew/utils.rb | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 24b1b9ea6..b0bdcf6e8 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -155,11 +155,31 @@ def archs_for_command cmd end end -# replaces before with after for the file path -def inreplace path, before, after +module HomebrewInreplaceExtension + # Looks for Makefile style variable defintions and replaces the + # value with "new_value", or removes the definition entirely. + # See inreplace in utils.rb + def change_make_var! flag, new_value + new_value = "#{flag} = #{new_value}" unless new_value.to_s.empty? + gsub! Regexp.new("^#{flag}\\s*=.*$"), new_value.to_s + end + def remove_make_var! flags + flags.each { |flag| change_make_var! flag, "" } + end +end + +def inreplace path, before=nil, after=nil f = File.open(path, 'r') - o = f.read.gsub(before, after) - f.reopen(path, 'w').write(o) + s = f.read + + if before == nil and after == nil + s.extend(HomebrewInreplaceExtension) + yield s + else + s.gsub!(before, after) + end + + f.reopen(path, 'w').write(s) f.close end |
