Module: Utils::Inreplace Private
- Defined in:
- utils/inreplace.rb
Overview
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Helper functions for replacing text in files in-place.
Defined Under Namespace
Classes: Error
Class Method Summary collapse
-
.inreplace(paths, before = nil, after = nil, audit_result: true, global: true, &block) ⇒ void
Sometimes we have to change a bit before we install.
- .inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) ⇒ String private
Class Method Details
.inreplace(paths, before = nil, after = nil, audit_result: true, global: true, &block) ⇒ void
This method returns an undefined value.
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 variables
defined by the formula, as only HOMEBREW_PREFIX
is available
in the embedded patch.
Examples
inreplace
supports regular expressions:
inreplace "somefile.cfg", /look[for]what?/, "replace by #{bin}/tool"
inreplace
supports blocks:
inreplace "Makefile" do |s|
s.gsub! "/usr/local", HOMEBREW_PREFIX.to_s
end
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'utils/inreplace.rb', line 55 def self.inreplace(paths, before = nil, after = nil, audit_result: true, global: true, &block) paths = Array(paths) after &&= after.to_s before = before.to_s if before.is_a?(Pathname) errors = {} errors["`paths` (first) parameter"] = ["`paths` was empty"] if paths.all?(&:blank?) paths.each do |path| str = File.binread(path) s = StringInreplaceExtension.new(str) if before.nil? && after.nil? raise ArgumentError, "Must supply a block or before/after params" unless block yield s elsif global s.gsub!(T.must(before), T.must(after), audit_result:) else s.sub!(T.must(before), T.must(after), audit_result:) end errors[path] = s.errors unless s.errors.empty? Pathname(path).atomic_write(s.inreplace_string) end raise Utils::Inreplace::Error, errors if errors.present? end |
.inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'utils/inreplace.rb', line 94 def self.inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) str = File.binread(path) contents = StringInreplaceExtension.new(str) replacement_pairs.each do |old, new| if old.blank? contents.errors << "No old value for new value #{new}! Did you pass the wrong arguments?" next end contents.gsub!(old, new) end raise Utils::Inreplace::Error, path => contents.errors if contents.errors.present? Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run contents.inreplace_string end |