brew/Library/Homebrew/utils/inreplace.rb

34 lines
792 B
Ruby
Raw Normal View History

module Utils
class InreplaceError < RuntimeError
def initialize(errors)
super errors.inject("inreplace failed\n") { |s, (path, errs)|
s << "#{path}:\n" << errs.map { |e| " #{e}\n" }.join
}
end
end
module Inreplace
def inreplace paths, before=nil, after=nil
errors = {}
Array(paths).each do |path|
s = File.open(path, "rb", &:read).extend(StringInreplaceExtension)
if before.nil? && after.nil?
yield s
else
after = after.to_s if Symbol === after
s.gsub!(before, after)
end
errors[path] = s.errors if s.errors.any?
2014-03-26 16:07:49 -05:00
Pathname(path).atomic_write(s)
end
raise InreplaceError.new(errors) if errors.any?
end
2014-06-18 19:23:42 -05:00
module_function :inreplace
end
end