brew/Library/Homebrew/utils/inreplace.rb

104 lines
3.3 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "utils/string_inreplace_extension"
module Utils
2020-08-19 08:18:14 +02:00
# Helper functions for replacing text in files in-place.
module Inreplace
# Error during text replacement.
2020-08-19 08:18:14 +02:00
class Error < RuntimeError
def initialize(errors)
formatted_errors = errors.reduce(+"inreplace failed\n") do |s, (path, errs)|
s << "#{path}:\n" << errs.map { |e| " #{e}\n" }.join
end
super formatted_errors.freeze
2016-09-11 17:47:04 +01:00
end
end
# Sometimes we have to change a bit before we install. Mostly we
# prefer a patch, but if you need the {Formula#prefix 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 {DATAPatch embedded patch}.
#
# ### Examples
2020-08-19 08:18:14 +02:00
#
# `inreplace` supports regular expressions:
#
# ```ruby
# inreplace "somefile.cfg", /look[for]what?/, "replace by #{bin}/tool"
# ```
#
# `inreplace` supports blocks:
#
# ```ruby
# inreplace "Makefile" do |s|
# s.gsub! "/usr/local", HOMEBREW_PREFIX.to_s
# end
# ```
#
# @see StringInreplaceExtension
2020-08-19 08:18:14 +02:00
# @api public
sig {
params(
2023-08-01 09:32:42 -07:00
paths: T.any(T::Enumerable[T.any(String, Pathname)], String, Pathname),
before: T.nilable(T.any(Pathname, Regexp, String)),
after: T.nilable(T.any(Pathname, String, Symbol)),
audit_result: T::Boolean,
global: T::Boolean,
block: T.nilable(T.proc.params(s: StringInreplaceExtension).void),
).void
}
def self.inreplace(paths, before = nil, after = nil, audit_result: true, global: true, &block)
2023-08-01 09:32:42 -07:00
paths = Array(paths)
after &&= after.to_s
2023-07-23 15:56:26 -07:00
before = before.to_s if before.is_a?(Pathname)
2020-09-19 01:53:59 +02:00
errors = {}
2023-08-01 09:32:42 -07:00
errors["`paths` (first) parameter"] = ["`paths` was empty"] if paths.all?(&:blank?)
2023-08-01 09:32:42 -07:00
paths.each do |path|
2021-12-23 14:58:51 -05:00
str = File.binread(path)
s = StringInreplaceExtension.new(str)
if before.nil? && after.nil?
2023-08-07 17:26:46 -07:00
raise ArgumentError, "Must supply a block or before/after params" unless block
2023-08-04 16:18:54 -07:00
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
2020-08-16 10:28:26 -07:00
def self.inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false)
2021-12-23 14:58:51 -05:00
str = File.binread(path)
2020-08-16 10:28:26 -07:00
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 Utils::Inreplace::Error, path => contents.errors if contents.errors.present?
2020-08-16 10:28:26 -07:00
Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run
contents.inreplace_string
end
end
end