diff options
Diffstat (limited to 'Library/Homebrew/utils/inreplace.rb')
| -rw-r--r-- | Library/Homebrew/utils/inreplace.rb | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index 52a43ece9..02372d214 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -1,24 +1,32 @@ 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) + s = File.open(path, "rb", &:read).extend(StringInreplaceExtension) if before.nil? && after.nil? - yield s.extend(StringInreplaceExtension) + yield s else after = after.to_s if Symbol === after - 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 + s.gsub!(before, after) end + errors[path] = s.errors if s.errors.any? + Pathname(path).atomic_write(s) end + + raise InreplaceError.new(errors) if errors.any? end module_function :inreplace end |
