aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Reiter2017-03-06 20:37:13 +0100
committerMarkus Reiter2017-03-07 00:06:34 +0100
commit3b8524d770172ec4aacb48fceb4dc8d2c1836bce (patch)
treebf72cd1271b3f4ab8ea3f4b6e2ed122931f5cfa9
parent1959cc3f2df6287467d90242fc6dee41398a5731 (diff)
downloadbrew-3b8524d770172ec4aacb48fceb4dc8d2c1836bce.tar.bz2
Refactor CLI options.
-rw-r--r--Library/Homebrew/cask/lib/hbc.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/artifact/binary.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/artifact/pkg.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/cask.rb3
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli.rb35
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/cleanup.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/options.rb39
-rw-r--r--Library/Homebrew/cask/lib/hbc/system_command.rb2
-rw-r--r--Library/Homebrew/cask/lib/hbc/utils.rb3
-rw-r--r--Library/Homebrew/test/cask/artifact/binary_spec.rb16
-rw-r--r--Library/Homebrew/test/cask/cli/options_spec.rb18
-rw-r--r--Library/Homebrew/test/cask/cli_spec.rb11
13 files changed, 60 insertions, 77 deletions
diff --git a/Library/Homebrew/cask/lib/hbc.rb b/Library/Homebrew/cask/lib/hbc.rb
index c971cbd58..048f912ec 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 646e5c3ad..9136d6a2a 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 6d6362d46..c43481c82 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 cb0fdb665..ae0eed792 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 8e178e373..bb88a6297 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) }
@@ -149,13 +161,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
@@ -205,9 +217,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
@@ -235,7 +247,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 2273280b9..5b6b00560 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 ac1b20493..36a1ca74b 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 e9ba54ff6..000000000
--- 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 f26be8e62..17658bdfa 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 ef3e5eda3..6b839345a 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 1b26773ca..e503a3ebb 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 86933e27e..35dafa853 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 1ad6790a3..0a4559ff2 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