diff options
| author | Markus Reiter | 2017-05-23 13:27:44 +0200 |
|---|---|---|
| committer | GitHub | 2017-05-23 13:27:44 +0200 |
| commit | d1f599f1ed51976fc76ad19803de04864d12ef2b (patch) | |
| tree | 9096569a3630b7ee9bd5751811e35c043ab1a02d /Library/Homebrew/cask | |
| parent | 04420d07a4c127b9a85013f31619eed4b88ec94b (diff) | |
| parent | 02a1e2781fcb808b731fd04d18369ec5bfd3bc68 (diff) | |
| download | brew-d1f599f1ed51976fc76ad19803de04864d12ef2b.tar.bz2 | |
Merge pull request #2654 from reitermarkus/refactor-cli
Refactor `CLI`.
Diffstat (limited to 'Library/Homebrew/cask')
39 files changed, 518 insertions, 435 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/artifact.rb b/Library/Homebrew/cask/lib/hbc/artifact.rb index b155a125a..e5eb54c36 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact.rb @@ -54,12 +54,12 @@ module Hbc Zap, ].freeze - def self.for_cask(cask, command: SystemCommand, force: false) + def self.for_cask(cask, options = {}) odebug "Determining which artifacts are present in Cask #{cask}" TYPES .select { |klass| klass.me?(cask) } - .map { |klass| klass.new(cask, command: command, force: force) } + .map { |klass| klass.new(cask, options) } end end end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/base.rb b/Library/Homebrew/cask/lib/hbc/artifact/base.rb index 924493fc0..96349f081 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/base.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/base.rb @@ -65,10 +65,19 @@ module Hbc {} end - def initialize(cask, command: SystemCommand, force: false) + def verbose? + @verbose + end + + def force? + @force + end + + def initialize(cask, command: SystemCommand, force: false, verbose: false) @cask = cask @command = command @force = force + @verbose = verbose end end end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/binary.rb b/Library/Homebrew/cask/lib/hbc/artifact/binary.rb index 21d123ab9..7178c2af6 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/binary.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/binary.rb @@ -3,10 +3,6 @@ require "hbc/artifact/symlinked" module Hbc module Artifact class Binary < Symlinked - def install_phase - super if CLI.binaries? - end - def link super return if source.executable? diff --git a/Library/Homebrew/cask/lib/hbc/artifact/moved.rb b/Library/Homebrew/cask/lib/hbc/artifact/moved.rb index eaaa49e20..3fe969c0c 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/moved.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/moved.rb @@ -20,7 +20,7 @@ module Hbc def move if Utils.path_occupied?(target) message = "It seems there is already #{self.class.artifact_english_article} #{self.class.artifact_english_name} at '#{target}'" - raise CaskError, "#{message}." unless force + raise CaskError, "#{message}." unless force? opoo "#{message}; overwriting." delete end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index c43481c82..be0a6be71 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 CLI.verbose? + args << "-verboseR" if 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/artifact/uninstall_base.rb b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb index 478f313b5..2ddd0cfb3 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb @@ -173,7 +173,7 @@ module Hbc unless executable_path.exist? message = "uninstall script #{executable} does not exist" - raise CaskError, "#{message}." unless force + raise CaskError, "#{message}." unless force? opoo "#{message}, skipping." return end diff --git a/Library/Homebrew/cask/lib/hbc/cask.rb b/Library/Homebrew/cask/lib/hbc/cask.rb index a193a394e..47f3b176a 100644 --- a/Library/Homebrew/cask/lib/hbc/cask.rb +++ b/Library/Homebrew/cask/lib/hbc/cask.rb @@ -74,8 +74,6 @@ module Hbc end def dumpcask - 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 37784f4c3..1c20a2a33 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -2,8 +2,9 @@ require "optparse" require "shellwords" require "extend/optparse" +require "hbc/cli/options" -require "hbc/cli/base" +require "hbc/cli/abstract_command" require "hbc/cli/audit" require "hbc/cli/cat" require "hbc/cli/cleanup" @@ -23,7 +24,7 @@ require "hbc/cli/uninstall" require "hbc/cli/--version" require "hbc/cli/zap" -require "hbc/cli/internal_use_base" +require "hbc/cli/abstract_internal_command" require "hbc/cli/internal_audit_modified_casks" require "hbc/cli/internal_appcast_checkpoint" require "hbc/cli/internal_checkurl" @@ -44,54 +45,41 @@ module Hbc "remove" => "uninstall", "abv" => "info", "dr" => "doctor", - # aliases from Homebrew that we don't (yet) support - # 'ln' => 'link', - # 'configure' => 'diy', - # '--repo' => '--repository', - # 'environment' => '--env', - # '-c1' => '--config', }.freeze - OPTIONS = { - "--caskroom=" => :caskroom=, - "--appdir=" => :appdir=, - "--colorpickerdir=" => :colorpickerdir=, - "--prefpanedir=" => :prefpanedir=, - "--qlplugindir=" => :qlplugindir=, - "--dictionarydir=" => :dictionarydir=, - "--fontdir=" => :fontdir=, - "--servicedir=" => :servicedir=, - "--input_methoddir=" => :input_methoddir=, - "--internet_plugindir=" => :internet_plugindir=, - "--audio_unit_plugindir=" => :audio_unit_plugindir=, - "--vst_plugindir=" => :vst_plugindir=, - "--vst3_plugindir=" => :vst3_plugindir=, - "--screen_saverdir=" => :screen_saverdir=, - }.freeze - - FLAGS = { - ["--[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 + include Options + + option "--caskroom=PATH", ->(value) { Hbc.caskroom = value } + option "--appdir=PATH", ->(value) { Hbc.appdir = value } + option "--colorpickerdir=PATH", ->(value) { Hbc.colorpickerdir = value } + option "--prefpanedir=PATH", ->(value) { Hbc.prefpanedir = value } + option "--qlplugindir=PATH", ->(value) { Hbc.qlplugindir = value } + option "--dictionarydir=PATH", ->(value) { Hbc.dictionarydir = value } + option "--fontdir=PATH", ->(value) { Hbc.fontdir = value } + option "--servicedir=PATH", ->(value) { Hbc.servicedir = value } + option "--input_methoddir=PATH", ->(value) { Hbc.input_methoddir = value } + option "--internet_plugindir=PATH", ->(value) { Hbc.internet_plugindir = value } + option "--audio_unit_plugindir=PATH", ->(value) { Hbc.audio_unit_plugindir = value } + option "--vst_plugindir=PATH", ->(value) { Hbc.vst_plugindir = value } + option "--vst3_plugindir=PATH", ->(value) { Hbc.vst3_plugindir = value } + option "--screen_saverdir=PATH", ->(value) { Hbc.screen_saverdir = value } + option "--binarydir=PATH", ->(*) { opoo(<<-EOS.undent) } + Option --binarydir is obsolete! + Homebrew-Cask now uses the same location as your Homebrew installation for executable links. + EOS + + option "--help", :help, false + + # handled in OS::Mac + option "--language a,b,c", ->(*) { raise OptionParser::InvalidOption } + + # override default handling of --version + option "--version", ->(*) { raise OptionParser::InvalidOption } def self.command_classes @command_classes ||= constants.map(&method(:const_get)) - .select { |sym| sym.respond_to?(:run) } + .select { |klass| klass.respond_to?(:run) } + .reject(&:abstract?) .sort_by(&:command_name) end @@ -99,14 +87,14 @@ module Hbc @commands ||= command_classes.map(&:command_name) end - def self.lookup_command(command_string) + def self.lookup_command(command_name) @lookup ||= Hash[commands.zip(command_classes)] - command_string = ALIASES.fetch(command_string, command_string) - @lookup.fetch(command_string, command_string) + command_name = ALIASES.fetch(command_name, command_name) + @lookup.fetch(command_name, command_name) end def self.should_init?(command) - (command.is_a? Class) && (command < CLI::Base) && command.needs_init? + command.is_a?(Class) && !command.abstract? && command.needs_init? end def self.run_command(command, *rest) @@ -145,20 +133,28 @@ module Hbc end end - def self.process(arguments) + def self.run(*args) + new(*args).run + end + + def initialize(*args) + @args = process_options(*args) + end + + def run + command_name, *args = *@args + command = help? ? "help" : self.class.lookup_command(command_name) + unless ENV["MACOS_VERSION"].nil? MacOS.full_version = ENV["MACOS_VERSION"] end - command_string, *rest = *arguments - rest = process_options(rest) - 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 + Hbc.init if self.class.should_init?(command) + self.class.run_command(command, *args) + rescue CaskError, CaskSha256MismatchError, ArgumentError, OptionParser::InvalidOption => e msg = e.message - msg << e.backtrace.join("\n") if debug? + msg << e.backtrace.join("\n") if ARGV.debug? onoe msg exit 1 rescue StandardError, ScriptError, NoMemoryError => e @@ -188,60 +184,25 @@ module Hbc list.sort end - def self.parser - # If you modify these arguments, please update USAGE.md - @parser ||= OptionParser.new do |opts| - opts.on("--language STRING") do - # handled in OS::Mac - end - - OPTIONS.each do |option, method| - opts.on("#{option}" "PATH", Pathname) do |path| - Hbc.public_send(method, path) - end - end - - opts.on("--binarydir=PATH") do - opoo <<-EOS.undent - Option --binarydir is obsolete! - Homebrew-Cask now uses the same location as your Homebrew installation for executable links. - EOS - end + def process_options(*args) + all_args = Shellwords.shellsplit(ENV["HOMEBREW_CASK_OPTS"] || "") + args - FLAGS.keys.each do |flag, method| - opts.on(flag) do |bool| - send(:"#{method}=", bool) - end - end + non_options = [] - opts.on("--version") do - raise OptionParser::InvalidOption # override default handling of --version - end + if idx = all_args.index("--") + non_options += all_args.drop(idx) + all_args = all_args.first(idx) end - end - def self.process_options(args) - all_args = Shellwords.shellsplit(ENV["HOMEBREW_CASK_OPTS"] || "") + args - remaining = [] - until all_args.empty? + remaining = all_args.select do |arg| begin - head = all_args.shift - remaining.concat(parser.parse([head])) - rescue OptionParser::InvalidOption - remaining << head - retry - rescue OptionParser::MissingArgument - raise ArgumentError, "The option '#{head}' requires an argument." - rescue OptionParser::AmbiguousOption - raise ArgumentError, "There is more than one possible option that starts with '#{head}'." + !process_arguments([arg]).empty? + rescue OptionParser::InvalidOption, OptionParser::MissingArgument, OptionParser::AmbiguousOption + true end end - # for compat with Homebrew, not certain if this is desirable - self.verbose = true if ARGV.verbose? - self.debug = true if ARGV.debug? - - remaining + remaining + non_options end class NullCommand diff --git a/Library/Homebrew/cask/lib/hbc/cli/--version.rb b/Library/Homebrew/cask/lib/hbc/cli/--version.rb index bbc719c3b..1833c51c6 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/--version.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/--version.rb @@ -1,12 +1,17 @@ module Hbc class CLI - class Version < Base + class Version < AbstractCommand def self.command_name "--#{super}" end - def self.run(*args) - raise ArgumentError, "#{command_name} does not take arguments." unless args.empty? + def initialize(*) + super + return if args.empty? + raise ArgumentError, "#{self.class.command_name} does not take arguments." + end + + def run puts Hbc.full_version end diff --git a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb new file mode 100644 index 000000000..5264799ed --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb @@ -0,0 +1,49 @@ +require_relative "options" + +module Hbc + class CLI + class AbstractCommand + include Options + + option "--[no-]binaries", :binaries, true + option "--debug", :debug, false + option "--verbose", :verbose, false + option "--outdated", :outdated_only, false + + def self.command_name + @command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase + end + + def self.abstract? + !(name.split("::").last !~ /^Abstract[^a-z]/) + end + + def self.visible + true + end + + def self.cask_tokens_from(args) + args.reject { |a| a.empty? || a.chars.first == "-" } + end + + def self.help + nil + end + + def self.needs_init? + false + end + + def self.run(*args) + new(*args).run + end + + attr_accessor :args + private :args= + + def initialize(*args) + @args = process_arguments(*args) + end + end + end +end diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_use_base.rb b/Library/Homebrew/cask/lib/hbc/cli/abstract_internal_command.rb index b1f9c5631..8af71e589 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_use_base.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/abstract_internal_command.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class InternalUseBase < Base + class AbstractInternalCommand < AbstractCommand def self.command_name super.sub(/^internal_/i, "_") end diff --git a/Library/Homebrew/cask/lib/hbc/cli/audit.rb b/Library/Homebrew/cask/lib/hbc/cli/audit.rb index ec1c33754..74d1ebfa7 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/audit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/audit.rb @@ -1,51 +1,27 @@ module Hbc class CLI - class Audit < Base + class Audit < AbstractCommand + option "--download", :download, false + option "--token-conflicts", :token_conflicts, false + def self.help "verifies installability of Casks" end - def self.run(*args) - failed_casks = new(args, Auditor).run - return if failed_casks.empty? - raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}" - end - - def initialize(args, auditor) - @args = args - @auditor = auditor - end - def run - casks_to_audit.each_with_object([]) do |cask, failed| - failed << cask unless audit(cask) + casks_to_audit = args.empty? ? Hbc.all : args.map(&CaskLoader.public_method(:load)) + + failed_casks = casks_to_audit.reject do |cask| + audit(cask) end + + return if failed_casks.empty? + raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}" end def audit(cask) odebug "Auditing Cask #{cask}" - @auditor.audit(cask, audit_download: audit_download?, - check_token_conflicts: check_token_conflicts?) - end - - def audit_download? - @args.include?("--download") - end - - def check_token_conflicts? - @args.include?("--token-conflicts") - end - - def casks_to_audit - if cask_tokens.empty? - Hbc.all - else - cask_tokens.map { |token| CaskLoader.load(token) } - end - end - - def cask_tokens - @cask_tokens ||= self.class.cask_tokens_from(@args) + Auditor.audit(cask, audit_download: download?, check_token_conflicts: token_conflicts?) end def self.needs_init? diff --git a/Library/Homebrew/cask/lib/hbc/cli/base.rb b/Library/Homebrew/cask/lib/hbc/cli/base.rb deleted file mode 100644 index 3301cad91..000000000 --- a/Library/Homebrew/cask/lib/hbc/cli/base.rb +++ /dev/null @@ -1,25 +0,0 @@ -module Hbc - class CLI - class Base - def self.command_name - @command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase - end - - def self.visible - true - end - - def self.cask_tokens_from(args) - args.reject { |a| a.empty? || a.chars.first == "-" } - end - - def self.help - nil - end - - def self.needs_init? - false - end - end - end -end diff --git a/Library/Homebrew/cask/lib/hbc/cli/cat.rb b/Library/Homebrew/cask/lib/hbc/cli/cat.rb index 52f6e0eab..e68481b46 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cat.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cat.rb @@ -1,14 +1,17 @@ module Hbc class CLI - class Cat < Base - def self.run(*args) - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - # only respects the first argument - cask_token = cask_tokens.first.sub(/\.rb$/i, "") - cask_path = CaskLoader.path(cask_token) - raise CaskUnavailableError, cask_token.to_s unless cask_path.exist? - puts File.open(cask_path, &:read) + class Cat < AbstractCommand + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + + def run + args.each do |cask_token| + cask_path = CaskLoader.path(cask_token) + raise CaskUnavailableError, cask_token.to_s unless cask_path.exist? + puts File.open(cask_path, &:read) + end end def self.help diff --git a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb index 9ebccabd0..e25393230 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Cleanup < Base + class Cleanup < AbstractCommand OUTDATED_DAYS = 10 OUTDATED_TIMESTAMP = Time.now - (60 * 60 * 24 * OUTDATED_DAYS) @@ -12,30 +12,15 @@ module Hbc true end - def self.run(*args) - if args.empty? - default.cleanup! - else - default.cleanup(args) - end - end + attr_reader :cache_location - def self.default - @default ||= new(Hbc.cache, CLI.outdated?) - end - - attr_reader :cache_location, :outdated_only - def initialize(cache_location, outdated_only) + def initialize(*args, cache_location: Hbc.cache) + super(*args) @cache_location = Pathname.new(cache_location) - @outdated_only = outdated_only - end - - def cleanup! - remove_cache_files end - def cleanup(tokens) - remove_cache_files(*tokens) + def run + remove_cache_files(*@args) end def cache_files @@ -46,7 +31,7 @@ module Hbc end def outdated?(file) - outdated_only && file && file.stat.mtime > OUTDATED_TIMESTAMP + outdated_only? && file && file.stat.mtime > OUTDATED_TIMESTAMP end def incomplete?(file) @@ -68,7 +53,7 @@ module Hbc def remove_cache_files(*tokens) message = "Removing cached downloads" message.concat " for #{tokens.join(", ")}" unless tokens.empty? - message.concat " older than #{OUTDATED_DAYS} days old" if outdated_only + message.concat " older than #{OUTDATED_DAYS} days old" if outdated_only? ohai message deletable_cache_files = if tokens.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/create.rb b/Library/Homebrew/cask/lib/hbc/cli/create.rb index 5e143d085..8de101092 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/create.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/create.rb @@ -1,17 +1,20 @@ module Hbc class CLI - class Create < Base - def self.run(*args) - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - cask_token = cask_tokens.first.sub(/\.rb$/i, "") - cask_path = CaskLoader.path(cask_token) - odebug "Creating Cask #{cask_token}" + class Create < AbstractCommand + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + raise ArgumentError, "Only one Cask can be created at a time." if args.count > 1 + end + def run + cask_token = args.first + cask_path = CaskLoader.path(cask_token) raise CaskAlreadyCreatedError, cask_token if cask_path.exist? + odebug "Creating Cask #{cask_token}" File.open(cask_path, "w") do |f| - f.write template(cask_token) + f.write self.class.template(cask_token) end exec_editor cask_path diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 031f78824..64cd11f9e 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -1,14 +1,20 @@ module Hbc class CLI - class Doctor < Base - def self.run + class Doctor < AbstractCommand + def initialize(*) + super + return if args.empty? + raise ArgumentError, "#{self.class.command_name} does not take arguments." + end + + def run ohai "Homebrew-Cask Version", Hbc.full_version - ohai "Homebrew-Cask Install Location", render_install_location - ohai "Homebrew-Cask Staging Location", render_staging_location(Hbc.caskroom) - ohai "Homebrew-Cask Cached Downloads", render_cached_downloads + ohai "Homebrew-Cask Install Location", self.class.render_install_location + ohai "Homebrew-Cask Staging Location", self.class.render_staging_location(Hbc.caskroom) + ohai "Homebrew-Cask Cached Downloads", self.class.render_cached_downloads ohai "Homebrew-Cask Taps:" - puts render_taps(Hbc.default_tap, *alt_taps) - ohai "Contents of $LOAD_PATH", render_load_path($LOAD_PATH) + puts self.class.render_taps(Hbc.default_tap, *self.class.alt_taps) + ohai "Contents of $LOAD_PATH", self.class.render_load_path($LOAD_PATH) ohai "Environment Variables" environment_variables = [ @@ -24,7 +30,7 @@ module Hbc "SHELL", ] - (locale_variables + environment_variables).sort.each(&method(:render_env_var)) + (self.class.locale_variables + environment_variables).sort.each(&self.class.method(:render_env_var)) end def self.locale_variables @@ -45,7 +51,7 @@ module Hbc end def self.alt_taps - Tap.select { |t| t.cask_dir && t != Hbc.default_tap } + Tap.select { |t| t.cask_dir.exist? && t != Hbc.default_tap } end def self.cask_count_for_tap(tap) @@ -107,7 +113,7 @@ module Hbc end def self.render_cached_downloads - cleanup = CLI::Cleanup.default + cleanup = CLI::Cleanup.new count = cleanup.cache_files.count size = cleanup.disk_cleanup_size msg = user_tilde(Hbc.cache.to_s) diff --git a/Library/Homebrew/cask/lib/hbc/cli/edit.rb b/Library/Homebrew/cask/lib/hbc/cli/edit.rb index 1f1e0d918..dec0fe36b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/edit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/edit.rb @@ -1,16 +1,21 @@ module Hbc class CLI - class Edit < Base - def self.run(*args) - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - # only respects the first argument - cask_token = cask_tokens.first.sub(/\.rb$/i, "") + class Edit < AbstractCommand + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + raise ArgumentError, "Only one Cask can be created at a time." if args.count > 1 + end + + def run + cask_token = args.first cask_path = CaskLoader.path(cask_token) - odebug "Opening editor for Cask #{cask_token}" + unless cask_path.exist? raise CaskUnavailableError, %Q(#{cask_token}, run "brew cask create #{cask_token}" to create a new Cask) end + + odebug "Opening editor for Cask #{cask_token}" exec_editor cask_path end diff --git a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb index 83dba672c..2c1cc5f66 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb @@ -1,15 +1,18 @@ module Hbc class CLI - class Fetch < Base - def self.run(*args) - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - force = args.include? "--force" + class Fetch < AbstractCommand + option "--force", :force, false - cask_tokens.each do |cask_token| + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + + def run + args.each do |cask_token| ohai "Downloading external files for Cask #{cask_token}" cask = CaskLoader.load(cask_token) - downloaded_path = Download.new(cask, force: force).perform + downloaded_path = Download.new(cask, force: force?).perform Verify.all(cask, downloaded_path) ohai "Success! Downloaded to -> #{downloaded_path}" end diff --git a/Library/Homebrew/cask/lib/hbc/cli/home.rb b/Library/Homebrew/cask/lib/hbc/cli/home.rb index 66be49186..009bc1e3e 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/home.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/home.rb @@ -1,19 +1,24 @@ module Hbc class CLI - class Home < Base - def self.run(*cask_tokens) - if cask_tokens.empty? + class Home < AbstractCommand + def run + casks = args.map(&CaskLoader.public_method(:load)) + + if casks.empty? odebug "Opening project homepage" - system "/usr/bin/open", "--", "https://caskroom.github.io/" + self.class.open_url "https://caskroom.github.io/" else - cask_tokens.each do |cask_token| - odebug "Opening homepage for Cask #{cask_token}" - cask = CaskLoader.load(cask_token) - system "/usr/bin/open", "--", cask.homepage + casks.each do |cask| + odebug "Opening homepage for Cask #{cask}" + self.class.open_url cask.homepage end end end + def self.open_url(url) + SystemCommand.run!(OS::PATH_OPEN, args: ["--", url]) + end + def self.help "opens the homepage of the given Cask" end diff --git a/Library/Homebrew/cask/lib/hbc/cli/info.rb b/Library/Homebrew/cask/lib/hbc/cli/info.rb index 625b4ecae..dcfc8d9bb 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/info.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/info.rb @@ -1,14 +1,17 @@ module Hbc class CLI - class Info < Base - def self.run(*args) - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - cask_tokens.each do |cask_token| + class Info < AbstractCommand + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + + def run + args.each do |cask_token| odebug "Getting info for Cask #{cask_token}" cask = CaskLoader.load(cask_token) - info(cask) + self.class.info(cask) end end diff --git a/Library/Homebrew/cask/lib/hbc/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb index 438f860c1..5acd837b1 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/install.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb @@ -1,28 +1,33 @@ module Hbc class CLI - class Install < Base - def self.run(*args) - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - force = args.include? "--force" - skip_cask_deps = args.include? "--skip-cask-deps" - require_sha = args.include? "--require-sha" - retval = install_casks cask_tokens, force, skip_cask_deps, require_sha + class Install < AbstractCommand + option "--force", :force, false + option "--skip-cask-deps", :skip_cask_deps, false + option "--require-sha", :require_sha, false + + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + + def run + retval = install_casks # retval is ternary: true/false/nil raise CaskError, "nothing to install" if retval.nil? raise CaskError, "install incomplete" unless retval end - def self.install_casks(cask_tokens, force, skip_cask_deps, require_sha) + def install_casks count = 0 - cask_tokens.each do |cask_token| + args.each do |cask_token| begin cask = CaskLoader.load(cask_token) - Installer.new(cask, - force: force, - skip_cask_deps: skip_cask_deps, - require_sha: require_sha).install + Installer.new(cask, binaries: binaries?, + verbose: verbose?, + force: force?, + skip_cask_deps: skip_cask_deps?, + require_sha: require_sha?).install count += 1 rescue CaskAlreadyInstalledError => e opoo e.message @@ -31,7 +36,7 @@ module Hbc opoo e.message count += 1 rescue CaskUnavailableError => e - warn_unavailable_with_suggestion cask_token, e + self.class.warn_unavailable_with_suggestion cask_token, e rescue CaskNoShasumError => e opoo e.message count += 1 @@ -39,7 +44,8 @@ module Hbc onoe e.message end end - count.zero? ? nil : count == cask_tokens.length + + count.zero? ? nil : count == args.length end def self.warn_unavailable_with_suggestion(cask_token, e) diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb index da3567108..6b83b2446 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb @@ -1,15 +1,18 @@ module Hbc class CLI - class InternalAppcastCheckpoint < InternalUseBase - def self.run(*args) - calculate = args.include? "--calculate" - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? + class InternalAppcastCheckpoint < AbstractInternalCommand + option "--calculate", :calculate, false + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + + def run if cask_tokens.all? { |t| t =~ %r{^https?://} && t !~ /\.rb$/ } - appcask_checkpoint_for_url(cask_tokens) + self.class.appcask_checkpoint_for_url(cask_tokens) else - appcask_checkpoint(cask_tokens, calculate) + self.class.appcask_checkpoint(cask_tokens, calculate?) end end diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb index 1a8ca0e98..f1a0308e5 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb @@ -1,26 +1,29 @@ module Hbc class CLI - class InternalAuditModifiedCasks < InternalUseBase + class InternalAuditModifiedCasks < AbstractInternalCommand RELEVANT_STANZAS = [:version, :sha256, :url, :appcast].freeze + option "--cleanup", :cleanup, false + def self.needs_init? true end - def self.run(*args) - commit_range = commit_range(args) - cleanup = args.any? { |a| a =~ /^-+c(leanup)?$/i } - new(commit_range, cleanup: cleanup).run - end + attr_accessor :commit_range + private :commit_range= - def self.commit_range(args) - posargs = args.reject { |a| a.empty? || a.chars.first == "-" } - odie usage unless posargs.size == 1 - posargs.first - end + def initialize(*) + super + + if args.count != 1 + raise ArgumentError, <<-EOS.undent + This command requires exactly one argument. + + #{self.class.usage} + EOS + end - def self.posargs(args) - args.reject { |a| a.empty? || a.chars.first == "-" } + @commit_range = args.first end def self.help @@ -41,17 +44,6 @@ module Hbc EOS end - def initialize(commit_range, cleanup: false) - @commit_range = commit_range - @cleanup = cleanup - end - - attr_reader :commit_range - - def cleanup? - @cleanup - end - def run at_exit do cleanup diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb index b7d95957d..a8c3d5c8f 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb @@ -1,8 +1,8 @@ module Hbc class CLI - class InternalCheckurl < InternalUseBase - def self.run(*args) - casks_to_check = args.empty? ? Hbc.all : args.map { |arg| CaskLoader.load(arg) } + class InternalCheckurl < AbstractInternalCommand + def run + casks_to_check = args.empty? ? Hbc.all : args.map(&CaskLoader.public_method(:load)) casks_to_check.each do |cask| odebug "Checking URL for Cask #{cask}" checker = UrlChecker.new(cask) diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb index 8017a32cf..78dbf1622 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb @@ -1,20 +1,22 @@ module Hbc class CLI - class InternalDump < InternalUseBase - def self.run(*arguments) - cask_tokens = cask_tokens_from(arguments) - raise CaskUnspecifiedError if cask_tokens.empty? - retval = dump_casks(*cask_tokens) + class InternalDump < AbstractInternalCommand + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + + def run + retval = dump_casks # retval is ternary: true/false/nil raise CaskError, "nothing to dump" if retval.nil? raise CaskError, "dump incomplete" unless retval end - def self.dump_casks(*cask_tokens) - CLI.debug = true # Yuck. At the moment this is the only way to make dumps visible + def dump_casks count = 0 - cask_tokens.each do |cask_token| + args.each do |cask_token| begin cask = CaskLoader.load(cask_token) count += 1 @@ -23,7 +25,7 @@ module Hbc opoo "#{cask_token} was not found or would not load: #{e}" end end - count.zero? ? nil : count == cask_tokens.length + count.zero? ? nil : count == args.length end def self.help diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb index 0908ee05e..beac77b29 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb @@ -1,12 +1,18 @@ module Hbc class CLI - class InternalHelp < InternalUseBase - def self.run(*_ignored) + class InternalHelp < AbstractInternalCommand + def initialize(*) + super + return if args.empty? + raise ArgumentError, "#{self.class.command_name} does not take arguments." + end + + def run max_command_len = CLI.commands.map(&:length).max puts "Unstable Internal-use Commands:\n\n" CLI.command_classes.each do |klass| next if klass.visible - puts " #{klass.command_name.ljust(max_command_len)} #{help_for(klass)}" + puts " #{klass.command_name.ljust(max_command_len)} #{self.class.help_for(klass)}" end puts "\n" end diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb index 303aa7ffe..86dee7c9c 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class InternalStanza < InternalUseBase + class InternalStanza < AbstractInternalCommand # Syntax # # brew cask _stanza <stanza_name> [ --table | --yaml | --inspect | --quiet ] [ <cask_token> ... ] @@ -50,71 +50,82 @@ module Hbc :uninstall_postflight, ] - def self.run(*args) + option "--table", :table, false + option "--quiet", :quiet, false + option "--yaml", :yaml, false + option "--inspect", :inspect, false + + attr_accessor :format + private :format, :format= + + attr_accessor :stanza + private :stanza, :stanza= + + def initialize(*) + super raise ArgumentError, "No stanza given." if args.empty? - table = args.include? "--table" - quiet = args.include? "--quiet" - format = :to_yaml if args.include? "--yaml" - format = :inspect if args.include? "--inspect" - cask_tokens = cask_tokens_from(args) - stanza = cask_tokens.shift.to_sym - cask_tokens = Hbc.all_tokens if cask_tokens.empty? + @stanza = args.shift.to_sym - retval = print_stanzas(stanza, format, table, quiet, *cask_tokens) + @format = :to_yaml if yaml? + @format = :inspect if inspect? + end + def run + retval = print_stanzas # retval is ternary: true/false/nil if retval.nil? - exit 1 if quiet + exit 1 if quiet? raise CaskError, "nothing to print" elsif !retval - exit 1 if quiet + exit 1 if quiet? raise CaskError, "print incomplete" end end - def self.print_stanzas(stanza, format = nil, table = nil, quiet = nil, *cask_tokens) + def print_stanzas count = 0 if ARTIFACTS.include?(stanza) artifact_name = stanza - stanza = :artifacts + @stanza = :artifacts end + cask_tokens = args.empty? ? Hbc.all_tokens : args cask_tokens.each do |cask_token| - print "#{cask_token}\t" if table + print "#{cask_token}\t" if table? begin cask = CaskLoader.load(cask_token) rescue StandardError - opoo "Cask '#{cask_token}' was not found" unless quiet + opoo "Cask '#{cask_token}' was not found" unless quiet? puts "" next end unless cask.respond_to?(stanza) - opoo "no such stanza '#{stanza}' on Cask '#{cask_token}'" unless quiet + opoo "no such stanza '#{stanza}' on Cask '#{cask_token}'" unless quiet? puts "" next end begin - value = cask.send(stanza) + value = cask.send(@stanza) rescue StandardError - opoo "failure calling '#{stanza}' on Cask '#{cask_token}'" unless quiet + opoo "failure calling '#{stanza}' on Cask '#{cask_token}'" unless quiet? puts "" next end if artifact_name && !value.key?(artifact_name) - opoo "no such stanza '#{artifact_name}' on Cask '#{cask_token}'" unless quiet + opoo "no such stanza '#{artifact_name}' on Cask '#{cask_token}'" unless quiet? puts "" next end value = value.fetch(artifact_name).to_a.flatten if artifact_name - if format - puts value.send(format) + if @format + puts value.send(@format) elsif artifact_name || value.is_a?(Symbol) puts value.inspect else diff --git a/Library/Homebrew/cask/lib/hbc/cli/list.rb b/Library/Homebrew/cask/lib/hbc/cli/list.rb index d9bf2187b..04dccbf85 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/list.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/list.rb @@ -1,19 +1,18 @@ module Hbc class CLI - class List < Base - def self.run(*arguments) - @options = {} - @options[:one] = true if arguments.delete("-1") - @options[:versions] = true if arguments.delete("--versions") + class List < AbstractCommand + option "-1", :one, false + option "--versions", :versions, false - if arguments.delete("-l") - @options[:one] = true - opoo "Option -l is obsolete! Implying option -1." - end + option "-l", (lambda do |*| + one = true # rubocop:disable Lint/UselessAssignment + opoo "Option -l is obsolete! Implying option -1." + end) - retval = arguments.any? ? list(*arguments) : list_installed + def run + retval = args.any? ? list : list_installed # retval is ternary: true/false/nil - if retval.nil? && !arguments.any? + if retval.nil? && !args.any? opoo "nothing to list" # special case: avoid exit code elsif retval.nil? raise CaskError, "nothing to list" @@ -22,22 +21,22 @@ module Hbc end end - def self.list(*cask_tokens) + def list count = 0 - cask_tokens.each do |cask_token| + args.each do |cask_token| odebug "Listing files for Cask #{cask_token}" begin cask = CaskLoader.load(cask_token) if cask.installed? - if @options[:one] + if one? puts cask.token - elsif @options[:versions] - puts format_versioned(cask) + elsif versions? + puts self.class.format_versioned(cask) else cask = CaskLoader.load_from_file(cask.installed_caskfile) - list_artifacts(cask) + self.class.list_artifacts(cask) end count += 1 @@ -49,7 +48,7 @@ module Hbc end end - count.zero? ? nil : count == cask_tokens.length + count.zero? ? nil : count == args.length end def self.list_artifacts(cask) @@ -59,13 +58,13 @@ module Hbc end end - def self.list_installed + def list_installed installed_casks = Hbc.installed - if @options[:one] + if one? puts installed_casks.map(&:to_s) - elsif @options[:versions] - puts installed_casks.map(&method(:format_versioned)) + elsif versions? + puts installed_casks.map(&self.class.method(:format_versioned)) elsif !installed_casks.empty? puts Formatter.columns(installed_casks.map(&:to_s)) end diff --git a/Library/Homebrew/cask/lib/hbc/cli/options.rb b/Library/Homebrew/cask/lib/hbc/cli/options.rb new file mode 100644 index 000000000..84126caa5 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/cli/options.rb @@ -0,0 +1,62 @@ +module Hbc + class CLI + module Options + def self.included(klass) + klass.extend(ClassMethods) + end + + module ClassMethods + def options + @options ||= {} + return @options unless superclass.respond_to?(:options) + superclass.options.merge(@options) + end + + def option(name, method, default_value = nil) + @options ||= {} + @options[name] = method + + return if method.respond_to?(:call) + + define_method(:"#{method}=") do |value| + instance_variable_set(:"@#{method}", value) + end + + if [true, false].include?(default_value) + define_method(:"#{method}?") do + instance_variable_get(:"@#{method}") == true + end + else + define_method(:"#{method}") do + instance_variable_get(:"@#{method}") + end + end + end + end + + def process_arguments(*arguments) + parser = OptionParser.new do |opts| + next if self.class.options.nil? + + self.class.options.each do |option_name, option_method| + option_type = case option_name.split(/(\ |\=)/).last + when "PATH" + Pathname + when /\w+(,\w+)+/ + Array + end + + opts.on(option_name, *option_type) do |value| + if option_method.respond_to?(:call) + option_method.call(value) + else + send(:"#{option_method}=", value) + end + end + end + end + parser.parse(*arguments) + end + end + end +end diff --git a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb index 5956f59ac..7877ead05 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb @@ -1,20 +1,20 @@ module Hbc class CLI - class Outdated < Base - def self.run(*args) - greedy = args.include?("--greedy") - verbose = ($stdout.tty? || CLI.verbose?) && !args.include?("--quiet") + class Outdated < AbstractCommand + option "--greedy", :greedy, false + option "--quiet", :quiet, false - cask_tokens = cask_tokens_from(args) - casks_to_check = if cask_tokens.empty? - Hbc.installed - else - cask_tokens.map { |token| CaskLoader.load(token) } - end + def initialize(*) + super + self.verbose = ($stdout.tty? || verbose?) && !quiet? + end + + def run + casks_to_check = args.empty? ? Hbc.installed : args.map(&CaskLoader.public_method(:load)) casks_to_check.each do |cask| odebug "Checking update info of Cask #{cask}" - list_if_outdated(cask, greedy, verbose) + self.class.list_if_outdated(cask, greedy?, verbose?) end end diff --git a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb index c2ed8f462..eb5f45c90 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb @@ -1,26 +1,29 @@ module Hbc class CLI class Reinstall < Install - def self.install_casks(cask_tokens, force, skip_cask_deps, require_sha) + def install_casks count = 0 - cask_tokens.each do |cask_token| + args.each do |cask_token| begin cask = CaskLoader.load(cask_token) Installer.new(cask, - force: force, - skip_cask_deps: skip_cask_deps, - require_sha: require_sha).reinstall + binaries: binaries?, + verbose: verbose?, + force: force?, + skip_cask_deps: skip_cask_deps?, + require_sha: require_sha?).reinstall count += 1 rescue CaskUnavailableError => e - warn_unavailable_with_suggestion cask_token, e + self.class.warn_unavailable_with_suggestion cask_token, e rescue CaskNoShasumError => e opoo e.message count += 1 end end - count.zero? ? nil : count == cask_tokens.length + + count.zero? ? nil : count == args.length end def self.help diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index 7abd744e4..b24091aef 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -1,8 +1,13 @@ module Hbc class CLI - class Search < Base - def self.run(*arguments) - render_results(*search(*arguments)) + class Search < AbstractCommand + def initialize(*args) + @args = args + end + + def run + results = self.class.search(*args) + self.class.render_results(*results) end def self.extract_regexp(string) diff --git a/Library/Homebrew/cask/lib/hbc/cli/style.rb b/Library/Homebrew/cask/lib/hbc/cli/style.rb index 191aefd3c..078796e7e 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/style.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/style.rb @@ -2,25 +2,18 @@ require "English" module Hbc class CLI - class Style < Base + class Style < AbstractCommand def self.help "checks Cask style using RuboCop" end - def self.run(*args) - retval = new(args).run - raise CaskError, "style check failed" unless retval - end - - attr_reader :args - def initialize(args) - @args = args - end + option "--fix", :fix, false def run install_rubocop system "rubocop", *rubocop_args, "--", *cask_paths - $CHILD_STATUS.success? + raise CaskError, "style check failed" unless $CHILD_STATUS.success? + true end def install_rubocop @@ -34,19 +27,15 @@ module Hbc end def cask_paths - @cask_paths ||= if cask_tokens.empty? + @cask_paths ||= if args.empty? Hbc.all_tapped_cask_dirs - elsif cask_tokens.any? { |file| File.exist?(file) } - cask_tokens + elsif args.any? { |file| File.exist?(file) } + args else - cask_tokens.map { |token| CaskLoader.path(token) } + args.map { |token| CaskLoader.path(token) } end end - def cask_tokens - @cask_tokens ||= self.class.cask_tokens_from(args) - end - def rubocop_args fix? ? autocorrect_args : default_args end @@ -63,10 +52,6 @@ module Hbc def autocorrect_args default_args + ["--auto-correct"] end - - def fix? - args.any? { |arg| arg =~ /--(fix|(auto-?)?correct)/ } - end end end end diff --git a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb index 1ee3230ad..33ee5afa9 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb @@ -1,23 +1,26 @@ module Hbc class CLI - class Uninstall < Base - def self.run(*args) - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - force = args.include? "--force" + class Uninstall < AbstractCommand + option "--force", :force, false - cask_tokens.each do |cask_token| + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + + def run + args.each do |cask_token| odebug "Uninstalling Cask #{cask_token}" cask = CaskLoader.load(cask_token) - raise CaskNotInstalledError, cask unless cask.installed? || force + raise CaskNotInstalledError, cask unless cask.installed? || force? if cask.installed? && !cask.installed_caskfile.nil? # use the same cask file that was used for installation, if possible cask = CaskLoader.load_from_file(cask.installed_caskfile) if cask.installed_caskfile.exist? end - Installer.new(cask, force: force).uninstall + Installer.new(cask, binaries: binaries?, verbose: verbose?, force: force?).uninstall next if (versions = cask.versions).empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/zap.rb b/Library/Homebrew/cask/lib/hbc/cli/zap.rb index 83da1c932..3c07ff9e8 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/zap.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/zap.rb @@ -1,13 +1,16 @@ module Hbc class CLI - class Zap < Base - def self.run(*args) - cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - cask_tokens.each do |cask_token| + class Zap < AbstractCommand + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + + def run + args.each do |cask_token| odebug "Zapping Cask #{cask_token}" cask = CaskLoader.load(cask_token) - Installer.new(cask).zap + Installer.new(cask, verbose: verbose?).zap end end diff --git a/Library/Homebrew/cask/lib/hbc/installer.rb b/Library/Homebrew/cask/lib/hbc/installer.rb index f02f07806..a783f764b 100644 --- a/Library/Homebrew/cask/lib/hbc/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/installer.rb @@ -14,19 +14,35 @@ module Hbc include Staged include Verify - attr_reader :force, :skip_cask_deps - PERSISTENT_METADATA_SUBDIRS = ["gpg"].freeze - def initialize(cask, command: SystemCommand, force: false, skip_cask_deps: false, require_sha: false) + def initialize(cask, command: SystemCommand, force: false, skip_cask_deps: false, binaries: true, verbose: false, require_sha: false) @cask = cask @command = command @force = force @skip_cask_deps = skip_cask_deps + @binaries = binaries + @verbose = verbose @require_sha = require_sha @reinstall = false end + def skip_cask_deps? + @skip_cask_deps + end + + def force? + @force + end + + def binaries? + @binaries + end + + def verbose? + @verbose + end + def self.print_caveats(cask) odebug "Printing caveats" return if cask.caveats.empty? @@ -59,7 +75,7 @@ module Hbc odebug "Hbc::Installer#fetch" satisfy_dependencies - verify_has_sha if @require_sha && !@force + verify_has_sha if @require_sha && !force? download verify end @@ -77,7 +93,7 @@ module Hbc def install odebug "Hbc::Installer#install" - if @cask.installed? && !force && !@reinstall + if @cask.installed? && !force? && !@reinstall raise CaskAlreadyInstalledAutoUpdatesError, @cask if @cask.auto_updates raise CaskAlreadyInstalledError, @cask end @@ -108,7 +124,7 @@ module Hbc installed_cask = installed_caskfile.exist? ? CaskLoader.load_from_file(installed_caskfile) : @cask # Always force uninstallation, ignore method parameter - Installer.new(installed_cask, force: true).uninstall + Installer.new(installed_cask, binaries: binaries?, verbose: verbose?, force: true).uninstall end def summary @@ -156,12 +172,17 @@ module Hbc already_installed_artifacts = [] odebug "Installing artifacts" - artifacts = Artifact.for_cask(@cask, command: @command, force: force) + artifacts = Artifact.for_cask(@cask, command: @command, verbose: verbose?, force: force?) odebug "#{artifacts.length} artifact/s defined", artifacts artifacts.each do |artifact| next unless artifact.respond_to?(:install_phase) odebug "Installing artifact of class #{artifact.class}" + + if artifact.is_a?(Artifact::Binary) + next unless binaries? + end + artifact.install_phase already_installed_artifacts.unshift(artifact) end @@ -189,7 +210,7 @@ module Hbc arch_dependencies x11_dependencies formula_dependencies - cask_dependencies unless skip_cask_deps + cask_dependencies unless skip_cask_deps? puts "complete" end @@ -254,7 +275,7 @@ module Hbc if dep.installed? puts "already installed" else - Installer.new(dep, force: false, skip_cask_deps: true).install + Installer.new(dep, binaries: binaries?, verbose: verbose?, skip_cask_deps: true, force: false).install puts "done" end end @@ -330,12 +351,12 @@ module Hbc disable_accessibility_access uninstall_artifacts purge_versioned_files - purge_caskroom_path if force + purge_caskroom_path if force? end def uninstall_artifacts odebug "Un-installing artifacts" - artifacts = Artifact.for_cask(@cask, command: @command, force: force) + artifacts = Artifact.for_cask(@cask, command: @command, verbose: verbose?, force: force?) # Make sure the `uninstall` stanza is run first, as it # may depend on other artifacts still being installed. diff --git a/Library/Homebrew/cask/lib/hbc/system_command.rb b/Library/Homebrew/cask/lib/hbc/system_command.rb index c14079bc8..ac2a2346e 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 CLI.debug? + _warn_plist_garbage(command, Regexp.last_match[1]) if ARGV.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 b2b65a08e..4562fc468 100644 --- a/Library/Homebrew/cask/lib/hbc/utils.rb +++ b/Library/Homebrew/cask/lib/hbc/utils.rb @@ -29,7 +29,7 @@ end # global methods def odebug(title, *sput) - return unless Hbc::CLI.debug? + return unless ARGV.debug? puts Formatter.headline(title, color: :magenta) puts sput unless sput.empty? end |
