aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils/inreplace.rb
blob: 52a43ece94c9d9b10b0a94887f4e44b76b809684 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module Utils
  module Inreplace
    def inreplace paths, before=nil, after=nil
      Array(paths).each do |path|
        s = File.open(path, "rb", &:read)

        if before.nil? && after.nil?
          yield s.extend(StringInreplaceExtension)
        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
        end

        Pathname(path).atomic_write(s)
      end
    end
    module_function :inreplace
  end
end