aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils/inreplace.rb
blob: 4b3cf9587041504a82150b2ec2f8c59bf1b9d2c0 (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
module Utils
  module Inreplace
    def inreplace paths, before=nil, after=nil
      Array(paths).each do |path|
        f = File.open(path, 'r')
        s = f.read

        if before.nil? && after.nil?
          s.extend(StringInreplaceExtension)
          yield s
        else
          sub = s.gsub!(before, after)
          if sub.nil?
            opoo "inreplace in '#{path}' failed"
            puts "Expected replacement of '#{before}' with '#{after}'"
          end
        end

        f.reopen(path, 'w').write(s)
        f.close
      end
    end
  end
end