From 490e503b1b6977cfd9d7580058e12a2d4d3c964a Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 2 Aug 2020 14:32:31 +0200 Subject: [PATCH] Replace `Homebrew.args` with `Context`. --- Library/Homebrew/bintray.rb | 9 +-- Library/Homebrew/brew.rb | 1 + Library/Homebrew/build.rb | 5 +- Library/Homebrew/cask/cmd.rb | 4 +- Library/Homebrew/cleaner.rb | 8 +-- Library/Homebrew/cli/args.rb | 4 ++ Library/Homebrew/cmd/fetch.rb | 4 +- Library/Homebrew/cmd/install.rb | 2 +- Library/Homebrew/cmd/migrate.rb | 2 +- Library/Homebrew/cmd/outdated.rb | 10 +-- Library/Homebrew/cmd/reinstall.rb | 2 +- Library/Homebrew/cmd/update-report.rb | 2 +- Library/Homebrew/context.rb | 71 +++++++++++++++++++ Library/Homebrew/dev-cmd/mirror.rb | 2 +- Library/Homebrew/dev-cmd/pr-pull.rb | 2 +- Library/Homebrew/dev-cmd/pr-upload.rb | 2 +- Library/Homebrew/dev-cmd/unpack.rb | 2 +- Library/Homebrew/download_strategy.rb | 29 ++++---- Library/Homebrew/extend/pathname.rb | 6 +- Library/Homebrew/formula.rb | 42 +++++------ Library/Homebrew/formula_assertions.rb | 6 +- Library/Homebrew/formula_installer.rb | 8 +-- Library/Homebrew/formula_versions.rb | 4 +- Library/Homebrew/formulary.rb | 6 +- Library/Homebrew/global.rb | 3 +- Library/Homebrew/migrator.rb | 16 ++--- Library/Homebrew/os/mac/keg.rb | 4 +- Library/Homebrew/patch.rb | 4 +- Library/Homebrew/postinstall.rb | 2 +- Library/Homebrew/resource.rb | 22 +++--- Library/Homebrew/software_spec.rb | 2 +- Library/Homebrew/system_command.rb | 16 +++-- Library/Homebrew/test.rb | 8 +-- .../test/system_command_result_spec.rb | 4 +- Library/Homebrew/upgrade.rb | 2 +- Library/Homebrew/utils.rb | 16 +++-- Library/Homebrew/utils/analytics.rb | 4 +- Library/Homebrew/utils/curl.rb | 2 +- 38 files changed, 203 insertions(+), 135 deletions(-) create mode 100644 Library/Homebrew/context.rb diff --git a/Library/Homebrew/bintray.rb b/Library/Homebrew/bintray.rb index 455419795f..5d4b05f36a 100644 --- a/Library/Homebrew/bintray.rb +++ b/Library/Homebrew/bintray.rb @@ -4,6 +4,8 @@ require "utils/curl" require "json" class Bintray + include Context + API_URL = "https://api.bintray.com" class Error < RuntimeError @@ -13,9 +15,8 @@ class Bintray "#" end - def initialize(org: "homebrew", verbose: false) + def initialize(org: "homebrew") @bintray_org = org - @verbose = verbose raise UsageError, "Must set a Bintray organisation!" unless @bintray_org @@ -33,8 +34,8 @@ class Bintray end curl(*args, url, - show_output: @verbose, - secrets: @bintray_key) + show_output: verbose?, + secrets: key) end def upload(local_file, repo:, package:, version:, remote_file:, sha256: nil) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 22d959459a..719b39f18e 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -61,6 +61,7 @@ begin args = Homebrew::CLI::Parser.new.parse(ARGV.dup.freeze, ignore_invalid_options: true) Homebrew.args = args + Context.current = args.context path = PATH.new(ENV["PATH"]) homebrew_path = PATH.new(ENV["HOMEBREW_PATH"]) diff --git a/Library/Homebrew/build.rb b/Library/Homebrew/build.rb index 303fd943a3..51b20093da 100644 --- a/Library/Homebrew/build.rb +++ b/Library/Homebrew/build.rb @@ -143,8 +143,6 @@ class Build fetch: false, keep_tmp: args.keep_tmp?, interactive: args.interactive?, - debug: args.debug?, - verbose: args.verbose?, ) do # For head builds, HOMEBREW_FORMULA_PREFIX should include the commit, # which is not known until after the formula has been staged. @@ -214,9 +212,8 @@ class Build end begin - Homebrew.args = Homebrew::CLI::Parser.new.parse(ARGV.dup.freeze, ignore_invalid_options: true) - args = Homebrew.install_args.parse + Context.current = args.context error_pipe = UNIXSocket.open(ENV["HOMEBREW_ERROR_PIPE"], &:recv_io) error_pipe.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) diff --git a/Library/Homebrew/cask/cmd.rb b/Library/Homebrew/cask/cmd.rb index 63ae4c5fa1..b3d4b5e33a 100644 --- a/Library/Homebrew/cask/cmd.rb +++ b/Library/Homebrew/cask/cmd.rb @@ -35,6 +35,8 @@ require "cask/cmd/internal_stanza" module Cask class Cmd + include Context + ALIASES = { "ls" => "list", "homepage" => "home", @@ -154,7 +156,7 @@ module Cask end rescue CaskError, MethodDeprecatedError, ArgumentError, OptionParser::InvalidOption => e onoe e.message - $stderr.puts e.backtrace if Homebrew.args.debug? + $stderr.puts e.backtrace if debug? exit 1 rescue StandardError, ScriptError, NoMemoryError => e onoe e.message diff --git a/Library/Homebrew/cleaner.rb b/Library/Homebrew/cleaner.rb index e011af9094..651662a7c9 100644 --- a/Library/Homebrew/cleaner.rb +++ b/Library/Homebrew/cleaner.rb @@ -10,15 +10,11 @@ # * sets permissions on executables # * removes unresolved symlinks class Cleaner - extend Predicable - - attr_predicate :verbose?, :debug? + include Context # Create a cleaner for the given formula - def initialize(f, verbose: false, debug: false) + def initialize(f) @f = f - @verbose = verbose - @debug = debug end # Clean the keg of formula @f diff --git a/Library/Homebrew/cli/args.rb b/Library/Homebrew/cli/args.rb index f9d7adef1f..a67d0e7d10 100644 --- a/Library/Homebrew/cli/args.rb +++ b/Library/Homebrew/cli/args.rb @@ -188,6 +188,10 @@ module Homebrew flag_with_value.delete_prefix(arg_prefix) end + def context + Context::ContextStruct.new(debug: debug?, quiet: quiet?, verbose: verbose?) + end + private def option_to_name(option) diff --git a/Library/Homebrew/cmd/fetch.rb b/Library/Homebrew/cmd/fetch.rb index 2b14a2a0f7..93fb6ff6b9 100644 --- a/Library/Homebrew/cmd/fetch.rb +++ b/Library/Homebrew/cmd/fetch.rb @@ -133,7 +133,7 @@ module Homebrew already_fetched = f.cached_download.exist? begin - download = f.fetch(verify_download_integrity: false, verbose: args.verbose?) + download = f.fetch(verify_download_integrity: false) rescue DownloadError retry if retry_fetch?(f, args: args) raise @@ -144,6 +144,6 @@ module Homebrew puts "Downloaded to: #{download}" unless already_fetched puts Checksum::TYPES.map { |t| "#{t.to_s.upcase}: #{download.send(t)}" } - f.verify_download_integrity(download, verbose: args.verbose?) + f.verify_download_integrity(download) end end diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index 8541406e35..efb73358d6 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -258,7 +258,7 @@ module Homebrew Install.perform_preinstall_checks(cc: args.cc) formulae.each do |f| - Migrator.migrate_if_needed(f, force: args.force?, verbose: args.verbose?) + Migrator.migrate_if_needed(f, force: args.force?) install_formula(f, args: args) Cleanup.install_formula_clean!(f) end diff --git a/Library/Homebrew/cmd/migrate.rb b/Library/Homebrew/cmd/migrate.rb index 158404a48c..c6f12d7831 100644 --- a/Library/Homebrew/cmd/migrate.rb +++ b/Library/Homebrew/cmd/migrate.rb @@ -33,7 +33,7 @@ module Homebrew raise "#{rack} is a symlink" if rack.symlink? end - migrator = Migrator.new(f, force: args.force?, verbose: args.verbose?) + migrator = Migrator.new(f, force: args.force?) migrator.migrate end end diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index a3366b7564..c6fe50c200 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -94,7 +94,7 @@ module Homebrew if formula_or_cask.is_a?(Formula) f = formula_or_cask - if verbose? args: args + if verbose? outdated_kegs = f.outdated_kegs(fetch_head: args.fetch_HEAD?) current_version = if f.alias_changed? @@ -122,7 +122,7 @@ module Homebrew else c = formula_or_cask - puts c.outdated_info(args.greedy?, verbose?(args: args), false) + puts c.outdated_info(args.greedy?, verbose?, false) end end end @@ -147,13 +147,13 @@ module Homebrew else c = formula_or_cask - c.outdated_info(args.greedy?, verbose?(args: args), true) + c.outdated_info(args.greedy?, verbose?, true) end end end - def verbose?(args:) - ($stdout.tty? || args.verbose?) && !args.quiet? + def verbose? + ($stdout.tty? || super) && !quiet? end def json_version(version) diff --git a/Library/Homebrew/cmd/reinstall.rb b/Library/Homebrew/cmd/reinstall.rb index 2de30916ce..cd57578a13 100644 --- a/Library/Homebrew/cmd/reinstall.rb +++ b/Library/Homebrew/cmd/reinstall.rb @@ -67,7 +67,7 @@ module Homebrew onoe "#{f.full_name} is pinned. You must unpin it to reinstall." next end - Migrator.migrate_if_needed(f, force: args.force?, verbose: args.verbose?) + Migrator.migrate_if_needed(f, force: args.force?) reinstall_formula(f, args: args) Cleanup.install_formula_clean!(f) end diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index 94d6b6cb89..6760897cc1 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -395,7 +395,7 @@ class Reporter next end - Migrator.migrate_if_needed(f, force: force, verbose: verbose) + Migrator.migrate_if_needed(f, force: force) end end diff --git a/Library/Homebrew/context.rb b/Library/Homebrew/context.rb new file mode 100644 index 0000000000..c8c70ac7b8 --- /dev/null +++ b/Library/Homebrew/context.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require "monitor" + +module Context + extend MonitorMixin + + def self.current=(context) + synchronize do + @current = context + end + end + + def self.current + if current_context = Thread.current[:context] + return current_context + end + + synchronize do + @current ||= ContextStruct.new + end + end + + class ContextStruct + def initialize(debug: nil, quiet: nil, verbose: nil) + @debug = debug + @quiet = quiet + @verbose = verbose + end + + def debug? + @debug + end + + def quiet? + @quiet + end + + def verbose? + @verbose + end + end + + def debug? + Context.current.debug? + end + + def quiet? + Context.current.quiet? + end + + def verbose? + Context.current.verbose? + end + + def with_context(**options) + old_context = Thread.current[:context] + + new_context = ContextStruct.new( + debug: options.fetch(:debug, old_context&.debug?), + quiet: options.fetch(:quiet, old_context&.quiet?), + verbose: options.fetch(:verbose, old_context&.verbose?), + ) + + Thread.current[:context] = new_context + + yield + ensure + Thread.current[:context] = old_context + end +end diff --git a/Library/Homebrew/dev-cmd/mirror.rb b/Library/Homebrew/dev-cmd/mirror.rb index 3de9a3c1a2..832707227a 100644 --- a/Library/Homebrew/dev-cmd/mirror.rb +++ b/Library/Homebrew/dev-cmd/mirror.rb @@ -31,7 +31,7 @@ module Homebrew bintray_org = args.bintray_org || "homebrew" bintray_repo = args.bintray_repo || "mirror" - bintray = Bintray.new(org: bintray_org, verbose: args.verbose?) + bintray = Bintray.new(org: bintray_org) args.formulae.each do |formula| mirror_url = bintray.mirror_formula(formula, repo: bintray_repo, publish_package: !args.no_publish?) diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index 28ae197fac..052e5dbdbc 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -116,7 +116,7 @@ module Homebrew # git cherry-pick unfortunately has no quiet option ohai "Cherry-picking #{commit_count} commit#{"s" unless commit_count == 1} from ##{pr}" cherry_pick_args = "git", "-C", path, "cherry-pick", "--ff", "--allow-empty", "#{merge_base}..FETCH_HEAD" - result = Homebrew.args.verbose? ? system(*cherry_pick_args) : quiet_system(*cherry_pick_args) + result = args.verbose? ? system(*cherry_pick_args) : quiet_system(*cherry_pick_args) unless result if args.resolve? diff --git a/Library/Homebrew/dev-cmd/pr-upload.rb b/Library/Homebrew/dev-cmd/pr-upload.rb index 40e0b9ff96..aa83627f63 100644 --- a/Library/Homebrew/dev-cmd/pr-upload.rb +++ b/Library/Homebrew/dev-cmd/pr-upload.rb @@ -49,7 +49,7 @@ module Homebrew args = pr_upload_args.parse bintray_org = args.bintray_org || "homebrew" - bintray = Bintray.new(org: bintray_org, verbose: args.verbose?) + bintray = Bintray.new(org: bintray_org) json_files = Dir["*.json"] odie "No JSON files found in the current working directory" if json_files.empty? diff --git a/Library/Homebrew/dev-cmd/unpack.rb b/Library/Homebrew/dev-cmd/unpack.rb index b72aa312bb..fbdd7c459e 100644 --- a/Library/Homebrew/dev-cmd/unpack.rb +++ b/Library/Homebrew/dev-cmd/unpack.rb @@ -57,7 +57,7 @@ module Homebrew # show messages about tar with_env VERBOSE: "1" do - f.brew(debug: args.debug?) do + f.brew do f.patch if args.patch? cp_r getwd, stage_dir, preserve: true end diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index ce99d2845c..7afadb9c85 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -13,6 +13,7 @@ require "mechanize/http/content_disposition_parser" class AbstractDownloadStrategy extend Forwardable include FileUtils + include Context module Pourable def stage @@ -21,9 +22,9 @@ class AbstractDownloadStrategy end end - attr_reader :cache, :cached_location, :url, :meta, :name, :version, :shutup + attr_reader :cache, :cached_location, :url, :meta, :name, :version - private :meta, :name, :version, :shutup + private :meta, :name, :version def initialize(url, name, version, **meta) @url = url @@ -31,24 +32,18 @@ class AbstractDownloadStrategy @version = version @cache = meta.fetch(:cache, HOMEBREW_CACHE) @meta = meta - @shutup = false extend Pourable if meta[:bottle] end # Download and cache the resource as {#cached_location}. def fetch; end - # Suppress output - def shutup! - @shutup = true - end - def puts(*args) - super(*args) unless shutup + super(*args) unless quiet? end def ohai(*args) - super(*args) unless shutup + super(*args) unless quiet? end # Unpack {#cached_location} into the current working directory, and possibly @@ -60,7 +55,7 @@ class AbstractDownloadStrategy ref_type: @ref_type, ref: @ref) .extract_nestedly(basename: basename, prioritise_extension: true, - verbose: Homebrew.args.verbose? && !shutup) + verbose: verbose? && !quiet?) chdir end @@ -102,9 +97,9 @@ class AbstractDownloadStrategy def system_command!(*args, **options) super( *args, - print_stdout: !shutup, - print_stderr: !shutup, - verbose: Homebrew.args.verbose? && !shutup, + print_stdout: !quiet?, + print_stderr: !quiet?, + verbose: verbose? && !quiet?, env: env, **options, ) @@ -498,7 +493,7 @@ class NoUnzipCurlDownloadStrategy < CurlDownloadStrategy def stage UnpackStrategy::Uncompressed.new(cached_location) .extract(basename: basename, - verbose: Homebrew.args.verbose? && !shutup) + verbose: verbose? && !quiet?) end end @@ -553,7 +548,7 @@ class SubversionDownloadStrategy < VCSDownloadStrategy # This saves on bandwidth and will have a similar effect to verifying the # cache as it will make any changes to get the right revision. args = [] - args << "--quiet" unless Homebrew.args.verbose? + args << "--quiet" unless verbose? if revision ohai "Checking out #{@ref}" @@ -897,7 +892,7 @@ class CVSDownloadStrategy < VCSDownloadStrategy end def quiet_flag - "-Q" unless Homebrew.args.verbose? + "-Q" unless verbose? end def clone_repo diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 63023593ca..fb3ddd9659 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -419,6 +419,8 @@ require "extend/os/pathname" # @private module ObserverPathnameExtension class << self + include Context + attr_accessor :n, :d def reset_counts! @@ -437,8 +439,8 @@ module ObserverPathnameExtension MAXIMUM_VERBOSE_OUTPUT = 100 def verbose? - return Homebrew.args.verbose? unless ENV["CI"] - return false unless Homebrew.args.verbose? + return super unless ENV["CI"] + return false unless super if total < MAXIMUM_VERBOSE_OUTPUT true diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 03b7a5ec5c..5849baaa20 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -54,6 +54,7 @@ class Formula include Utils::Inreplace include Utils::Shebang include Utils::Shell + include Context extend Enumerable extend Forwardable extend Cachable @@ -537,8 +538,10 @@ class Formula return false unless head&.downloader.is_a?(VCSDownloadStrategy) downloader = head.downloader - downloader.shutup! unless Homebrew.args.verbose? - downloader.commit_outdated?(version.version.commit) + + with_context quiet: true do + downloader.commit_outdated?(version.version.commit) + end end # The latest prefix for this formula. Checks for {#head}, then {#devel} @@ -1168,35 +1171,28 @@ class Formula patchlist.each(&:apply) end - # @private - attr_predicate :debug?, :verbose? - # yields |self,staging| with current working directory set to the uncompressed tarball # where staging is a Mktemp staging context # @private - def brew(fetch: true, keep_tmp: false, interactive: false, debug: false, verbose: false) - @debug = debug - @verbose = verbose + def brew(fetch: true, keep_tmp: false, interactive: false) @prefix_returns_versioned_prefix = true active_spec.fetch if fetch stage(interactive: interactive) do |staging| staging.retain! if keep_tmp prepare_patches - fetch_patches(verbose: verbose) if fetch + fetch_patches if fetch begin yield self, staging rescue - staging.retain! if interactive || debug + staging.retain! if interactive || debug? raise ensure cp Dir["config.log", "CMakeCache.txt"], logs end end ensure - @debug = nil - @verbose = nil @prefix_returns_versioned_prefix = false end @@ -1801,19 +1797,17 @@ class Formula end # @private - def fetch(verify_download_integrity: true, verbose: false) - active_spec.fetch(verify_download_integrity: verify_download_integrity, verbose: verbose) + def fetch(verify_download_integrity: true) + active_spec.fetch(verify_download_integrity: verify_download_integrity) end # @private - def verify_download_integrity(fn, verbose: false) - active_spec.verify_download_integrity(fn, verbose: verbose) + def verify_download_integrity(fn) + active_spec.verify_download_integrity(fn) end # @private - def run_test(keep_tmp: false, debug: false, verbose: false) - @debug = debug - @verbose = verbose + def run_test(keep_tmp: false) @prefix_returns_versioned_prefix = true test_env = { @@ -1841,13 +1835,11 @@ class Formula end end rescue Exception # rubocop:disable Lint/RescueException - staging.retain! if debug + staging.retain! if debug? raise end end ensure - @debug = nil - @verbose = nil @prefix_returns_versioned_prefix = false @testpath = nil end @@ -2103,10 +2095,8 @@ class Formula ENV.update(removed) end - def fetch_patches(verbose: false) - patchlist.select(&:external?).each do |p| - p.fetch(verbose: verbose) - end + def fetch_patches + patchlist.select(&:external?).each(&:fetch) end private diff --git a/Library/Homebrew/formula_assertions.rb b/Library/Homebrew/formula_assertions.rb index acb428ae98..55592f2214 100644 --- a/Library/Homebrew/formula_assertions.rb +++ b/Library/Homebrew/formula_assertions.rb @@ -2,6 +2,8 @@ module Homebrew module Assertions + include Context + require "test/unit/assertions" include ::Test::Unit::Assertions @@ -12,7 +14,7 @@ module Homebrew assert_equal result, $CHILD_STATUS.exitstatus output rescue Test::Unit::AssertionFailedError - puts output if Homebrew.args.verbose? + puts output if verbose? raise end @@ -28,7 +30,7 @@ module Homebrew assert_equal result, $CHILD_STATUS.exitstatus unless result.nil? output rescue Test::Unit::AssertionFailedError - puts output if Homebrew.args.verbose? + puts output if verbose? raise end end diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index b0a2c61888..06d5db08b8 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -952,7 +952,7 @@ class FormulaInstaller def clean ohai "Cleaning" if verbose? - Cleaner.new(formula, verbose: verbose?, debug: debug?).clean + Cleaner.new(formula).clean rescue Exception => e # rubocop:disable Lint/RescueException opoo "The cleaning step did not complete successfully" puts "Still, the installation was successful, so we will link it into your prefix" @@ -1027,10 +1027,8 @@ class FormulaInstaller end return if pour_bottle? - formula.fetch_patches(verbose: verbose?) - formula.resources.each do |r| - r.fetch(verbose: verbose?) - end + formula.fetch_patches + formula.resources.each(&:fetch) downloader.fetch end diff --git a/Library/Homebrew/formula_versions.rb b/Library/Homebrew/formula_versions.rb index 5805213c8b..d6503bd22b 100644 --- a/Library/Homebrew/formula_versions.rb +++ b/Library/Homebrew/formula_versions.rb @@ -3,6 +3,8 @@ require "formula" class FormulaVersions + include Context + IGNORED_EXCEPTIONS = [ ArgumentError, NameError, SyntaxError, TypeError, FormulaSpecificationError, FormulaValidationError, @@ -44,7 +46,7 @@ class FormulaVersions rescue *IGNORED_EXCEPTIONS => e # We rescue these so that we can skip bad versions and # continue walking the history - odebug "#{e} in #{name} at revision #{rev}", e.backtrace if Homebrew.args.debug? + odebug "#{e} in #{name} at revision #{rev}", e.backtrace if debug? rescue FormulaUnavailableError nil ensure diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 80600d7e34..ce3d098cf9 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -109,6 +109,8 @@ module Formulary # A FormulaLoader returns instances of formulae. # Subclasses implement loaders for particular sources of formulae. class FormulaLoader + include Context + # The formula's name attr_reader :name # The formula's ruby file's path or filename @@ -138,7 +140,7 @@ module Formulary private def load_file(flags:) - $stderr.puts "#{$PROGRAM_NAME} (#{self.class.name}): loading #{path}" if Homebrew.args.debug? + $stderr.puts "#{$PROGRAM_NAME} (#{self.class.name}): loading #{path}" if debug? raise FormulaUnavailableError, name unless path.file? Formulary.load_formula_from_path(name, path, flags: flags) @@ -314,7 +316,7 @@ module Formulary end def klass(flags:) - $stderr.puts "#{$PROGRAM_NAME} (#{self.class.name}): loading #{path}" if Homebrew.args.debug? + $stderr.puts "#{$PROGRAM_NAME} (#{self.class.name}): loading #{path}" if debug? namespace = "FormulaNamespace#{Digest::MD5.hexdigest(contents.to_s)}" Formulary.load_formula(name, path, contents, namespace, flags: flags) end diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index b100822fc0..9276582221 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -37,7 +37,6 @@ require "config" require "os" require "cli/args" require "messages" -require "system_command" HOMEBREW_PRODUCT = ENV["HOMEBREW_PRODUCT"] HOMEBREW_VERSION = ENV["HOMEBREW_VERSION"] @@ -116,6 +115,7 @@ end.compact.freeze require "set" +require "context" require "extend/pathname" require "extend/module" @@ -125,6 +125,7 @@ require "active_support/core_ext/object/blank" require "active_support/core_ext/hash/deep_merge" require "active_support/core_ext/file/atomic" +require "system_command" require "exceptions" require "utils" diff --git a/Library/Homebrew/migrator.rb b/Library/Homebrew/migrator.rb index 9309e8a084..e0903aa48f 100644 --- a/Library/Homebrew/migrator.rb +++ b/Library/Homebrew/migrator.rb @@ -5,7 +5,7 @@ require "keg" require "tab" class Migrator - extend Predicable + include Context class MigrationNeededError < RuntimeError def initialize(formula) @@ -88,8 +88,6 @@ class Migrator # path to newname keg that will be linked if old_linked_keg isn't nil attr_reader :new_linked_keg_record - attr_predicate :verbose? - def self.needs_migration?(formula) oldname = formula.oldname return false unless oldname @@ -101,20 +99,18 @@ class Migrator true end - def self.migrate_if_needed(formula, force:, verbose:) + def self.migrate_if_needed(formula, force:) return unless Migrator.needs_migration?(formula) begin - migrator = Migrator.new(formula, force: force, verbose: verbose) + migrator = Migrator.new(formula, force: force) migrator.migrate rescue => e onoe e end end - def initialize(formula, force: false, verbose: false) - @verbose = verbose - + def initialize(formula, force: false) @oldname = formula.oldname @newname = formula.name raise MigratorNoOldnameError, formula unless oldname @@ -215,7 +211,7 @@ class Migrator rescue Exception => e # rubocop:disable Lint/RescueException onoe "Error occurred while migrating." puts e - puts e.backtrace if Homebrew.args.debug? + puts e.backtrace if debug? puts "Backing up..." ignore_interrupts { backup_oldname } ensure @@ -322,7 +318,7 @@ class Migrator rescue Exception => e # rubocop:disable Lint/RescueException onoe "An unexpected error occurred during linking" puts e - puts e.backtrace if Homebrew.args.debug? + puts e.backtrace if debug? ignore_interrupts { new_keg.unlink(verbose: verbose?) } raise end diff --git a/Library/Homebrew/os/mac/keg.rb b/Library/Homebrew/os/mac/keg.rb index 90593712ff..0782226c00 100644 --- a/Library/Homebrew/os/mac/keg.rb +++ b/Library/Homebrew/os/mac/keg.rb @@ -5,7 +5,7 @@ class Keg return if file.dylib_id == id @require_relocation = true - odebug "Changing dylib ID of #{file}\n from #{file.dylib_id}\n to #{id}" if Homebrew.args.debug? + odebug "Changing dylib ID of #{file}\n from #{file.dylib_id}\n to #{id}" MachO::Tools.change_dylib_id(file, id, strict: false) rescue MachO::MachOError onoe <<~EOS @@ -20,7 +20,7 @@ class Keg return if old == new @require_relocation = true - odebug "Changing install name in #{file}\n from #{old}\n to #{new}" if Homebrew.args.debug? + odebug "Changing install name in #{file}\n from #{old}\n to #{new}" MachO::Tools.change_install_name(file, old, new, strict: false) rescue MachO::MachOError onoe <<~EOS diff --git a/Library/Homebrew/patch.rb b/Library/Homebrew/patch.rb index 009b34c672..f2a92a0616 100644 --- a/Library/Homebrew/patch.rb +++ b/Library/Homebrew/patch.rb @@ -179,12 +179,12 @@ class LegacyPatch < ExternalPatch resource.download_strategy = CurlDownloadStrategy end - def fetch(verbose: false) + def fetch clear_cache super end - def verify_download_integrity(_fn, verbose: false) + def verify_download_integrity(_fn) # no-op end diff --git a/Library/Homebrew/postinstall.rb b/Library/Homebrew/postinstall.rb index 557fd3387a..d6ae171797 100644 --- a/Library/Homebrew/postinstall.rb +++ b/Library/Homebrew/postinstall.rb @@ -17,7 +17,7 @@ begin trap("INT", old_trap) formula = args.resolved_formulae.first - formula.extend(Debrew::Formula) if Homebrew.args.debug? + formula.extend(Debrew::Formula) if args.debug? formula.run_post_install rescue Exception => e # rubocop:disable Lint/RescueException error_pipe.puts e.to_json diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 8d15dfb0c6..b76bd703fa 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -9,6 +9,7 @@ require "mktemp" # primary formula download, along with other declared resources, are instances # of this class. class Resource + include Context include FileUtils attr_reader :mirrors, :specs, :using, :source_modified_time, :patches, :owner @@ -74,10 +75,9 @@ class Resource def stage(target = nil, &block) raise ArgumentError, "target directory or block is required" if !target && block.blank? - verbose = owner.owner.verbose? prepare_patches - fetch_patches(skip_downloaded: true, verbose: verbose) - fetch(verbose: owner.owner.verbose?) unless downloaded? + fetch_patches(skip_downloaded: true) + fetch unless downloaded? unpack(target, &block) end @@ -86,12 +86,10 @@ class Resource patches.grep(DATAPatch) { |p| p.path = owner.owner.path } end - def fetch_patches(skip_downloaded: false, verbose: false) + def fetch_patches(skip_downloaded: false) external_patches = patches.select(&:external?) external_patches.reject!(&:downloaded?) if skip_downloaded - external_patches.each do |p| - p.fetch(verbose: verbose) - end + external_patches.each(&:fetch) end def apply_patches @@ -125,10 +123,10 @@ class Resource Partial.new(self, files) end - def fetch(verify_download_integrity: true, verbose: false) + def fetch(verify_download_integrity: true) HOMEBREW_CACHE.mkpath - fetch_patches(verbose: verbose) + fetch_patches begin downloader.fetch @@ -137,13 +135,13 @@ class Resource end download = cached_download - verify_download_integrity(download, verbose: verbose) if verify_download_integrity + verify_download_integrity(download) if verify_download_integrity download end - def verify_download_integrity(fn, verbose: false) + def verify_download_integrity(fn) if fn.file? - ohai "Verifying #{fn.basename} checksum" if verbose + ohai "Verifying #{fn.basename} checksum" if verbose? fn.verify_checksum(checksum) end rescue ChecksumMissingError diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index f9a80f1e3e..91ff124c09 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -239,7 +239,7 @@ class HeadSoftwareSpec < SoftwareSpec @resource.version = Version.create("HEAD") end - def verify_download_integrity(_fn, verbose: false) + def verify_download_integrity(_fn) # no-op end end diff --git a/Library/Homebrew/system_command.rb b/Library/Homebrew/system_command.rb index 3db2429ab0..c0e91059a1 100644 --- a/Library/Homebrew/system_command.rb +++ b/Library/Homebrew/system_command.rb @@ -8,7 +8,6 @@ require "shellwords" require "extend/io" require "extend/hash_validator" using HashValidator -require "extend/predicable" module Kernel def system_command(*args) @@ -21,6 +20,7 @@ module Kernel end class SystemCommand + include Context extend Predicable attr_reader :pid @@ -34,7 +34,7 @@ class SystemCommand end def run! - puts redact_secrets(command.shelljoin.gsub('\=', "="), @secrets) if verbose? || Homebrew.args.debug? + puts redact_secrets(command.shelljoin.gsub('\=', "="), @secrets) if verbose? || debug? @output = [] @@ -84,7 +84,13 @@ class SystemCommand attr_reader :executable, :args, :input, :options, :env - attr_predicate :sudo?, :print_stdout?, :print_stderr?, :verbose?, :must_succeed? + attr_predicate :sudo?, :print_stdout?, :print_stderr?, :must_succeed? + + def verbose? + return super if @verbose.nil? + + @verbose + end def env_args set_variables = env.reject { |_, value| value.nil? } @@ -160,6 +166,8 @@ class SystemCommand end class Result + include Context + attr_accessor :command, :status, :exit_status def initialize(command, output, status, secrets:) @@ -222,7 +230,7 @@ class SystemCommand end def warn_plist_garbage(garbage) - return unless Homebrew.args.verbose? + return unless verbose? return unless garbage.match?(/\S/) opoo "Received non-XML output from #{Formatter.identifier(command.first)}:" diff --git a/Library/Homebrew/test.rb b/Library/Homebrew/test.rb index 9a3d4107b7..a175ca0637 100644 --- a/Library/Homebrew/test.rb +++ b/Library/Homebrew/test.rb @@ -16,9 +16,9 @@ require "dev-cmd/test" TEST_TIMEOUT_SECONDS = 5 * 60 begin - Homebrew.args = Homebrew::CLI::Parser.new.parse(ARGV.dup.freeze, ignore_invalid_options: true) - args = Homebrew.test_args.parse + Context.current = args.context + error_pipe = UNIXSocket.open(ENV["HOMEBREW_ERROR_PIPE"], &:recv_io) error_pipe.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) @@ -34,9 +34,7 @@ begin # tests can also return false to indicate failure Timeout.timeout TEST_TIMEOUT_SECONDS do - if formula.run_test(keep_tmp: args.keep_tmp?, debug: args.debug?, verbose: args.verbose?) == false - raise "test returned false" - end + raise "test returned false" if formula.run_test(keep_tmp: args.keep_tmp?) == false end rescue Exception => e # rubocop:disable Lint/RescueException error_pipe.puts e.to_json diff --git a/Library/Homebrew/test/system_command_result_spec.rb b/Library/Homebrew/test/system_command_result_spec.rb index a424661c39..bc48fcdd75 100644 --- a/Library/Homebrew/test/system_command_result_spec.rb +++ b/Library/Homebrew/test/system_command_result_spec.rb @@ -120,7 +120,7 @@ describe SystemCommand::Result do context "when verbose" do before do - allow(Homebrew).to receive(:args).and_return(OpenStruct.new("verbose?" => true)) + allow(Context).to receive(:current).and_return(Context::ContextStruct.new(verbose: true)) end it "warns about garbage" do @@ -144,7 +144,7 @@ describe SystemCommand::Result do context "when verbose" do before do - allow(Homebrew).to receive(:args).and_return(OpenStruct.new("verbose?" => true)) + allow(Context).to receive(:current).and_return(Context::ContextStruct.new(verbose: true)) end it "warns about garbage" do diff --git a/Library/Homebrew/upgrade.rb b/Library/Homebrew/upgrade.rb index e7e8f70d60..06e314380f 100644 --- a/Library/Homebrew/upgrade.rb +++ b/Library/Homebrew/upgrade.rb @@ -26,7 +26,7 @@ module Homebrew end formulae_to_install.each do |f| - Migrator.migrate_if_needed(f, force: args.force?, verbose: args.verbose?) + Migrator.migrate_if_needed(f, force: args.force?) begin upgrade_formula(f, args: args) Cleanup.install_formula_clean!(f) diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index fce6709031..0c4068c89e 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -16,6 +16,8 @@ require "tap_constants" require "time" module Homebrew + extend Context + module_function def _system(cmd, *args, **options) @@ -34,7 +36,7 @@ module Homebrew end def system(cmd, *args, **options) - if Homebrew.args.verbose? + if verbose? puts "#{cmd} #{args * " "}".gsub(RUBY_PATH, "ruby") .gsub($LOAD_PATH.join(File::PATH_SEPARATOR).to_s, "$LOAD_PATH") end @@ -89,7 +91,7 @@ module Kernel verbose = if respond_to?(:verbose?) verbose? else - Homebrew.args.verbose? + Context.current.verbose? end title = Tty.truncate(title) if $stdout.tty? && !verbose @@ -102,10 +104,10 @@ module Kernel end def odebug(title, *sput, always_display: false) - debug = if respond_to?(:debug?) + debug = if respond_to?(:debug) debug? else - Homebrew.args.debug? + Context.current.debug? end return unless debug || always_display @@ -118,7 +120,7 @@ module Kernel verbose = if respond_to?(:verbose?) verbose? else - Homebrew.args.verbose? + Context.current.verbose? end title = Tty.truncate(title) if $stdout.tty? && !verbose && truncate == :auto @@ -387,12 +389,12 @@ module Kernel end def nostdout - if Homebrew.args.verbose? + if verbose? yield else begin out = $stdout.dup - $stdout.reopen("/dev/null") + $stdout.reopen(File::NULL) yield ensure $stdout.reopen(out) diff --git a/Library/Homebrew/utils/analytics.rb b/Library/Homebrew/utils/analytics.rb index 7921136a5c..8f0e7e0e4d 100644 --- a/Library/Homebrew/utils/analytics.rb +++ b/Library/Homebrew/utils/analytics.rb @@ -5,6 +5,8 @@ require "erb" module Utils module Analytics class << self + include Context + def report(type, metadata = {}) return if not_this_run? return if disabled? @@ -148,7 +150,7 @@ module Utils end def get_analytics(json, args:) - full_analytics = args.analytics? || Homebrew.args.verbose? + full_analytics = args.analytics? || verbose? ohai "Analytics" json["analytics"].each do |category, value| diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb index e28265867e..2318579ee0 100644 --- a/Library/Homebrew/utils/curl.rb +++ b/Library/Homebrew/utils/curl.rb @@ -34,7 +34,7 @@ def curl_args(*extra_args, show_output: false, user_agent: :default) unless show_output args << "--fail" - args << "--progress-bar" unless Homebrew.args.verbose? + args << "--progress-bar" unless Context.current.verbose? args << "--verbose" if Homebrew::EnvConfig.curl_verbose? args << "--silent" unless $stdout.tty? end