Module: Utils::Inreplace

Included in:
Formula
Defined in:
Library/Homebrew/utils/inreplace.rb

Class Method Summary collapse

Class Method Details

.inreplace(paths, before = nil, after = nil, audit_result = true) ⇒ Object

Sometimes we have to change a bit before we install. Mostly we prefer a patch but if you need the prefix of this formula in the patch you have to resort to inreplace, because in the patch you don't have access to any var defined by the formula. Only HOMEBREW_PREFIX is available in the embedded patch. inreplace supports regular expressions.

inreplace "somefile.cfg", /look[for]what?/, "replace by #{bin}/tool"

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'Library/Homebrew/utils/inreplace.rb', line 19

def inreplace(paths, before = nil, after = nil, audit_result = true)
  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 after.is_a? Symbol
      s.gsub!(before, after, audit_result)
    end

    errors[path] = s.errors unless s.errors.empty?

    Pathname(path).atomic_write(s)
  end

  raise InreplaceError, errors unless errors.empty?
end