diff options
| author | Adam Vandenberg | 2009-12-17 12:41:54 -0800 |
|---|---|---|
| committer | Max Howell | 2010-01-13 11:23:07 +0000 |
| commit | 3422582da6aefaf8d437eba5f9a58eadef415915 (patch) | |
| tree | 96bcb11a23aa7aa6df5e8f3bc0d7de4af09eada3 | |
| parent | 672b675c6606edf4eddb69e48f4489f63b511156 (diff) | |
| download | homebrew-3422582da6aefaf8d437eba5f9a58eadef415915.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 |
