Module: Utils::Inreplace Private
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
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) ⇒ Object
private
rubocop:disable Style/OptionalBooleanParameter.
- .inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) ⇒ Object private
Class Method Details
.inreplace(paths, before = nil, after = nil, audit_result = true) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
rubocop:disable Style/OptionalBooleanParameter
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'Library/Homebrew/utils/inreplace.rb', line 43 def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter after = after.to_s if after.is_a? Symbol errors = {} errors["`paths` (first) parameter"] = ["`paths` was empty"] if paths.blank? Array(paths).each do |path| str = File.open(path, "rb", &:read) || "" s = StringInreplaceExtension.new(str) if before.nil? && after.nil? yield s else s.gsub!(T.must(before), after, audit_result) end errors[path] = s.errors unless s.errors.empty? Pathname(path).atomic_write(s.inreplace_string) end raise Error, errors unless errors.empty? end |
.inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'Library/Homebrew/utils/inreplace.rb', line 68 def inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) str = File.open(path, "rb", &:read) || "" contents = StringInreplaceExtension.new(str) replacement_pairs.each do |old, new| ohai "replace #{old.inspect} with #{new.inspect}" unless silent unless old contents.errors << "No old value for new value #{new}! Did you pass the wrong arguments?" next end contents.gsub!(old, new) end raise Error, path => contents.errors unless contents.errors.empty? Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run contents.inreplace_string end |