brew/Library/Homebrew/utils/inreplace.rb

25 lines
671 B
Ruby
Raw Normal View History

module Utils
module Inreplace
def inreplace paths, before=nil, after=nil
Array(paths).each do |path|
2014-03-26 16:07:49 -05:00
s = File.open(path, "rb", &:read)
if before.nil? && after.nil?
2014-03-26 16:07:49 -05:00
yield s.extend(StringInreplaceExtension)
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
2014-03-26 16:07:49 -05:00
Pathname(path).atomic_write(s)
end
end
end
end