diff --git a/Library/Homebrew/cask/lib/hbc.rb b/Library/Homebrew/cask/lib/hbc.rb index 74158b04e86dd4e5b85ad0e8f81972d057e984a1..3cdec07d223046a5ef472b84af6f872be52c1472 100644 --- a/Library/Homebrew/cask/lib/hbc.rb +++ b/Library/Homebrew/cask/lib/hbc.rb @@ -21,7 +21,6 @@ require "hbc/fetcher" require "hbc/installer" require "hbc/locations" require "hbc/macos" -require "hbc/options" require "hbc/pkg" require "hbc/qualified_token" require "hbc/scopes" @@ -40,7 +39,6 @@ require "utils" module Hbc include Locations include Scopes - include Options include Utils def self.init diff --git a/Library/Homebrew/cask/lib/hbc/artifact/binary.rb b/Library/Homebrew/cask/lib/hbc/artifact/binary.rb index 646e5c3ad07c31126af92de50fcdea770979fef3..9136d6a2a2fcdf61921b4588f92f49f0c5b0ae1e 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/binary.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/binary.rb @@ -4,7 +4,7 @@ module Hbc module Artifact class Binary < Symlinked def install_phase - super unless Hbc.no_binaries + super if CLI.binaries? end end end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index 6d6362d46d9bd7537a7d00f95ef9a4989ecbf986..c43481c82b21db3f41e9fd17b74c0ffb9b3eaf2d 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb @@ -48,7 +48,7 @@ module Hbc "-pkg", source, "-target", "/" ] - args << "-verboseR" if Hbc.verbose + args << "-verboseR" if CLI.verbose? args << "-allowUntrusted" if pkg_install_opts :allow_untrusted with_choices_file do |choices_path| args << "-applyChoiceChangesXML" << choices_path if choices_path diff --git a/Library/Homebrew/cask/lib/hbc/cask.rb b/Library/Homebrew/cask/lib/hbc/cask.rb index 41357760d33e3553bf865042d39783b739c4f1d3..52dd676b4e979ac0c21bed544f16d64a7a282650 100644 --- a/Library/Homebrew/cask/lib/hbc/cask.rb +++ b/Library/Homebrew/cask/lib/hbc/cask.rb @@ -90,8 +90,7 @@ module Hbc end def dumpcask - return unless Hbc.respond_to?(:debug) - return unless Hbc.debug + return unless CLI.debug? odebug "Cask instance dumps in YAML:" odebug "Cask instance toplevel:", to_yaml diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index afc928bb134bc0bddd45148ca4fe85a9e0a70c36..39235fa63b1424e743c8fd1c47a0c7334861f740 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -68,13 +68,25 @@ module Hbc }.freeze FLAGS = { - "--no-binaries" => :no_binaries=, - "--debug" => :debug=, - "--verbose" => :verbose=, - "--outdated" => :cleanup_outdated=, - "--help" => :help=, + ["--[no-]binaries", :binaries] => true, + ["--debug", :debug] => false, + ["--verbose", :verbose] => false, + ["--outdated", :outdated] => false, + ["--help", :help] => false, }.freeze + FLAGS.each do |(_, method), default_value| + instance_variable_set(:"@#{method}", default_value) + + define_singleton_method(:"#{method}=") do |arg| + instance_variable_set(:"@#{method}", arg) + end + + define_singleton_method(:"#{method}?") do + instance_variable_get(:"@#{method}") + end + end + def self.command_classes @command_classes ||= constants.map(&method(:const_get)) .select { |sym| sym.respond_to?(:run) } @@ -138,13 +150,13 @@ module Hbc command_string, *rest = *arguments rest = process_options(rest) - command = Hbc.help ? "help" : lookup_command(command_string) + command = help? ? "help" : lookup_command(command_string) Hbc.default_tap.install unless Hbc.default_tap.installed? Hbc.init if should_init?(command) run_command(command, *rest) rescue CaskError, CaskSha256MismatchError, ArgumentError => e msg = e.message - msg << e.backtrace.join("\n") if Hbc.debug + msg << e.backtrace.join("\n") if debug? onoe msg exit 1 rescue StandardError, ScriptError, NoMemoryError => e @@ -194,9 +206,9 @@ module Hbc EOS end - FLAGS.each do |flag, method| - opts.on(flag) do - Hbc.public_send(method, true) + FLAGS.keys.each do |flag, method| + opts.on(flag) do |bool| + send(:"#{method}=", bool) end end @@ -224,7 +236,8 @@ module Hbc end # for compat with Homebrew, not certain if this is desirable - Hbc.verbose = true if !ENV["VERBOSE"].nil? || !ENV["HOMEBREW_VERBOSE"].nil? + self.verbose = true if ARGV.verbose? + self.debug = true if ARGV.debug? remaining end diff --git a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb index 0bb3c369463a6fff21d213dc855a67a08dc413b8..9ebccabd02e1939c09187b2010c2954bcb343b34 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb @@ -21,7 +21,7 @@ module Hbc end def self.default - @default ||= new(Hbc.cache, Hbc.cleanup_outdated) + @default ||= new(Hbc.cache, CLI.outdated?) end attr_reader :cache_location, :outdated_only diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb index ac1b2049392a37383d0182f910b87f1470cd8f16..36a1ca74bb199dfe8b2fab853234b6fda9fa0af1 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb @@ -12,7 +12,7 @@ module Hbc end def self.dump_casks(*cask_tokens) - Hbc.debug = true # Yuck. At the moment this is the only way to make dumps visible + CLI.debug = true # Yuck. At the moment this is the only way to make dumps visible count = 0 cask_tokens.each do |cask_token| begin diff --git a/Library/Homebrew/cask/lib/hbc/options.rb b/Library/Homebrew/cask/lib/hbc/options.rb deleted file mode 100644 index e9ba54ff6c207901f7dcf55123de3ffe17b97f76..0000000000000000000000000000000000000000 --- a/Library/Homebrew/cask/lib/hbc/options.rb +++ /dev/null @@ -1,39 +0,0 @@ -module Hbc - module Options - def self.included(base) - base.extend(ClassMethods) - end - - module ClassMethods - attr_writer :no_binaries - - def no_binaries - @no_binaries ||= false - end - - attr_writer :debug - - def debug - @debug ||= false - end - - attr_writer :verbose - - def verbose - @verbose ||= false - end - - attr_writer :cleanup_outdated - - def cleanup_outdated - @cleanup_outdated ||= false - end - - attr_writer :help - - def help - @help ||= false - end - end - end -end diff --git a/Library/Homebrew/cask/lib/hbc/system_command.rb b/Library/Homebrew/cask/lib/hbc/system_command.rb index f26be8e62cb602ac4eca7f9ece9d36014fd5e0af..17658bdfacf505fbf0e22cd52b72b3950ea509c4 100644 --- a/Library/Homebrew/cask/lib/hbc/system_command.rb +++ b/Library/Homebrew/cask/lib/hbc/system_command.rb @@ -154,7 +154,7 @@ module Hbc def self._parse_plist(command, output) raise CaskError, "Empty plist input" unless output =~ /\S/ output.sub!(/\A(.*?)(<\?\s*xml)/m, '\2') - _warn_plist_garbage(command, Regexp.last_match[1]) if Hbc.debug + _warn_plist_garbage(command, Regexp.last_match[1]) if CLI.debug? output.sub!(%r{(<\s*/\s*plist\s*>)(.*?)\Z}m, '\1') _warn_plist_garbage(command, Regexp.last_match[2]) xml = Plist.parse_xml(output) diff --git a/Library/Homebrew/cask/lib/hbc/utils.rb b/Library/Homebrew/cask/lib/hbc/utils.rb index 7bfc4ea4298b537d1c75288ed998d32200c6015b..3fc817dd56c412d7621f16702f5385116ed71b32 100644 --- a/Library/Homebrew/cask/lib/hbc/utils.rb +++ b/Library/Homebrew/cask/lib/hbc/utils.rb @@ -30,8 +30,7 @@ end # global methods def odebug(title, *sput) - return unless Hbc.respond_to?(:debug) - return unless Hbc.debug + return unless Hbc::CLI.debug? puts Formatter.headline(title, color: :magenta) puts sput unless sput.empty? end diff --git a/Library/Homebrew/test/cask/artifact/binary_spec.rb b/Library/Homebrew/test/cask/artifact/binary_spec.rb index 1b26773caf37e4b43151ef091ba0e07924e8e4e9..e503a3ebbf58e8013d11a0fb3f12b7039534fc5f 100644 --- a/Library/Homebrew/test/cask/artifact/binary_spec.rb +++ b/Library/Homebrew/test/cask/artifact/binary_spec.rb @@ -47,15 +47,19 @@ describe Hbc::Artifact::Binary, :cask do end it "respects --no-binaries flag" do - Hbc.no_binaries = true + begin + Hbc::CLI.binaries = false - shutup do - Hbc::Artifact::Binary.new(cask).install_phase - end + expect(Hbc::CLI).not_to be_binaries - expect(expected_path.exist?).to be false + shutup do + Hbc::Artifact::Binary.new(cask).install_phase + end - Hbc.no_binaries = false + expect(expected_path.exist?).to be false + ensure + Hbc::CLI.binaries = true + end end it "creates parent directory if it doesn't exist" do diff --git a/Library/Homebrew/test/cask/cli/options_spec.rb b/Library/Homebrew/test/cask/cli/options_spec.rb index 86933e27edc488356a7964059fb81881362d4929..35dafa853de4d790b63a9c3f03d69582c8e3411f 100644 --- a/Library/Homebrew/test/cask/cli/options_spec.rb +++ b/Library/Homebrew/test/cask/cli/options_spec.rb @@ -122,17 +122,23 @@ describe Hbc::CLI, :cask do describe "--debug" do it "sets the Cask debug method to true" do - Hbc::CLI.process_options %w[help --debug] - expect(Hbc.debug).to be true - Hbc.debug = false + begin + Hbc::CLI.process_options %w[help --debug] + expect(Hbc::CLI.debug?).to be true + ensure + Hbc::CLI.debug = false + end end end describe "--help" do it "sets the Cask help method to true" do - Hbc::CLI.process_options %w[foo --help] - expect(Hbc.help).to be true - Hbc.help = false + begin + Hbc::CLI.process_options %w[foo --help] + expect(Hbc::CLI.help?).to be true + ensure + Hbc::CLI.help = false + end end end end diff --git a/Library/Homebrew/test/cask/cli_spec.rb b/Library/Homebrew/test/cask/cli_spec.rb index 1ad6790a3bcb1481b0d5d1cd6b70bc2aaa6f560b..0a4559ff21a0a51ba938ff03ab599f4ea516f91c 100644 --- a/Library/Homebrew/test/cask/cli_spec.rb +++ b/Library/Homebrew/test/cask/cli_spec.rb @@ -32,10 +32,13 @@ describe Hbc::CLI, :cask do end it "prints help output when subcommand receives `--help` flag" do - expect(described_class).to receive(:run_command).with("help") - described_class.process(%w[noop --help]) - expect(Hbc.help).to eq(true) - Hbc.help = false + begin + expect(described_class).to receive(:run_command).with("help") + described_class.process(%w[noop --help]) + expect(Hbc::CLI.help?).to eq(true) + ensure + Hbc::CLI.help = false + end end it "respects the env variable when choosing what appdir to create" do