Refactor CLI options.
This commit is contained in:
parent
1959cc3f2d
commit
3b8524d770
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
expect(Hbc::CLI).not_to be_binaries
|
||||
|
||||
shutup do
|
||||
Hbc::Artifact::Binary.new(cask).install_phase
|
||||
end
|
||||
|
||||
expect(expected_path.exist?).to be false
|
||||
ensure
|
||||
Hbc::CLI.binaries = true
|
||||
end
|
||||
|
||||
expect(expected_path.exist?).to be false
|
||||
|
||||
Hbc.no_binaries = false
|
||||
end
|
||||
|
||||
it "creates parent directory if it doesn't exist" do
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user