From 84ec42f31ba5a11fab7c673cd6f867c787ee7c69 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Thu, 4 Aug 2022 22:34:16 +0900 Subject: [PATCH 01/10] check whether token is multiple tap's name --- Library/Homebrew/cask/cask_loader.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 8049077ee6..63d4f83367 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -236,7 +236,9 @@ module Cask when 2..Float::INFINITY loaders = possible_tap_casks.map(&FromTapPathLoader.method(:new)) - raise TapCaskAmbiguityError.new(ref, loaders) + raise TapCaskAmbiguityError.new(ref, loaders) if loaders.map(&:tap).map(&:name).size == 1 + + return FromTapPathLoader.new(possible_tap_casks.first) end possible_installed_cask = Cask.new(ref) From 71ab2f6e7afee7307471cdd61d828c48b68e4c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fn=20=E2=8C=83=20=E2=8C=A5?= <70830482+FnControlOption@users.noreply.github.com> Date: Wed, 20 Jul 2022 08:24:55 -0700 Subject: [PATCH 02/10] Run periodic cleanup after installing all packages --- Library/Homebrew/cleanup.rb | 35 ++++++++++++++++++------------- Library/Homebrew/cmd/install.rb | 2 ++ Library/Homebrew/cmd/reinstall.rb | 2 ++ Library/Homebrew/cmd/upgrade.rb | 2 ++ 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index e381088a1d..d4193d32b1 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -156,15 +156,19 @@ module Homebrew def self.install_formula_clean!(f, dry_run: false) return if Homebrew::EnvConfig.no_install_cleanup? + return unless f.latest_version_installed? + return if skip_clean_formula?(f) - cleanup = Cleanup.new(dry_run: dry_run) - if cleanup.periodic_clean_due? - cleanup.periodic_clean! - elsif f.latest_version_installed? && !Cleanup.skip_clean_formula?(f) + if dry_run + ohai "Would run `brew cleanup #{f}`" + else ohai "Running `brew cleanup #{f}`..." - puts_no_install_cleanup_disable_message_if_not_already! - cleanup.cleanup_formula(f) end + + puts_no_install_cleanup_disable_message_if_not_already! + return if dry_run + + Cleanup.new.cleanup_formula(f) end def self.puts_no_install_cleanup_disable_message_if_not_already! @@ -184,7 +188,7 @@ module Homebrew skip_clean_formulae.include?(f.name) || (skip_clean_formulae & f.aliases).present? end - def periodic_clean_due? + def self.periodic_clean_due? return false if Homebrew::EnvConfig.no_install_cleanup? unless PERIODIC_CLEAN_FILE.exist? @@ -196,19 +200,20 @@ module Homebrew PERIODIC_CLEAN_FILE.mtime < CLEANUP_DEFAULT_DAYS.days.ago end - def periodic_clean! - return false unless periodic_clean_due? + def self.periodic_clean!(dry_run: false) + return if Homebrew::EnvConfig.no_install_cleanup? + return unless periodic_clean_due? - if dry_run? - ohai "Would run `brew cleanup` which has not been run in the last #{CLEANUP_DEFAULT_DAYS} days" + if dry_run + oh1 "Would run `brew cleanup` which has not been run in the last #{CLEANUP_DEFAULT_DAYS} days" else - ohai "`brew cleanup` has not been run in the last #{CLEANUP_DEFAULT_DAYS} days, running now..." + oh1 "`brew cleanup` has not been run in the last #{CLEANUP_DEFAULT_DAYS} days, running now..." end - Cleanup.puts_no_install_cleanup_disable_message_if_not_already! - return if dry_run? + puts_no_install_cleanup_disable_message_if_not_already! + return if dry_run - clean!(quiet: true, periodic: true) + Cleanup.new.clean!(quiet: true, periodic: true) end def clean!(quiet: false, periodic: false) diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index cff794979e..07f061e544 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -253,6 +253,8 @@ module Homebrew verbose: args.verbose?, ) + Cleanup.periodic_clean! + Homebrew.messages.display_messages(display_times: args.display_times?) rescue FormulaUnreadableError, FormulaClassUnavailableError, TapFormulaUnreadableError, TapFormulaClassUnavailableError => e diff --git a/Library/Homebrew/cmd/reinstall.rb b/Library/Homebrew/cmd/reinstall.rb index bc6bca5053..e7bfad6b29 100644 --- a/Library/Homebrew/cmd/reinstall.rb +++ b/Library/Homebrew/cmd/reinstall.rb @@ -151,6 +151,8 @@ module Homebrew ) end + Cleanup.periodic_clean! + Homebrew.messages.display_messages(display_times: args.display_times?) end end diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index ec7520a7c3..3dd8afb805 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -110,6 +110,8 @@ module Homebrew upgrade_outdated_formulae(formulae, args: args) unless only_upgrade_casks upgrade_outdated_casks(casks, args: args) unless only_upgrade_formulae + Cleanup.periodic_clean!(dry_run: args.dry_run?) + Homebrew.messages.display_messages(display_times: args.display_times?) end From dce57eaad6b15366a195a7c553e54b35893a6107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fn=20=E2=8C=83=20=E2=8C=A5?= <70830482+FnControlOption@users.noreply.github.com> Date: Wed, 20 Jul 2022 08:52:37 -0700 Subject: [PATCH 03/10] Always print install cleanup disable message for periodic clean --- Library/Homebrew/cleanup.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index d4193d32b1..07e1438c84 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -171,13 +171,18 @@ module Homebrew Cleanup.new.cleanup_formula(f) end - def self.puts_no_install_cleanup_disable_message_if_not_already! + def self.puts_no_install_cleanup_disable_message return if Homebrew::EnvConfig.no_env_hints? return if Homebrew::EnvConfig.no_install_cleanup? - return if @puts_no_install_cleanup_disable_message_if_not_already puts "Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP." puts "Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`)." + end + + def self.puts_no_install_cleanup_disable_message_if_not_already! + return if @puts_no_install_cleanup_disable_message_if_not_already + + puts_no_install_cleanup_disable_message @puts_no_install_cleanup_disable_message_if_not_already = true end @@ -210,7 +215,7 @@ module Homebrew oh1 "`brew cleanup` has not been run in the last #{CLEANUP_DEFAULT_DAYS} days, running now..." end - puts_no_install_cleanup_disable_message_if_not_already! + puts_no_install_cleanup_disable_message return if dry_run Cleanup.new.clean!(quiet: true, periodic: true) From a43633e0948dbbb1fceeebcad965c7ccbdc498cb Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Fri, 5 Aug 2022 20:59:19 +0900 Subject: [PATCH 04/10] add uniq to check multiple tap --- Library/Homebrew/cask/cask_loader.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 63d4f83367..cb6aefb10d 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -236,7 +236,7 @@ module Cask when 2..Float::INFINITY loaders = possible_tap_casks.map(&FromTapPathLoader.method(:new)) - raise TapCaskAmbiguityError.new(ref, loaders) if loaders.map(&:tap).map(&:name).size == 1 + raise TapCaskAmbiguityError.new(ref, loaders) if loaders.map(&:tap).map(&:name).uniq.size == 1 return FromTapPathLoader.new(possible_tap_casks.first) end From 0acadc857c7c5999ee89e9b18f5d6cace34a60e4 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Sun, 7 Aug 2022 15:43:51 +0900 Subject: [PATCH 05/10] repair CaskLoader.load args --- Library/Homebrew/cask/cask.rb | 2 +- Library/Homebrew/cask/cask_loader.rb | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 955cad92df..6bfc198f74 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -96,7 +96,7 @@ module Cask MacOSVersions::SYMBOLS.each do |os_name, os_version| MacOS.full_version = os_version - cask = CaskLoader.load(token) + cask = CaskLoader.load(full_name) version_os_hash[os_name] = cask.version if cask.version != version end diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index cb6aefb10d..8049077ee6 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -236,9 +236,7 @@ module Cask when 2..Float::INFINITY loaders = possible_tap_casks.map(&FromTapPathLoader.method(:new)) - raise TapCaskAmbiguityError.new(ref, loaders) if loaders.map(&:tap).map(&:name).uniq.size == 1 - - return FromTapPathLoader.new(possible_tap_casks.first) + raise TapCaskAmbiguityError.new(ref, loaders) end possible_installed_cask = Cask.new(ref) From 03a489bf78709f9361109d65817ae8821eeef864 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sat, 30 Jul 2022 19:10:40 +0200 Subject: [PATCH 06/10] brew.rb: tell users to fix head issues with inreplace --- Library/Homebrew/formula.rb | 9 ++++++--- Library/Homebrew/test/formula_spec.rb | 10 ++++++++++ Library/Homebrew/utils/inreplace.rb | 6 +++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index d2f9ea56f2..cd67b45278 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -69,9 +69,6 @@ class Formula extend Cachable extend Predicable - # @!method inreplace(paths, before = nil, after = nil) - # @see Utils::Inreplace.inreplace - # The name of this {Formula}. # e.g. `this-formula` attr_reader :name @@ -2169,6 +2166,12 @@ class Formula # end def install; end + def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter + super(paths, before, after, audit_result) + rescue Utils::Inreplace::Error + raise BuildError.new(self, "inreplace", paths, nil) + end + protected def setup_home(home) diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 0883985623..582596d29a 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -483,6 +483,16 @@ describe Formula do end end + describe "::inreplace" do + specify "raises build error on failure" do + f = formula do + url "https://brew.sh/test-1.0.tbz" + end + + expect { f.inreplace([]) }.to raise_error(BuildError) + end + end + describe "::installed_with_alias_path" do specify "with alias path with nil" do expect(described_class.installed_with_alias_path(nil)).to be_empty diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index 35f7b19ae9..fc54572fce 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -10,7 +10,7 @@ module Utils module Inreplace extend T::Sig - # Error during replacement. + # Error during text replacement. class Error < RuntimeError def initialize(errors) formatted_errors = errors.reduce(+"inreplace failed\n") do |s, (path, errs)| @@ -70,7 +70,7 @@ module Utils Pathname(path).atomic_write(s.inreplace_string) end - raise Error, errors unless errors.empty? + raise Utils::Inreplace::Error, errors unless errors.empty? end # @api private @@ -86,7 +86,7 @@ module Utils contents.gsub!(old, new) end - raise Error, path => contents.errors unless contents.errors.empty? + raise Utils::Inreplace::Error, [path => contents.errors] unless contents.errors.empty? Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run contents.inreplace_string From 040d93a0060d277c684470165ab170df64f2d1a8 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 7 Aug 2022 15:55:37 +0200 Subject: [PATCH 07/10] formula: add back docstring --- Library/Homebrew/formula.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index cd67b45278..c4ae6330a4 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -2166,6 +2166,24 @@ class Formula # end def install; 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}. + # + # `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
+  # 
+ # + # @see Utils::Inreplace.inreplace + # @api public def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter super(paths, before, after, audit_result) rescue Utils::Inreplace::Error From acf39f8777a825d48b4ca46249c75be13614e2d1 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Mon, 8 Aug 2022 19:58:50 +0900 Subject: [PATCH 08/10] add TODO comment --- Library/Homebrew/cask/cask.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 6bfc198f74..d347e27ac3 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -90,6 +90,7 @@ module Cask end def os_versions + # TODO: use #to_hash_with_variations instead once all casks use on_system blocks @os_versions ||= begin version_os_hash = {} actual_version = MacOS.full_version.to_s From 12d31853abe33915c9334c80346a80f5e3cd1366 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 8 Aug 2022 14:01:44 +0200 Subject: [PATCH 09/10] inreplace: fix map --- Library/Homebrew/utils/inreplace.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index fc54572fce..39543d3020 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -86,7 +86,7 @@ module Utils contents.gsub!(old, new) end - raise Utils::Inreplace::Error, [path => contents.errors] unless contents.errors.empty? + raise Utils::Inreplace::Error, path => contents.errors unless contents.errors.empty? Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run contents.inreplace_string From 95080ebbb7f8293741172dd655bac3d37d6a389b Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 8 Aug 2022 16:18:06 +0200 Subject: [PATCH 10/10] inreplace: style fixes Co-authored-by: Mike McQuaid --- Library/Homebrew/utils/inreplace.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index 39543d3020..9763760258 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -70,7 +70,7 @@ module Utils Pathname(path).atomic_write(s.inreplace_string) end - raise Utils::Inreplace::Error, errors unless errors.empty? + raise Utils::Inreplace::Error, errors if errors.present? end # @api private @@ -86,7 +86,7 @@ module Utils contents.gsub!(old, new) end - raise Utils::Inreplace::Error, path => contents.errors unless contents.errors.empty? + 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