aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils
diff options
context:
space:
mode:
Diffstat (limited to 'Library/Homebrew/utils')
-rw-r--r--Library/Homebrew/utils/inreplace.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb
new file mode 100644
index 000000000..4b3cf9587
--- /dev/null
+++ b/Library/Homebrew/utils/inreplace.rb
@@ -0,0 +1,24 @@
+module Utils
+ module Inreplace
+ def inreplace paths, before=nil, after=nil
+ Array(paths).each do |path|
+ f = File.open(path, 'r')
+ s = f.read
+
+ if before.nil? && after.nil?
+ s.extend(StringInreplaceExtension)
+ yield s
+ else
+ sub = s.gsub!(before, after)
+ if sub.nil?
+ opoo "inreplace in '#{path}' failed"
+ puts "Expected replacement of '#{before}' with '#{after}'"
+ end
+ end
+
+ f.reopen(path, 'w').write(s)
+ f.close
+ end
+ end
+ end
+end