brew/Library/Homebrew/utils/inreplace.rb

28 lines
709 B
Ruby
Raw Normal View History

module Utils
module Inreplace
def inreplace paths, before=nil, after=nil
Array(paths).each do |path|
f = File.open(path, 'rb')
s = f.read
if before.nil? && after.nil?
s.extend(StringInreplaceExtension)
yield s
else
after = after.to_s if Symbol === after
2014-03-26 16:07:49 -05:00
unless s.gsub!(before, after)
message = <<-EOS.undent
inreplace in '#{path}' failed
Expected replacement of '#{before}' with '#{after}'
EOS
ARGV.homebrew_developer? ? odie(message) : opoo(message)
end
end
f.reopen(path, 'wb').write(s)
f.close
end
end
end
end