From 9e821863d0ed97254bbae314b97af18d2f09cdfa Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 19:13:23 +0200 Subject: [PATCH 01/28] Pass along `CLI::Binaries`. --- .../Homebrew/cask/lib/hbc/artifact/binary.rb | 4 --- Library/Homebrew/cask/lib/hbc/cli/install.rb | 8 ++--- .../Homebrew/cask/lib/hbc/cli/reinstall.rb | 1 + .../Homebrew/cask/lib/hbc/cli/uninstall.rb | 2 +- Library/Homebrew/cask/lib/hbc/installer.rb | 16 ++++++++-- .../test/cask/artifact/binary_spec.rb | 30 +++++++++---------- 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/artifact/binary.rb b/Library/Homebrew/cask/lib/hbc/artifact/binary.rb index 21d123ab90..7178c2af69 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/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb index 438f860c1c..5b5ef82c45 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/install.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb @@ -19,10 +19,10 @@ module Hbc cask_tokens.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: CLI.binaries?, + force: force, + skip_cask_deps: skip_cask_deps, + require_sha: require_sha).install count += 1 rescue CaskAlreadyInstalledError => e opoo e.message diff --git a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb index c2ed8f4625..662899d5fe 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb @@ -8,6 +8,7 @@ module Hbc cask = CaskLoader.load(cask_token) Installer.new(cask, + binaries: CLI.binaries?, force: force, skip_cask_deps: skip_cask_deps, require_sha: require_sha).reinstall diff --git a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb index 1ee3230ad8..7fc40daafa 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb @@ -17,7 +17,7 @@ module Hbc cask = CaskLoader.load_from_file(cask.installed_caskfile) if cask.installed_caskfile.exist? end - Installer.new(cask, force: force).uninstall + Installer.new(cask, binaries: CLI.binaries?, force: force).uninstall next if (versions = cask.versions).empty? diff --git a/Library/Homebrew/cask/lib/hbc/installer.rb b/Library/Homebrew/cask/lib/hbc/installer.rb index f02f07806e..d9addd6040 100644 --- a/Library/Homebrew/cask/lib/hbc/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/installer.rb @@ -18,15 +18,20 @@ module Hbc 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, require_sha: false) @cask = cask @command = command @force = force @skip_cask_deps = skip_cask_deps + @binaries = binaries @require_sha = require_sha @reinstall = false end + def binaries? + @binaries + end + def self.print_caveats(cask) odebug "Printing caveats" return if cask.caveats.empty? @@ -108,7 +113,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?, force: true).uninstall end def summary @@ -162,6 +167,11 @@ module Hbc 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 @@ -254,7 +264,7 @@ module Hbc if dep.installed? puts "already installed" else - Installer.new(dep, force: false, skip_cask_deps: true).install + Installer.new(dep, force: false, binaries: binaries?, skip_cask_deps: true).install puts "done" end end diff --git a/Library/Homebrew/test/cask/artifact/binary_spec.rb b/Library/Homebrew/test/cask/artifact/binary_spec.rb index ee62e6439c..f9b5f5b424 100644 --- a/Library/Homebrew/test/cask/artifact/binary_spec.rb +++ b/Library/Homebrew/test/cask/artifact/binary_spec.rb @@ -16,6 +16,20 @@ describe Hbc::Artifact::Binary, :cask do FileUtils.rm expected_path if expected_path.exist? end + context "when --no-binaries is specified" do + let(:cask) { + Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-binary.rb") + } + + it "doesn't link the binary when --no-binaries is specified" do + shutup do + Hbc::Installer.new(cask, binaries: false).install + end + + expect(expected_path).not_to exist + end + end + it "links the binary to the proper directory" do shutup do Hbc::Artifact::Binary.new(cask).install_phase @@ -70,22 +84,6 @@ describe Hbc::Artifact::Binary, :cask do expect(File.readlink(expected_path)).not_to eq("/tmp") end - it "respects --no-binaries flag" do - begin - Hbc::CLI.binaries = false - - 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 - end - it "creates parent directory if it doesn't exist" do FileUtils.rmdir Hbc.binarydir From a44d4ce88b6fb3deea8dc41be662583941d5c96d Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 19:59:26 +0200 Subject: [PATCH 02/28] =?UTF-8?q?Remove=20Cask=E2=80=99s=20`CLI#debug=3F`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Homebrew/cask/lib/hbc/cask.rb | 2 -- Library/Homebrew/cask/lib/hbc/cli.rb | 4 +--- Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb | 1 - Library/Homebrew/cask/lib/hbc/system_command.rb | 2 +- Library/Homebrew/cask/lib/hbc/utils.rb | 2 +- Library/Homebrew/test/cask/cli/options_spec.rb | 11 ----------- 6 files changed, 3 insertions(+), 19 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cask.rb b/Library/Homebrew/cask/lib/hbc/cask.rb index a193a394e9..47f3b176ae 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 37784f4c3c..d0c4f30df8 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -71,7 +71,6 @@ module Hbc FLAGS = { ["--[no-]binaries", :binaries] => true, - ["--debug", :debug] => false, ["--verbose", :verbose] => false, ["--outdated", :outdated] => false, ["--help", :help] => false, @@ -158,7 +157,7 @@ module Hbc run_command(command, *rest) rescue CaskError, CaskSha256MismatchError, ArgumentError => 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 @@ -239,7 +238,6 @@ module Hbc # for compat with Homebrew, not certain if this is desirable self.verbose = true if ARGV.verbose? - self.debug = true if ARGV.debug? remaining end diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb index 8017a32cf7..94dd309bdb 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb @@ -12,7 +12,6 @@ module Hbc end def self.dump_casks(*cask_tokens) - 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/system_command.rb b/Library/Homebrew/cask/lib/hbc/system_command.rb index c14079bc8b..ac2a2346ec 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 b2b65a08e0..4562fc4689 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 diff --git a/Library/Homebrew/test/cask/cli/options_spec.rb b/Library/Homebrew/test/cask/cli/options_spec.rb index 44a391b489..eb0bf1a49d 100644 --- a/Library/Homebrew/test/cask/cli/options_spec.rb +++ b/Library/Homebrew/test/cask/cli/options_spec.rb @@ -120,17 +120,6 @@ describe Hbc::CLI, :cask do end end - describe "--debug" do - it "sets the Cask debug method to true" do - 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 begin From 1714c73b497b44358fa9fc17d05c767996ac6b7f Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 21:07:25 +0200 Subject: [PATCH 03/28] Refactor `CLI::Audit`. --- Library/Homebrew/cask/lib/hbc/cli/audit.rb | 16 +++++---- Library/Homebrew/test/cask/cli/audit_spec.rb | 38 ++++++++++++-------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/audit.rb b/Library/Homebrew/cask/lib/hbc/cli/audit.rb index ec1c33754e..5da2be4cfb 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/audit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/audit.rb @@ -6,20 +6,24 @@ module Hbc 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(" ")}" + new(*args).run end - def initialize(args, auditor) + def initialize(*args, auditor: Auditor) @args = args @auditor = auditor end def run - casks_to_audit.each_with_object([]) do |cask, failed| - failed << cask unless audit(cask) + failed_casks = [] + + casks_to_audit.each do |cask| + next if audit(cask) + failed_casks << cask end + + return if failed_casks.empty? + raise CaskError, "audit failed for casks: #{failed_casks.join(" ")}" end def audit(cask) diff --git a/Library/Homebrew/test/cask/cli/audit_spec.rb b/Library/Homebrew/test/cask/cli/audit_spec.rb index 007ba1eb3e..312a267f8e 100644 --- a/Library/Homebrew/test/cask/cli/audit_spec.rb +++ b/Library/Homebrew/test/cask/cli/audit_spec.rb @@ -6,54 +6,64 @@ describe Hbc::CLI::Audit, :cask do it "audits all Casks if no tokens are given" do allow(Hbc).to receive(:all).and_return([cask, cask]) - expect(auditor).to receive(:audit).twice + expect(auditor).to receive(:audit).twice.and_return(true) - run_audit([], auditor) + run_audit end it "audits specified Casks if tokens are given" do cask_token = "nice-app" expect(Hbc::CaskLoader).to receive(:load).with(cask_token).and_return(cask) - expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false) + expect(auditor).to receive(:audit) + .with(cask, audit_download: false, check_token_conflicts: false) + .and_return(true) - run_audit([cask_token], auditor) + run_audit(cask_token) end end describe "rules for downloading a Cask" do it "does not download the Cask per default" do allow(Hbc::CaskLoader).to receive(:load).and_return(cask) - expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false) + expect(auditor).to receive(:audit) + .with(cask, audit_download: false, check_token_conflicts: false) + .and_return(true) - run_audit(["casktoken"], auditor) + run_audit("casktoken") end it "download a Cask if --download flag is set" do allow(Hbc::CaskLoader).to receive(:load).and_return(cask) - expect(auditor).to receive(:audit).with(cask, audit_download: true, check_token_conflicts: false) + expect(auditor).to receive(:audit) + .with(cask, audit_download: true, check_token_conflicts: false) + .and_return(true) - run_audit(["casktoken", "--download"], auditor) + run_audit("casktoken", "--download") end end describe "rules for checking token conflicts" do it "does not check for token conflicts per default" do allow(Hbc::CaskLoader).to receive(:load).and_return(cask) - expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: false) + expect(auditor).to receive(:audit) + .with(cask, audit_download: false, check_token_conflicts: false) + .and_return(true) - run_audit(["casktoken"], auditor) + run_audit("casktoken") end it "checks for token conflicts if --token-conflicts flag is set" do allow(Hbc::CaskLoader).to receive(:load).and_return(cask) - expect(auditor).to receive(:audit).with(cask, audit_download: false, check_token_conflicts: true) + expect(auditor).to receive(:audit) + .with(cask, audit_download: false, check_token_conflicts: true) + .and_return(true) - run_audit(["casktoken", "--token-conflicts"], auditor) + run_audit("casktoken", "--token-conflicts") end end - def run_audit(args, auditor) - Hbc::CLI::Audit.new(args, auditor).run + def run_audit(*args) + Hbc::CLI::Audit.new(*args, auditor: auditor).run end end From 58db95c1d23b8269dffd654afb0b8b12b46a51ce Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 21:13:08 +0200 Subject: [PATCH 04/28] Refactor `CLI::Cat`. --- Library/Homebrew/cask/lib/hbc/cli/base.rb | 4 ++++ Library/Homebrew/cask/lib/hbc/cli/cat.rb | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/base.rb b/Library/Homebrew/cask/lib/hbc/cli/base.rb index 3301cad91c..1ad347de3f 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/base.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/base.rb @@ -20,6 +20,10 @@ module Hbc def self.needs_init? false end + + def initialize(*args) + @args = args + 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 52f6e0eabe..774b96e89c 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cat.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cat.rb @@ -2,7 +2,11 @@ module Hbc class CLI class Cat < Base def self.run(*args) - cask_tokens = cask_tokens_from(args) + new(*args).run + end + + def run + cask_tokens = self.class.cask_tokens_from(@args) raise CaskUnspecifiedError if cask_tokens.empty? # only respects the first argument cask_token = cask_tokens.first.sub(/\.rb$/i, "") From b7347dcc445fe1b3a61a79152c09c0d52fc4ea28 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 20:27:25 +0200 Subject: [PATCH 05/28] Refactor `CLI::Cleanup`. --- Library/Homebrew/cask/lib/hbc/cli/cleanup.rb | 22 ++--- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 2 +- .../Homebrew/test/cask/cli/cleanup_spec.rb | 90 ++++++++----------- 3 files changed, 43 insertions(+), 71 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb index 9ebccabd02..a62b8d069a 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb @@ -13,29 +13,19 @@ module Hbc end def self.run(*args) - if args.empty? - default.cleanup! - else - default.cleanup(args) - end - end - - def self.default - @default ||= new(Hbc.cache, CLI.outdated?) + new(*args).run end attr_reader :cache_location, :outdated_only - def initialize(cache_location, outdated_only) + + def initialize(*args, cache_location: Hbc.cache, outdated_only: CLI.outdated?) + @args = 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 diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 031f788247..11917ae6b7 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -107,7 +107,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/test/cask/cli/cleanup_spec.rb b/Library/Homebrew/test/cask/cli/cleanup_spec.rb index c7ef356c04..037fa1bd40 100644 --- a/Library/Homebrew/test/cask/cli/cleanup_spec.rb +++ b/Library/Homebrew/test/cask/cli/cleanup_spec.rb @@ -1,19 +1,20 @@ describe Hbc::CLI::Cleanup, :cask do let(:cache_location) { Pathname.new(Dir.mktmpdir).realpath } - let(:cleanup_outdated) { false } + let(:outdated_only) { false } - subject { described_class.new(cache_location, cleanup_outdated) } + subject { described_class.new(*cask_tokens, cache_location: cache_location, outdated_only: outdated_only) } after do cache_location.rmtree end describe "cleanup" do - it "removes cached downloads of given casks" do - cleaned_up_cached_download = "caffeine" + let(:cask_token) { "caffeine" } + let(:cask_tokens) { [cask_token] } + it "removes cached downloads of given casks" do cached_downloads = [ - cache_location.join("#{cleaned_up_cached_download}--latest.zip"), + cache_location.join("#{cask_token}--latest.zip"), cache_location.join("transmission--2.61.dmg"), ] @@ -22,9 +23,9 @@ describe Hbc::CLI::Cleanup, :cask do cleanup_size = cached_downloads[0].disk_usage expect { - subject.cleanup(cleaned_up_cached_download) + subject.run }.to output(<<-EOS.undent).to_stdout - ==> Removing cached downloads for #{cleaned_up_cached_download} + ==> Removing cached downloads for #{cask_token} #{cached_downloads[0]} ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space. EOS @@ -32,61 +33,42 @@ describe Hbc::CLI::Cleanup, :cask do expect(cached_downloads[0].exist?).to eq(false) expect(cached_downloads[1].exist?).to eq(true) end - end - describe "cleanup!" do - it "removes cached downloads" do - cached_download = cache_location.join("SomeDownload.dmg") - FileUtils.touch(cached_download) - cleanup_size = subject.disk_cleanup_size + context "when no argument is given" do + let(:cask_tokens) { [] } - expect { - subject.cleanup! - }.to output(<<-EOS.undent).to_stdout - ==> Removing cached downloads - #{cached_download} - ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space. - EOS - - expect(cached_download.exist?).to eq(false) - end - - # TODO: uncomment when unflaky. - # it "does not removed locked files" do - # cached_download = cache_location.join("SomeDownload.dmg") - # FileUtils.touch(cached_download) - # cleanup_size = subject.disk_cleanup_size - # - # File.new(cached_download).flock(File::LOCK_EX) - # - # expect(Hbc::Utils).to be_file_locked(cached_download) - # - # expect { - # subject.cleanup! - # }.to output(<<-EOS.undent).to_stdout - # ==> Removing cached downloads - # skipping: #{cached_download} is locked - # ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space. - # EOS - # - # expect(cached_download.exist?).to eq(true) - # end - - context "when cleanup_outdated is specified" do - let(:cleanup_outdated) { true } - - it "does not remove cache files newer than 10 days old" do - cached_download = cache_location.join("SomeNewDownload.dmg") + it "removes all cached downloads" do + cached_download = cache_location.join("SomeDownload.dmg") FileUtils.touch(cached_download) + cleanup_size = subject.disk_cleanup_size expect { - subject.cleanup! + subject.run }.to output(<<-EOS.undent).to_stdout - ==> Removing cached downloads older than 10 days old - Nothing to do + ==> Removing cached downloads + #{cached_download} + ==> This operation has freed approximately #{disk_usage_readable(cleanup_size)} of disk space. EOS - expect(cached_download.exist?).to eq(true) + expect(cached_download.exist?).to eq(false) + end + + context "and :outdated_only is specified" do + let(:outdated_only) { true } + + it "does not remove cache files newer than 10 days old" do + cached_download = cache_location.join("SomeNewDownload.dmg") + FileUtils.touch(cached_download) + + expect { + subject.run + }.to output(<<-EOS.undent).to_stdout + ==> Removing cached downloads older than 10 days old + Nothing to do + EOS + + expect(cached_download.exist?).to eq(true) + end end end end From 8248345a9a735aa9ee0421413166e9733bff277d Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 21:20:51 +0200 Subject: [PATCH 06/28] Refactor `CLI::Create`. --- Library/Homebrew/cask/lib/hbc/cli/create.rb | 8 ++- Library/Homebrew/test/cask/cli/create_spec.rb | 72 +++++++------------ 2 files changed, 32 insertions(+), 48 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/create.rb b/Library/Homebrew/cask/lib/hbc/cli/create.rb index 5e143d0859..39b9bcda78 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/create.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/create.rb @@ -2,7 +2,11 @@ module Hbc class CLI class Create < Base def self.run(*args) - cask_tokens = cask_tokens_from(args) + new(*args).run + end + + def run + cask_tokens = self.class.cask_tokens_from(@args) raise CaskUnspecifiedError if cask_tokens.empty? cask_token = cask_tokens.first.sub(/\.rb$/i, "") cask_path = CaskLoader.path(cask_token) @@ -11,7 +15,7 @@ module Hbc raise CaskAlreadyCreatedError, cask_token if cask_path.exist? 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/test/cask/cli/create_spec.rb b/Library/Homebrew/test/cask/cli/create_spec.rb index b1cee69908..ef6944c327 100644 --- a/Library/Homebrew/test/cask/cli/create_spec.rb +++ b/Library/Homebrew/test/cask/cli/create_spec.rb @@ -1,43 +1,26 @@ -# monkeypatch for testing -module Hbc - class CLI - class Create - def self.exec_editor(*command) - editor_commands << command - end - - def self.reset! - @editor_commands = [] - end - - def self.editor_commands - @editor_commands ||= [] - end - end - end -end - describe Hbc::CLI::Create, :cask do - before(:each) do - Hbc::CLI::Create.reset! + around(:each) do |example| + begin + example.run + ensure + %w[new-cask additional-cask another-cask yet-another-cask local-caff].each do |cask| + FileUtils.rm_f Hbc::CaskLoader.path(cask) + end + end end - after(:each) do - %w[new-cask additional-cask another-cask yet-another-cask local-caff].each do |cask| - path = Hbc::CaskLoader.path(cask) - path.delete if path.exist? - end + before(:each) do + allow_any_instance_of(described_class).to receive(:exec_editor) end it "opens the editor for the specified Cask" do - Hbc::CLI::Create.run("new-cask") - expect(Hbc::CLI::Create.editor_commands).to eq [ - [Hbc::CaskLoader.path("new-cask")], - ] + command = described_class.new("new-cask") + expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("new-cask")) + command.run end it "drops a template down for the specified Cask" do - Hbc::CLI::Create.run("new-cask") + described_class.run("new-cask") template = File.read(Hbc::CaskLoader.path("new-cask")) expect(template).to eq <<-EOS.undent cask 'new-cask' do @@ -54,36 +37,33 @@ describe Hbc::CLI::Create, :cask do end it "throws away additional Cask arguments and uses the first" do - Hbc::CLI::Create.run("additional-cask", "another-cask") - expect(Hbc::CLI::Create.editor_commands).to eq [ - [Hbc::CaskLoader.path("additional-cask")], - ] + command = described_class.new("additional-cask", "another-cask") + expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("additional-cask")) + command.run end it "throws away stray options" do - Hbc::CLI::Create.run("--notavalidoption", "yet-another-cask") - expect(Hbc::CLI::Create.editor_commands).to eq [ - [Hbc::CaskLoader.path("yet-another-cask")], - ] + command = described_class.new("--notavalidoption", "yet-another-cask") + expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("yet-another-cask")) + command.run end it "raises an exception when the Cask already exists" do expect { - Hbc::CLI::Create.run("basic-cask") + described_class.run("basic-cask") }.to raise_error(Hbc::CaskAlreadyCreatedError) end it "allows creating Casks that are substrings of existing Casks" do - Hbc::CLI::Create.run("local-caff") - expect(Hbc::CLI::Create.editor_commands).to eq [ - [Hbc::CaskLoader.path("local-caff")], - ] + command = described_class.new("local-caff") + expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("local-caff")) + command.run end describe "when no Cask is specified" do it "raises an exception" do expect { - Hbc::CLI::Create.run + described_class.run }.to raise_error(Hbc::CaskUnspecifiedError) end end @@ -91,7 +71,7 @@ describe Hbc::CLI::Create, :cask do describe "when no Cask is specified, but an invalid option" do it "raises an exception" do expect { - Hbc::CLI::Create.run("--notavalidoption") + described_class.run("--notavalidoption") }.to raise_error(Hbc::CaskUnspecifiedError) end end From 276adc9e8b0071e6c61160b93e7bd399fe4f29e4 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 22:01:50 +0200 Subject: [PATCH 07/28] Refactor `CLI::Edit`. --- Library/Homebrew/cask/lib/hbc/cli/edit.rb | 6 ++- Library/Homebrew/test/cask/cli/edit_spec.rb | 41 +++++---------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/edit.rb b/Library/Homebrew/cask/lib/hbc/cli/edit.rb index 1f1e0d9181..52b089a8bc 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/edit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/edit.rb @@ -2,7 +2,11 @@ module Hbc class CLI class Edit < Base def self.run(*args) - cask_tokens = cask_tokens_from(args) + new(*args).run + end + + def run + cask_tokens = self.class.cask_tokens_from(@args) raise CaskUnspecifiedError if cask_tokens.empty? # only respects the first argument cask_token = cask_tokens.first.sub(/\.rb$/i, "") diff --git a/Library/Homebrew/test/cask/cli/edit_spec.rb b/Library/Homebrew/test/cask/cli/edit_spec.rb index f5f98afc87..a097f08940 100644 --- a/Library/Homebrew/test/cask/cli/edit_spec.rb +++ b/Library/Homebrew/test/cask/cli/edit_spec.rb @@ -1,51 +1,30 @@ -# monkeypatch for testing -module Hbc - class CLI - class Edit - def self.exec_editor(*command) - editor_commands << command - end - - def self.reset! - @editor_commands = [] - end - - def self.editor_commands - @editor_commands ||= [] - end - end - end -end - describe Hbc::CLI::Edit, :cask do before(:each) do - Hbc::CLI::Edit.reset! + allow_any_instance_of(described_class).to receive(:exec_editor) end it "opens the editor for the specified Cask" do - Hbc::CLI::Edit.run("local-caffeine") - expect(Hbc::CLI::Edit.editor_commands).to eq [ - [Hbc::CaskLoader.path("local-caffeine")], - ] + command = described_class.new("local-caffeine") + expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("local-caffeine")) + command.run end it "throws away additional arguments and uses the first" do - Hbc::CLI::Edit.run("local-caffeine", "local-transmission") - expect(Hbc::CLI::Edit.editor_commands).to eq [ - [Hbc::CaskLoader.path("local-caffeine")], - ] + command = described_class.new("local-caffeine", "local-transmission") + expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("local-caffeine")) + command.run end it "raises an exception when the Cask doesnt exist" do expect { - Hbc::CLI::Edit.run("notacask") + described_class.run("notacask") }.to raise_error(Hbc::CaskUnavailableError) end describe "when no Cask is specified" do it "raises an exception" do expect { - Hbc::CLI::Edit.run + described_class.run }.to raise_error(Hbc::CaskUnspecifiedError) end end @@ -53,7 +32,7 @@ describe Hbc::CLI::Edit, :cask do describe "when no Cask is specified, but an invalid option" do it "raises an exception" do expect { - Hbc::CLI::Edit.run("--notavalidoption") + described_class.run("--notavalidoption") }.to raise_error(Hbc::CaskUnspecifiedError) end end From 1f5828c72de87ce87df31fad1c1046a6bcf4a994 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 21:55:41 +0200 Subject: [PATCH 08/28] Refactor `CLI::Fetch`. --- Library/Homebrew/cask/lib/hbc/cli/fetch.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb index 83dba672c2..f7139ff639 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb @@ -2,9 +2,13 @@ module Hbc class CLI class Fetch < Base def self.run(*args) - cask_tokens = cask_tokens_from(args) + new(*args).run + end + + def run + cask_tokens = self.class.cask_tokens_from(@args) raise CaskUnspecifiedError if cask_tokens.empty? - force = args.include? "--force" + force = @args.include? "--force" cask_tokens.each do |cask_token| ohai "Downloading external files for Cask #{cask_token}" From 66e9a060dea952a23bb6f9b40d88f2571254be6a Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 21:48:21 +0200 Subject: [PATCH 09/28] Refactor `CLI::Home`. --- Library/Homebrew/cask/lib/hbc/cli/home.rb | 23 ++++++++---- Library/Homebrew/test/cask/cli/home_spec.rb | 41 ++++----------------- 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/home.rb b/Library/Homebrew/cask/lib/hbc/cli/home.rb index 66be49186c..882cb67af7 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/home.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/home.rb @@ -1,19 +1,28 @@ module Hbc class CLI class Home < Base - def self.run(*cask_tokens) - if cask_tokens.empty? + def self.run(*args) + new(*args).run + end + + 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/test/cask/cli/home_spec.rb b/Library/Homebrew/test/cask/cli/home_spec.rb index 7be26dd4c2..e985fb6cd8 100644 --- a/Library/Homebrew/test/cask/cli/home_spec.rb +++ b/Library/Homebrew/test/cask/cli/home_spec.rb @@ -1,46 +1,21 @@ -# monkeypatch for testing -module Hbc - class CLI - class Home - def self.system(*command) - system_commands << command - end - - def self.reset! - @system_commands = [] - end - - def self.system_commands - @system_commands ||= [] - end - end - end -end - describe Hbc::CLI::Home, :cask do before do - Hbc::CLI::Home.reset! + allow(described_class).to receive(:open_url) end it "opens the homepage for the specified Cask" do - Hbc::CLI::Home.run("local-caffeine") - expect(Hbc::CLI::Home.system_commands).to eq [ - ["/usr/bin/open", "--", "http://example.com/local-caffeine"], - ] + expect(described_class).to receive(:open_url).with("http://example.com/local-caffeine") + described_class.run("local-caffeine") end it "works for multiple Casks" do - Hbc::CLI::Home.run("local-caffeine", "local-transmission") - expect(Hbc::CLI::Home.system_commands).to eq [ - ["/usr/bin/open", "--", "http://example.com/local-caffeine"], - ["/usr/bin/open", "--", "http://example.com/local-transmission"], - ] + expect(described_class).to receive(:open_url).with("http://example.com/local-caffeine") + expect(described_class).to receive(:open_url).with("http://example.com/local-transmission") + described_class.run("local-caffeine", "local-transmission") end it "opens the project page when no Cask is specified" do - Hbc::CLI::Home.run - expect(Hbc::CLI::Home.system_commands).to eq [ - ["/usr/bin/open", "--", "https://caskroom.github.io/"], - ] + expect(described_class).to receive(:open_url).with("https://caskroom.github.io/") + described_class.run end end From e17641bdd9d976a45e95501167a0aa76bf521cbc Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 18:31:07 +0200 Subject: [PATCH 10/28] Refactor `CLI::Info`. --- Library/Homebrew/cask/lib/hbc/cli/info.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/info.rb b/Library/Homebrew/cask/lib/hbc/cli/info.rb index 625b4ecae3..327290eea8 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/info.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/info.rb @@ -2,13 +2,20 @@ 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| + new(*args).run + end + + def initialize(*args) + @cask_tokens = self.class.cask_tokens_from(args) + raise CaskUnspecifiedError if @cask_tokens.empty? + end + + def run + @cask_tokens.each do |cask_token| odebug "Getting info for Cask #{cask_token}" cask = CaskLoader.load(cask_token) - info(cask) + self.class.info(cask) end end From 33580c283a5122424edbfa6a1f70e6c11bf29b36 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 02:27:13 +0200 Subject: [PATCH 11/28] Refactor `CLI::Install`. --- Library/Homebrew/cask/lib/hbc/cli/install.rb | 34 ++++++++++++------- .../Homebrew/cask/lib/hbc/cli/reinstall.rb | 15 ++++---- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb index 5b5ef82c45..53c5107744 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/install.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb @@ -2,27 +2,34 @@ 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 + new(*args).run + end + + def initialize(*args) + @cask_tokens = self.class.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" + 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| + @cask_tokens.each do |cask_token| begin cask = CaskLoader.load(cask_token) Installer.new(cask, binaries: CLI.binaries?, - force: force, - skip_cask_deps: skip_cask_deps, - require_sha: require_sha).install + force: @force, + skip_cask_deps: @skip_cask_deps, + require_sha: @require_sha).install count += 1 rescue CaskAlreadyInstalledError => e opoo e.message @@ -31,7 +38,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 +46,8 @@ module Hbc onoe e.message end end - count.zero? ? nil : count == cask_tokens.length + + count.zero? ? nil : count == @cask_tokens.length end def self.warn_unavailable_with_suggestion(cask_token, e) diff --git a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb index 662899d5fe..b675a79fa5 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb @@ -1,27 +1,28 @@ 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| + @cask_tokens.each do |cask_token| begin cask = CaskLoader.load(cask_token) Installer.new(cask, binaries: CLI.binaries?, - force: force, - skip_cask_deps: skip_cask_deps, - require_sha: require_sha).reinstall + 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 == @cask_tokens.length end def self.help From 326c425dc6c3160e515749676f7fe541da0da091 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 02:43:45 +0200 Subject: [PATCH 12/28] Refactor `CLI::InternalAppcastCheckpoint`. --- .../lib/hbc/cli/internal_appcast_checkpoint.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) 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 da35671086..89eecd8db6 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb @@ -2,14 +2,20 @@ 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? + new(*args).run + end - if cask_tokens.all? { |t| t =~ %r{^https?://} && t !~ /\.rb$/ } - appcask_checkpoint_for_url(cask_tokens) + def initialize(*args) + @cask_tokens = cask_tokens_from(args) + raise CaskUnspecifiedError if cask_tokens.empty? + @calculate = args.include? "--calculate" + end + + def run + if @cask_tokens.all? { |t| t =~ %r{^https?://} && t !~ /\.rb$/ } + self.class.appcask_checkpoint_for_url(cask_tokens) else - appcask_checkpoint(cask_tokens, calculate) + self.class.appcask_checkpoint(@cask_tokens, @calculate) end end From 6acca4e09a2182f1bb5eb09db0b73afc515227d6 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 02:52:18 +0200 Subject: [PATCH 13/28] Refactor `CLI::InternalAuditModifiedCasks`. --- .../lib/hbc/cli/internal_audit_modified_casks.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) 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 1a8ca0e985..371e195709 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 @@ -8,9 +8,12 @@ module Hbc end def self.run(*args) - commit_range = commit_range(args) - cleanup = args.any? { |a| a =~ /^-+c(leanup)?$/i } - new(commit_range, cleanup: cleanup).run + new(*args).run + end + + def initialize(*args) + @commit_range = self.class.commit_range(args) + @cleanup = args.any? { |a| a =~ /^-+c(leanup)?$/i } end def self.commit_range(args) @@ -41,11 +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? From cc18d9e2b6089165d9ed1a017c08ac3773684e8e Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 18:27:12 +0200 Subject: [PATCH 14/28] Refactor `CLI::InternalCheckurl`. --- Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb index b7d95957d8..3cd7add294 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb @@ -2,7 +2,11 @@ module Hbc class CLI class InternalCheckurl < InternalUseBase def self.run(*args) - casks_to_check = args.empty? ? Hbc.all : args.map { |arg| CaskLoader.load(arg) } + new(*args).run + end + + def run + casks_to_check = @args.empty? ? Hbc.all : @args.map { |arg| CaskLoader.load(arg) } casks_to_check.each do |cask| odebug "Checking URL for Cask #{cask}" checker = UrlChecker.new(cask) From cac0c29f732f2de3362b353b8f86e345469397ff Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 02:57:37 +0200 Subject: [PATCH 15/28] Refactor `CLI::InternalDump`. --- .../cask/lib/hbc/cli/internal_dump.rb | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb index 94dd309bdb..e6dc3510ff 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb @@ -1,19 +1,26 @@ 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) + def self.run(*args) + new(*args).run + end + + def initialize(*args) + @cask_tokens = self.class.cask_tokens_from(args) + raise CaskUnspecifiedError if @cask_tokens.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) + def dump_casks count = 0 - cask_tokens.each do |cask_token| + @cask_tokens.each do |cask_token| begin cask = CaskLoader.load(cask_token) count += 1 @@ -22,7 +29,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 == @cask_tokens.length end def self.help From ed6823e659b54b8089a8b3aaf381025e0ed44761 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 03:07:37 +0200 Subject: [PATCH 16/28] Refactor `CLI::InternalHelp`. --- Library/Homebrew/cask/lib/hbc/cli/internal_help.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb index 0908ee05ef..3903f3b080 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb @@ -1,12 +1,16 @@ module Hbc class CLI class InternalHelp < InternalUseBase - def self.run(*_ignored) + def self.run(*args) + new(*args).run + 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 From ccafa1b75984f32e3e1fe5f860b561ada8876f96 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 03:19:41 +0200 Subject: [PATCH 17/28] Refactor `CLI::InternalStanza`. --- .../cask/lib/hbc/cli/internal_stanza.rb | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb index 303aa7ffe7..a60a08d66c 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb @@ -51,70 +51,76 @@ module Hbc ] def self.run(*args) + new(*args).run + end + + def initialize(*args) 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? + @table = args.include? "--table" + @quiet = args.include? "--quiet" + @format = :to_yaml if args.include? "--yaml" + @format = :inspect if args.include? "--inspect" + @cask_tokens = self.class.cask_tokens_from(args) + @stanza = @cask_tokens.shift.to_sym + @cask_tokens = Hbc.all_tokens if @cask_tokens.empty? + end - retval = print_stanzas(stanza, format, table, quiet, *cask_tokens) + 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 + if ARTIFACTS.include?(@stanza) + artifact_name = @stanza + @stanza = :artifacts end - cask_tokens.each do |cask_token| - print "#{cask_token}\t" if table + @cask_tokens.each do |cask_token| + 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 + unless cask.respond_to?(@stanza) + 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 @@ -123,7 +129,7 @@ module Hbc count += 1 end - count.zero? ? nil : count == cask_tokens.length + count.zero? ? nil : count == @cask_tokens.length end def self.help From 24f38a2e8ad3d6d7f39030ee901dcc496a684a5e Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 03:30:46 +0200 Subject: [PATCH 18/28] Refactor `CLI::List`. --- Library/Homebrew/cask/lib/hbc/cli/list.rb | 47 +++++++++++++---------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/list.rb b/Library/Homebrew/cask/lib/hbc/cli/list.rb index d9bf2187b1..f79dc9a54b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/list.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/list.rb @@ -1,19 +1,24 @@ 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") + def self.run(*args) + new(*args).run + end - if arguments.delete("-l") - @options[:one] = true - opoo "Option -l is obsolete! Implying option -1." - end + def initialize(*args) + @cask_tokens = self.class.cask_tokens_from(args) + @one = true if args.delete("-1") + @versions = true if args.delete("--versions") - retval = arguments.any? ? list(*arguments) : list_installed + return unless args.delete("-l") + @one = true + opoo "Option -l is obsolete! Implying option -1." + end + + def run + retval = @cask_tokens.any? ? list : list_installed # retval is ternary: true/false/nil - if retval.nil? && !arguments.any? + if retval.nil? && !@cask_tokens.any? opoo "nothing to list" # special case: avoid exit code elsif retval.nil? raise CaskError, "nothing to list" @@ -22,22 +27,22 @@ module Hbc end end - def self.list(*cask_tokens) + def list count = 0 - cask_tokens.each do |cask_token| + @cask_tokens.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 +54,7 @@ module Hbc end end - count.zero? ? nil : count == cask_tokens.length + count.zero? ? nil : count == @cask_tokens.length end def self.list_artifacts(cask) @@ -59,13 +64,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 From ed6934b95414e69a80ccb430c46e551cf97a30fc Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 03:38:51 +0200 Subject: [PATCH 19/28] Refactor `CLI::Outdated`. --- Library/Homebrew/cask/lib/hbc/cli/outdated.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb index 5956f59ac1..8eb387df2d 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb @@ -2,19 +2,26 @@ module Hbc class CLI class Outdated < Base def self.run(*args) - greedy = args.include?("--greedy") - verbose = ($stdout.tty? || CLI.verbose?) && !args.include?("--quiet") + new(*args).run + end - cask_tokens = cask_tokens_from(args) - casks_to_check = if cask_tokens.empty? + def initialize(*args) + @cask_tokens = self.class.cask_tokens_from(args) + + @greedy = args.include?("--greedy") + @verbose = ($stdout.tty? || CLI.verbose?) && !args.include?("--quiet") + end + + def run + casks_to_check = if @cask_tokens.empty? Hbc.installed else - cask_tokens.map { |token| CaskLoader.load(token) } + @cask_tokens.map(&CaskLoader.public_method(:load)) end 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 From 957c5fb4f0f690ed82b8a6a38ec106f4784a9ed9 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 03:44:21 +0200 Subject: [PATCH 20/28] Refactor `CLI::Search`. --- Library/Homebrew/cask/lib/hbc/cli/search.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index 7abd744e43..1080538548 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -1,8 +1,12 @@ module Hbc class CLI class Search < Base - def self.run(*arguments) - render_results(*search(*arguments)) + def self.run(*args) + new(*args).run + end + + def run + self.class.render_results(*self.class.search(*@args)) end def self.extract_regexp(string) From 98f91fb88331ee1bc5f190fec827048a38bf8498 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 03:46:52 +0200 Subject: [PATCH 21/28] Refactor `CLI::Style`. --- Library/Homebrew/cask/lib/hbc/cli/style.rb | 31 ++++----- Library/Homebrew/test/cask/cli/style_spec.rb | 68 ++------------------ 2 files changed, 20 insertions(+), 79 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/style.rb b/Library/Homebrew/cask/lib/hbc/cli/style.rb index 191aefd3c6..a76c893fce 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/style.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/style.rb @@ -8,19 +8,24 @@ module Hbc end def self.run(*args) - retval = new(args).run - raise CaskError, "style check failed" unless retval + new(*args).run end attr_reader :args - def initialize(args) - @args = args + def initialize(*args) + @cask_tokens = self.class.cask_tokens_from(args) + @fix = args.any? { |arg| arg =~ /^--(fix|(auto-?)?correct)$/ } + end + + def fix? + @fix end 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 +39,15 @@ module Hbc end def cask_paths - @cask_paths ||= if cask_tokens.empty? + @cask_paths ||= if @cask_tokens.empty? Hbc.all_tapped_cask_dirs - elsif cask_tokens.any? { |file| File.exist?(file) } - cask_tokens + elsif @cask_tokens.any? { |file| File.exist?(file) } + @cask_tokens else - cask_tokens.map { |token| CaskLoader.path(token) } + @cask_tokens.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 +64,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/test/cask/cli/style_spec.rb b/Library/Homebrew/test/cask/cli/style_spec.rb index d41636beb4..1302c8352f 100644 --- a/Library/Homebrew/test/cask/cli/style_spec.rb +++ b/Library/Homebrew/test/cask/cli/style_spec.rb @@ -4,37 +4,12 @@ require "rubygems" describe Hbc::CLI::Style, :cask do let(:args) { [] } - let(:cli) { described_class.new(args) } + let(:cli) { described_class.new(*args) } around do |example| shutup { example.run } end - describe ".run" do - subject { described_class.run(args) } - - before do - allow(described_class).to receive(:new).and_return(cli) - allow(cli).to receive(:run).and_return(retval) - end - - context "when rubocop succeeds" do - let(:retval) { true } - - it "exits successfully" do - subject - end - end - - context "when rubocop fails" do - let(:retval) { false } - - it "raises an exception" do - expect { subject }.to raise_error(Hbc::CaskError) - end - end - end - describe "#run" do subject { cli.run } @@ -53,7 +28,10 @@ describe Hbc::CLI::Style, :cask do context "when rubocop fails" do let(:success) { false } - it { is_expected.to be_falsey } + + it "raises an error" do + expect { subject }.to raise_error(Hbc::CaskError) + end end end @@ -99,7 +77,7 @@ describe Hbc::CLI::Style, :cask do subject { cli.cask_paths } before do - allow(cli).to receive(:cask_tokens).and_return(tokens) + allow(described_class).to receive(:cask_tokens_from).and_return(tokens) end context "when no cask tokens are given" do @@ -136,40 +114,6 @@ describe Hbc::CLI::Style, :cask do end end - describe "#cask_tokens" do - subject { cli.cask_tokens } - - context "when no args are given" do - let(:args) { [] } - it { is_expected.to be_empty } - end - - context "when only flags are given" do - let(:args) { ["--fix"] } - it { is_expected.to be_empty } - end - - context "when only empty args are given" do - let(:args) { ["", ""] } - it { is_expected.to be_empty } - end - - context "when a cask token is given" do - let(:args) { ["adium"] } - it { is_expected.to eq(["adium"]) } - end - - context "when multiple cask tokens are given" do - let(:args) { %w[adium dropbox] } - it { is_expected.to eq(%w[adium dropbox]) } - end - - context "when cask tokens are given with flags" do - let(:args) { ["adium", "dropbox", "--fix"] } - it { is_expected.to eq(%w[adium dropbox]) } - end - end - describe "#rubocop_args" do subject { cli.rubocop_args } From 101371207f8c6d4bd4da308f96c320232ba9c584 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 03:57:38 +0200 Subject: [PATCH 22/28] Refactor `CLI::Uninstall`. --- Library/Homebrew/cask/lib/hbc/cli/uninstall.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb index 7fc40daafa..213c5beb6f 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb @@ -2,22 +2,28 @@ 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" + new(*args).run + end - cask_tokens.each do |cask_token| + def initialize(*args) + @cask_tokens = self.class.cask_tokens_from(args) + raise CaskUnspecifiedError if @cask_tokens.empty? + @force = args.include? "--force" + end + + def run + @cask_tokens.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, binaries: CLI.binaries?, force: force).uninstall + Installer.new(cask, binaries: CLI.binaries?, force: @force).uninstall next if (versions = cask.versions).empty? From 5429ab0d1b7e6db39815d1c5826fc5757639c16c Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 May 2017 21:15:36 +0200 Subject: [PATCH 23/28] Refactor `CLI::Version`. --- Library/Homebrew/cask/lib/hbc/cli/--version.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/--version.rb b/Library/Homebrew/cask/lib/hbc/cli/--version.rb index bbc719c3b6..253772ebe9 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/--version.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/--version.rb @@ -6,7 +6,11 @@ module Hbc end def self.run(*args) - raise ArgumentError, "#{command_name} does not take arguments." unless args.empty? + new(*args).run + end + + def run + raise ArgumentError, "#{self.class.command_name} does not take arguments." unless @args.empty? puts Hbc.full_version end From 811f4c5f237dad654c3eba81dfa96b7e9f29c10f Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 04:08:59 +0200 Subject: [PATCH 24/28] Refactor `CLI::Zap`. --- Library/Homebrew/cask/lib/hbc/cli/zap.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/zap.rb b/Library/Homebrew/cask/lib/hbc/cli/zap.rb index 83da1c932e..26dab15488 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/zap.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/zap.rb @@ -2,9 +2,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| + new(*args).run + end + + def initialize(*args) + @cask_tokens = self.class.cask_tokens_from(args) + raise CaskUnspecifiedError if @cask_tokens.empty? + end + + def run + @cask_tokens.each do |cask_token| odebug "Zapping Cask #{cask_token}" cask = CaskLoader.load(cask_token) Installer.new(cask).zap From acc7309ca34a28d340583bad619e236b6bc78e52 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 19:08:03 +0200 Subject: [PATCH 25/28] Rename `Base` and `InternalUseBase`. --- Library/Homebrew/cask/lib/hbc/cli.rb | 9 +++++---- Library/Homebrew/cask/lib/hbc/cli/--version.rb | 2 +- .../cask/lib/hbc/cli/{base.rb => abstract_command.rb} | 10 +++++++++- ...ternal_use_base.rb => abstract_internal_command.rb} | 2 +- Library/Homebrew/cask/lib/hbc/cli/audit.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/cat.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/cleanup.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/create.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/edit.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/fetch.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/home.rb | 6 +----- Library/Homebrew/cask/lib/hbc/cli/info.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/install.rb | 2 +- .../cask/lib/hbc/cli/internal_appcast_checkpoint.rb | 2 +- .../cask/lib/hbc/cli/internal_audit_modified_casks.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/internal_help.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/list.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/outdated.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/search.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/style.rb | 6 +----- Library/Homebrew/cask/lib/hbc/cli/uninstall.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/zap.rb | 2 +- Library/Homebrew/compat/hbc/cli/update.rb | 4 ++-- 27 files changed, 40 insertions(+), 39 deletions(-) rename Library/Homebrew/cask/lib/hbc/cli/{base.rb => abstract_command.rb} (72%) rename Library/Homebrew/cask/lib/hbc/cli/{internal_use_base.rb => abstract_internal_command.rb} (76%) diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index d0c4f30df8..3f6af2c340 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -3,7 +3,7 @@ require "shellwords" require "extend/optparse" -require "hbc/cli/base" +require "hbc/cli/abstract_command" require "hbc/cli/audit" require "hbc/cli/cat" require "hbc/cli/cleanup" @@ -23,7 +23,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" @@ -90,7 +90,8 @@ module Hbc 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 @@ -105,7 +106,7 @@ module Hbc 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) diff --git a/Library/Homebrew/cask/lib/hbc/cli/--version.rb b/Library/Homebrew/cask/lib/hbc/cli/--version.rb index 253772ebe9..23ecde0cc0 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/--version.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/--version.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Version < Base + class Version < AbstractCommand def self.command_name "--#{super}" end diff --git a/Library/Homebrew/cask/lib/hbc/cli/base.rb b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb similarity index 72% rename from Library/Homebrew/cask/lib/hbc/cli/base.rb rename to Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb index 1ad347de3f..0f9f05f94b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/base.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb @@ -1,10 +1,14 @@ module Hbc class CLI - class Base + class AbstractCommand 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 @@ -21,6 +25,10 @@ module Hbc false end + def self.run(*args) + new(*args).run + end + def initialize(*args) @args = args 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 similarity index 76% rename from Library/Homebrew/cask/lib/hbc/cli/internal_use_base.rb rename to Library/Homebrew/cask/lib/hbc/cli/abstract_internal_command.rb index b1f9c5631f..8af71e5895 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 5da2be4cfb..06cccb6acf 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/audit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/audit.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Audit < Base + class Audit < AbstractCommand def self.help "verifies installability of Casks" end diff --git a/Library/Homebrew/cask/lib/hbc/cli/cat.rb b/Library/Homebrew/cask/lib/hbc/cli/cat.rb index 774b96e89c..ca72259262 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cat.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cat.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Cat < Base + class Cat < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb index a62b8d069a..c605fea7ce 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) diff --git a/Library/Homebrew/cask/lib/hbc/cli/create.rb b/Library/Homebrew/cask/lib/hbc/cli/create.rb index 39b9bcda78..09de666493 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/create.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/create.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Create < Base + class Create < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 11917ae6b7..9a3dfcad4a 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Doctor < Base + class Doctor < AbstractCommand def self.run ohai "Homebrew-Cask Version", Hbc.full_version ohai "Homebrew-Cask Install Location", render_install_location diff --git a/Library/Homebrew/cask/lib/hbc/cli/edit.rb b/Library/Homebrew/cask/lib/hbc/cli/edit.rb index 52b089a8bc..f1797de1c8 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/edit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/edit.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Edit < Base + class Edit < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb index f7139ff639..0d1a91e85c 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Fetch < Base + class Fetch < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/home.rb b/Library/Homebrew/cask/lib/hbc/cli/home.rb index 882cb67af7..15cbbfc8d5 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/home.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/home.rb @@ -1,10 +1,6 @@ module Hbc class CLI - class Home < Base - def self.run(*args) - new(*args).run - end - + class Home < AbstractCommand def run casks = @args.map(&CaskLoader.public_method(:load)) diff --git a/Library/Homebrew/cask/lib/hbc/cli/info.rb b/Library/Homebrew/cask/lib/hbc/cli/info.rb index 327290eea8..6813ad63fa 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/info.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/info.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Info < Base + class Info < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb index 53c5107744..cec4d0f2bd 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/install.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Install < Base + class Install < AbstractCommand def self.run(*args) new(*args).run end 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 89eecd8db6..e3045d2899 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class InternalAppcastCheckpoint < InternalUseBase + class InternalAppcastCheckpoint < AbstractInternalCommand def self.run(*args) new(*args).run 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 371e195709..5bf73135a0 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,6 +1,6 @@ module Hbc class CLI - class InternalAuditModifiedCasks < InternalUseBase + class InternalAuditModifiedCasks < AbstractInternalCommand RELEVANT_STANZAS = [:version, :sha256, :url, :appcast].freeze def self.needs_init? diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb index 3cd7add294..06487597ea 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class InternalCheckurl < InternalUseBase + class InternalCheckurl < AbstractInternalCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb index e6dc3510ff..2202a737cd 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class InternalDump < InternalUseBase + class InternalDump < AbstractInternalCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb index 3903f3b080..3645d91384 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class InternalHelp < InternalUseBase + class InternalHelp < AbstractInternalCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb index a60a08d66c..82cf981fdf 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 [ --table | --yaml | --inspect | --quiet ] [ ... ] diff --git a/Library/Homebrew/cask/lib/hbc/cli/list.rb b/Library/Homebrew/cask/lib/hbc/cli/list.rb index f79dc9a54b..884203254b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/list.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/list.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class List < Base + class List < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb index 8eb387df2d..7a5485344b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Outdated < Base + class Outdated < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index 1080538548..3ad1dd4da4 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Search < Base + class Search < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/style.rb b/Library/Homebrew/cask/lib/hbc/cli/style.rb index a76c893fce..992e1bb136 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/style.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/style.rb @@ -2,15 +2,11 @@ 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) - new(*args).run - end - attr_reader :args def initialize(*args) @cask_tokens = self.class.cask_tokens_from(args) diff --git a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb index 213c5beb6f..dea6a8e5f1 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Uninstall < Base + class Uninstall < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/cask/lib/hbc/cli/zap.rb b/Library/Homebrew/cask/lib/hbc/cli/zap.rb index 26dab15488..df40894afc 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/zap.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/zap.rb @@ -1,6 +1,6 @@ module Hbc class CLI - class Zap < Base + class Zap < AbstractCommand def self.run(*args) new(*args).run end diff --git a/Library/Homebrew/compat/hbc/cli/update.rb b/Library/Homebrew/compat/hbc/cli/update.rb index 7820997cbb..95321e8981 100644 --- a/Library/Homebrew/compat/hbc/cli/update.rb +++ b/Library/Homebrew/compat/hbc/cli/update.rb @@ -1,8 +1,8 @@ -require "cask/lib/hbc/cli/base" +require "cask/lib/hbc/cli/abstract_command" module Hbc class CLI - class Update < Base + class Update < AbstractCommand def self.run(*_ignored) odeprecated "`brew cask update`", "`brew update`", disable_on: Time.utc(2017, 7, 1) result = SystemCommand.run(HOMEBREW_BREW_FILE, args: ["update"], From debe4540e4ff76be80467a2bf32bfd32dbfc72eb Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 19:11:34 +0200 Subject: [PATCH 26/28] Remove redundant `self.run` methods. --- Library/Homebrew/cask/lib/hbc/cli/--version.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/audit.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/cat.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/cleanup.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/create.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/edit.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/fetch.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/info.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/install.rb | 4 ---- .../Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb | 4 ---- .../cask/lib/hbc/cli/internal_audit_modified_casks.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/internal_help.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/list.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/outdated.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/search.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/uninstall.rb | 4 ---- Library/Homebrew/cask/lib/hbc/cli/zap.rb | 4 ---- 20 files changed, 80 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/--version.rb b/Library/Homebrew/cask/lib/hbc/cli/--version.rb index 23ecde0cc0..7f9609c881 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/--version.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/--version.rb @@ -5,10 +5,6 @@ module Hbc "--#{super}" end - def self.run(*args) - new(*args).run - end - def run raise ArgumentError, "#{self.class.command_name} does not take arguments." unless @args.empty? puts Hbc.full_version diff --git a/Library/Homebrew/cask/lib/hbc/cli/audit.rb b/Library/Homebrew/cask/lib/hbc/cli/audit.rb index 06cccb6acf..697b47ba6a 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/audit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/audit.rb @@ -5,10 +5,6 @@ module Hbc "verifies installability of Casks" end - def self.run(*args) - new(*args).run - end - def initialize(*args, auditor: Auditor) @args = args @auditor = auditor diff --git a/Library/Homebrew/cask/lib/hbc/cli/cat.rb b/Library/Homebrew/cask/lib/hbc/cli/cat.rb index ca72259262..9c678454cb 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cat.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cat.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Cat < AbstractCommand - def self.run(*args) - new(*args).run - end - def run cask_tokens = self.class.cask_tokens_from(@args) raise CaskUnspecifiedError if cask_tokens.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb index c605fea7ce..72ba9e65fc 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb @@ -12,10 +12,6 @@ module Hbc true end - def self.run(*args) - new(*args).run - end - attr_reader :cache_location, :outdated_only def initialize(*args, cache_location: Hbc.cache, outdated_only: CLI.outdated?) diff --git a/Library/Homebrew/cask/lib/hbc/cli/create.rb b/Library/Homebrew/cask/lib/hbc/cli/create.rb index 09de666493..dc8666b374 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/create.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/create.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Create < AbstractCommand - def self.run(*args) - new(*args).run - end - def run cask_tokens = self.class.cask_tokens_from(@args) raise CaskUnspecifiedError if cask_tokens.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/edit.rb b/Library/Homebrew/cask/lib/hbc/cli/edit.rb index f1797de1c8..d8ab3d9e0a 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/edit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/edit.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Edit < AbstractCommand - def self.run(*args) - new(*args).run - end - def run cask_tokens = self.class.cask_tokens_from(@args) raise CaskUnspecifiedError if cask_tokens.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb index 0d1a91e85c..2dc02aaf73 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/fetch.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/fetch.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Fetch < AbstractCommand - def self.run(*args) - new(*args).run - end - def run cask_tokens = self.class.cask_tokens_from(@args) raise CaskUnspecifiedError if cask_tokens.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/info.rb b/Library/Homebrew/cask/lib/hbc/cli/info.rb index 6813ad63fa..1a63b77471 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/info.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/info.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Info < AbstractCommand - def self.run(*args) - new(*args).run - end - def initialize(*args) @cask_tokens = self.class.cask_tokens_from(args) raise CaskUnspecifiedError if @cask_tokens.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb index cec4d0f2bd..fc9ba97fa5 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/install.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Install < AbstractCommand - def self.run(*args) - new(*args).run - end - def initialize(*args) @cask_tokens = self.class.cask_tokens_from(args) raise CaskUnspecifiedError if @cask_tokens.empty? 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 e3045d2899..ed90bc4a9b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb @@ -1,10 +1,6 @@ module Hbc class CLI class InternalAppcastCheckpoint < AbstractInternalCommand - def self.run(*args) - new(*args).run - end - def initialize(*args) @cask_tokens = cask_tokens_from(args) raise CaskUnspecifiedError if cask_tokens.empty? 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 5bf73135a0..e1b0e47918 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 @@ -7,10 +7,6 @@ module Hbc true end - def self.run(*args) - new(*args).run - end - def initialize(*args) @commit_range = self.class.commit_range(args) @cleanup = args.any? { |a| a =~ /^-+c(leanup)?$/i } diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb index 06487597ea..b0378cfd51 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb @@ -1,10 +1,6 @@ module Hbc class CLI class InternalCheckurl < AbstractInternalCommand - def self.run(*args) - new(*args).run - end - def run casks_to_check = @args.empty? ? Hbc.all : @args.map { |arg| CaskLoader.load(arg) } casks_to_check.each do |cask| diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb index 2202a737cd..6a0d458cf3 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb @@ -1,10 +1,6 @@ module Hbc class CLI class InternalDump < AbstractInternalCommand - def self.run(*args) - new(*args).run - end - def initialize(*args) @cask_tokens = self.class.cask_tokens_from(args) raise CaskUnspecifiedError if @cask_tokens.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb index 3645d91384..79f65da0e4 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb @@ -1,10 +1,6 @@ module Hbc class CLI class InternalHelp < AbstractInternalCommand - def self.run(*args) - new(*args).run - end - def run max_command_len = CLI.commands.map(&:length).max puts "Unstable Internal-use Commands:\n\n" diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb index 82cf981fdf..75ccfef023 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb @@ -50,10 +50,6 @@ module Hbc :uninstall_postflight, ] - def self.run(*args) - new(*args).run - end - def initialize(*args) raise ArgumentError, "No stanza given." if args.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/list.rb b/Library/Homebrew/cask/lib/hbc/cli/list.rb index 884203254b..79f7687e1b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/list.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/list.rb @@ -1,10 +1,6 @@ module Hbc class CLI class List < AbstractCommand - def self.run(*args) - new(*args).run - end - def initialize(*args) @cask_tokens = self.class.cask_tokens_from(args) @one = true if args.delete("-1") diff --git a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb index 7a5485344b..c56e28e28d 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Outdated < AbstractCommand - def self.run(*args) - new(*args).run - end - def initialize(*args) @cask_tokens = self.class.cask_tokens_from(args) diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index 3ad1dd4da4..255010baeb 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Search < AbstractCommand - def self.run(*args) - new(*args).run - end - def run self.class.render_results(*self.class.search(*@args)) end diff --git a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb index dea6a8e5f1..55c733108d 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Uninstall < AbstractCommand - def self.run(*args) - new(*args).run - end - def initialize(*args) @cask_tokens = self.class.cask_tokens_from(args) raise CaskUnspecifiedError if @cask_tokens.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/zap.rb b/Library/Homebrew/cask/lib/hbc/cli/zap.rb index df40894afc..61a0df27ce 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/zap.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/zap.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Zap < AbstractCommand - def self.run(*args) - new(*args).run - end - def initialize(*args) @cask_tokens = self.class.cask_tokens_from(args) raise CaskUnspecifiedError if @cask_tokens.empty? From df1864ee435c1c48f87344800d2e7cf055a12f93 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 21 May 2017 00:15:56 +0200 Subject: [PATCH 27/28] Add `CLI::Options` DSL. --- Library/Homebrew/.rubocop.yml | 3 + Library/Homebrew/cask/lib/hbc/artifact.rb | 4 +- .../Homebrew/cask/lib/hbc/artifact/base.rb | 11 +- .../Homebrew/cask/lib/hbc/artifact/moved.rb | 2 +- Library/Homebrew/cask/lib/hbc/artifact/pkg.rb | 2 +- .../cask/lib/hbc/artifact/uninstall_base.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli.rb | 152 +++++++----------- .../Homebrew/cask/lib/hbc/cli/--version.rb | 7 +- .../cask/lib/hbc/cli/abstract_command.rb | 14 +- Library/Homebrew/cask/lib/hbc/cli/audit.rb | 38 +---- Library/Homebrew/cask/lib/hbc/cli/cat.rb | 17 +- Library/Homebrew/cask/lib/hbc/cli/cleanup.rb | 11 +- Library/Homebrew/cask/lib/hbc/cli/create.rb | 15 +- Library/Homebrew/cask/lib/hbc/cli/doctor.rb | 22 ++- Library/Homebrew/cask/lib/hbc/cli/edit.rb | 15 +- Library/Homebrew/cask/lib/hbc/cli/fetch.rb | 15 +- Library/Homebrew/cask/lib/hbc/cli/home.rb | 2 +- Library/Homebrew/cask/lib/hbc/cli/info.rb | 8 +- Library/Homebrew/cask/lib/hbc/cli/install.rb | 26 +-- .../hbc/cli/internal_appcast_checkpoint.rb | 13 +- .../hbc/cli/internal_audit_modified_casks.rb | 32 ++-- .../cask/lib/hbc/cli/internal_checkurl.rb | 2 +- .../cask/lib/hbc/cli/internal_dump.rb | 10 +- .../cask/lib/hbc/cli/internal_help.rb | 6 + .../cask/lib/hbc/cli/internal_stanza.rb | 51 +++--- Library/Homebrew/cask/lib/hbc/cli/list.rb | 28 ++-- Library/Homebrew/cask/lib/hbc/cli/options.rb | 62 +++++++ Library/Homebrew/cask/lib/hbc/cli/outdated.rb | 17 +- .../Homebrew/cask/lib/hbc/cli/reinstall.rb | 13 +- Library/Homebrew/cask/lib/hbc/cli/search.rb | 7 +- Library/Homebrew/cask/lib/hbc/cli/style.rb | 18 +-- .../Homebrew/cask/lib/hbc/cli/uninstall.rb | 15 +- Library/Homebrew/cask/lib/hbc/cli/zap.rb | 10 +- Library/Homebrew/cask/lib/hbc/installer.rb | 33 ++-- Library/Homebrew/cmd/cask.rb | 2 +- 35 files changed, 377 insertions(+), 308 deletions(-) create mode 100644 Library/Homebrew/cask/lib/hbc/cli/options.rb diff --git a/Library/Homebrew/.rubocop.yml b/Library/Homebrew/.rubocop.yml index 938bf21b7c..4158f9f644 100644 --- a/Library/Homebrew/.rubocop.yml +++ b/Library/Homebrew/.rubocop.yml @@ -34,6 +34,9 @@ Metrics/ModuleLength: - 'cask/lib/hbc/macos.rb' - 'cask/lib/hbc/utils.rb' +Metrics/ParameterLists: + CountKeywordArgs: false + # so many of these in formulae but none in here Style/GuardClause: Enabled: true diff --git a/Library/Homebrew/cask/lib/hbc/artifact.rb b/Library/Homebrew/cask/lib/hbc/artifact.rb index b155a125a8..e5eb54c36f 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 924493fc0f..96349f0816 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/moved.rb b/Library/Homebrew/cask/lib/hbc/artifact/moved.rb index eaaa49e207..3fe969c0c2 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 c43481c82b..be0a6be717 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 478f313b5a..2ddd0cfb36 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/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index 3f6af2c340..1c20a2a331 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -2,6 +2,7 @@ require "optparse" require "shellwords" require "extend/optparse" +require "hbc/cli/options" require "hbc/cli/abstract_command" require "hbc/cli/audit" @@ -44,49 +45,36 @@ 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 + include Options - FLAGS = { - ["--[no-]binaries", :binaries] => true, - ["--verbose", :verbose] => false, - ["--outdated", :outdated] => false, - ["--help", :help] => false, - }.freeze + 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 - FLAGS.each do |(_, method), default_value| - instance_variable_set(:"@#{method}", default_value) + option "--help", :help, false - define_singleton_method(:"#{method}=") do |arg| - instance_variable_set(:"@#{method}", arg) - end + # handled in OS::Mac + option "--language a,b,c", ->(*) { raise OptionParser::InvalidOption } - define_singleton_method(:"#{method}?") do - instance_variable_get(:"@#{method}") - end - end + # override default handling of --version + option "--version", ->(*) { raise OptionParser::InvalidOption } def self.command_classes @command_classes ||= constants.map(&method(:const_get)) @@ -99,10 +87,10 @@ 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) @@ -145,18 +133,26 @@ 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 ARGV.debug? onoe msg @@ -188,59 +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 - - FLAGS.keys.each do |flag, method| - opts.on(flag) do |bool| - send(:"#{method}=", bool) - end - end - - opts.on("--version") do - raise OptionParser::InvalidOption # override default handling of --version - end - end - end - - def self.process_options(args) + def process_options(*args) all_args = Shellwords.shellsplit(ENV["HOMEBREW_CASK_OPTS"] || "") + args - remaining = [] - until all_args.empty? + + non_options = [] + + if idx = all_args.index("--") + non_options += all_args.drop(idx) + all_args = all_args.first(idx) + end + + 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? - - 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 7f9609c881..1833c51c67 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/--version.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/--version.rb @@ -5,8 +5,13 @@ module Hbc "--#{super}" end + def initialize(*) + super + return if args.empty? + raise ArgumentError, "#{self.class.command_name} does not take arguments." + end + def run - raise ArgumentError, "#{self.class.command_name} does not take arguments." unless @args.empty? 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 index 0f9f05f94b..5264799eda 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb @@ -1,6 +1,15 @@ +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 @@ -29,8 +38,11 @@ module Hbc new(*args).run end + attr_accessor :args + private :args= + def initialize(*args) - @args = args + @args = process_arguments(*args) end end end diff --git a/Library/Homebrew/cask/lib/hbc/cli/audit.rb b/Library/Homebrew/cask/lib/hbc/cli/audit.rb index 697b47ba6a..74d1ebfa77 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/audit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/audit.rb @@ -1,21 +1,18 @@ module Hbc class CLI class Audit < AbstractCommand + option "--download", :download, false + option "--token-conflicts", :token_conflicts, false + def self.help "verifies installability of Casks" end - def initialize(*args, auditor: Auditor) - @args = args - @auditor = auditor - end - def run - failed_casks = [] + casks_to_audit = args.empty? ? Hbc.all : args.map(&CaskLoader.public_method(:load)) - casks_to_audit.each do |cask| - next if audit(cask) - failed_casks << cask + failed_casks = casks_to_audit.reject do |cask| + audit(cask) end return if failed_casks.empty? @@ -24,28 +21,7 @@ module Hbc 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/cat.rb b/Library/Homebrew/cask/lib/hbc/cli/cat.rb index 9c678454cb..e68481b46b 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 < AbstractCommand + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? + end + def run - cask_tokens = self.class.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) + 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 72ba9e65fc..e253932307 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/cleanup.rb @@ -12,12 +12,11 @@ module Hbc true end - attr_reader :cache_location, :outdated_only + attr_reader :cache_location - def initialize(*args, cache_location: Hbc.cache, outdated_only: CLI.outdated?) - @args = args + def initialize(*args, cache_location: Hbc.cache) + super(*args) @cache_location = Pathname.new(cache_location) - @outdated_only = outdated_only end def run @@ -32,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) @@ -54,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 dc8666b374..8de101092b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/create.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/create.rb @@ -1,15 +1,18 @@ module Hbc class CLI class Create < AbstractCommand - def run - cask_tokens = self.class.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}" + 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 self.class.template(cask_token) end diff --git a/Library/Homebrew/cask/lib/hbc/cli/doctor.rb b/Library/Homebrew/cask/lib/hbc/cli/doctor.rb index 9a3dfcad4a..64cd11f9e2 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 < AbstractCommand - def self.run + 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) diff --git a/Library/Homebrew/cask/lib/hbc/cli/edit.rb b/Library/Homebrew/cask/lib/hbc/cli/edit.rb index d8ab3d9e0a..dec0fe36ba 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 < 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_tokens = self.class.cask_tokens_from(@args) - raise CaskUnspecifiedError if cask_tokens.empty? - # only respects the first argument - cask_token = cask_tokens.first.sub(/\.rb$/i, "") + 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 2dc02aaf73..2c1cc5f66e 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 < AbstractCommand - def run - cask_tokens = self.class.cask_tokens_from(@args) - raise CaskUnspecifiedError if cask_tokens.empty? - force = @args.include? "--force" + 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 15cbbfc8d5..009bc1e3e5 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/home.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/home.rb @@ -2,7 +2,7 @@ module Hbc class CLI class Home < AbstractCommand def run - casks = @args.map(&CaskLoader.public_method(:load)) + casks = args.map(&CaskLoader.public_method(:load)) if casks.empty? odebug "Opening project homepage" diff --git a/Library/Homebrew/cask/lib/hbc/cli/info.rb b/Library/Homebrew/cask/lib/hbc/cli/info.rb index 1a63b77471..dcfc8d9bb5 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/info.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/info.rb @@ -1,13 +1,13 @@ module Hbc class CLI class Info < AbstractCommand - def initialize(*args) - @cask_tokens = self.class.cask_tokens_from(args) - raise CaskUnspecifiedError if @cask_tokens.empty? + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? end def run - @cask_tokens.each do |cask_token| + args.each do |cask_token| odebug "Getting info for Cask #{cask_token}" cask = CaskLoader.load(cask_token) diff --git a/Library/Homebrew/cask/lib/hbc/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb index fc9ba97fa5..5acd837b1a 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/install.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb @@ -1,12 +1,13 @@ module Hbc class CLI class Install < AbstractCommand - def initialize(*args) - @cask_tokens = self.class.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" + 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 @@ -19,13 +20,14 @@ module Hbc 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, binaries: CLI.binaries?, - 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 @@ -43,7 +45,7 @@ module Hbc 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 ed90bc4a9b..6b83b24464 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb @@ -1,17 +1,18 @@ module Hbc class CLI class InternalAppcastCheckpoint < AbstractInternalCommand - def initialize(*args) - @cask_tokens = cask_tokens_from(args) - raise CaskUnspecifiedError if cask_tokens.empty? - @calculate = args.include? "--calculate" + 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$/ } + if cask_tokens.all? { |t| t =~ %r{^https?://} && t !~ /\.rb$/ } self.class.appcask_checkpoint_for_url(cask_tokens) else - self.class.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 e1b0e47918..f1a0308e57 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 @@ -3,23 +3,27 @@ module Hbc class InternalAuditModifiedCasks < AbstractInternalCommand RELEVANT_STANZAS = [:version, :sha256, :url, :appcast].freeze + option "--cleanup", :cleanup, false + def self.needs_init? true end - def initialize(*args) - @commit_range = self.class.commit_range(args) - @cleanup = args.any? { |a| a =~ /^-+c(leanup)?$/i } - 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 - def self.posargs(args) - args.reject { |a| a.empty? || a.chars.first == "-" } + if args.count != 1 + raise ArgumentError, <<-EOS.undent + This command requires exactly one argument. + + #{self.class.usage} + EOS + end + + @commit_range = args.first end def self.help @@ -40,12 +44,6 @@ module Hbc EOS 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 b0378cfd51..a8c3d5c8f0 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_checkurl.rb @@ -2,7 +2,7 @@ module Hbc class CLI class InternalCheckurl < AbstractInternalCommand def run - casks_to_check = @args.empty? ? Hbc.all : @args.map { |arg| CaskLoader.load(arg) } + 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 6a0d458cf3..78dbf1622b 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb @@ -1,9 +1,9 @@ module Hbc class CLI class InternalDump < AbstractInternalCommand - def initialize(*args) - @cask_tokens = self.class.cask_tokens_from(args) - raise CaskUnspecifiedError if @cask_tokens.empty? + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? end def run @@ -16,7 +16,7 @@ module Hbc def dump_casks count = 0 - @cask_tokens.each do |cask_token| + args.each do |cask_token| begin cask = CaskLoader.load(cask_token) count += 1 @@ -25,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 79f65da0e4..beac77b293 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_help.rb @@ -1,6 +1,12 @@ module Hbc class CLI 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" diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb index 75ccfef023..86dee7c9c1 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb @@ -50,51 +50,60 @@ module Hbc :uninstall_postflight, ] - def initialize(*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 = self.class.cask_tokens_from(args) - @stanza = @cask_tokens.shift.to_sym - @cask_tokens = Hbc.all_tokens if @cask_tokens.empty? + @stanza = args.shift.to_sym + + @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 print_stanzas count = 0 - if ARTIFACTS.include?(@stanza) - artifact_name = @stanza + if ARTIFACTS.include?(stanza) + artifact_name = stanza @stanza = :artifacts end - @cask_tokens.each do |cask_token| - print "#{cask_token}\t" if @table + cask_tokens = args.empty? ? Hbc.all_tokens : args + cask_tokens.each do |cask_token| + 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 + unless cask.respond_to?(stanza) + opoo "no such stanza '#{stanza}' on Cask '#{cask_token}'" unless quiet? puts "" next end @@ -102,13 +111,13 @@ module Hbc begin 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 @@ -125,7 +134,7 @@ module Hbc count += 1 end - count.zero? ? nil : count == @cask_tokens.length + count.zero? ? nil : count == cask_tokens.length end def self.help diff --git a/Library/Homebrew/cask/lib/hbc/cli/list.rb b/Library/Homebrew/cask/lib/hbc/cli/list.rb index 79f7687e1b..04dccbf858 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/list.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/list.rb @@ -1,20 +1,18 @@ module Hbc class CLI class List < AbstractCommand - def initialize(*args) - @cask_tokens = self.class.cask_tokens_from(args) - @one = true if args.delete("-1") - @versions = true if args.delete("--versions") + option "-1", :one, false + option "--versions", :versions, false - return unless args.delete("-l") - @one = true + option "-l", (lambda do |*| + one = true # rubocop:disable Lint/UselessAssignment opoo "Option -l is obsolete! Implying option -1." - end + end) def run - retval = @cask_tokens.any? ? list : list_installed + retval = args.any? ? list : list_installed # retval is ternary: true/false/nil - if retval.nil? && !@cask_tokens.any? + if retval.nil? && !args.any? opoo "nothing to list" # special case: avoid exit code elsif retval.nil? raise CaskError, "nothing to list" @@ -26,15 +24,15 @@ module Hbc 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 @one + if one? puts cask.token - elsif @versions + elsif versions? puts self.class.format_versioned(cask) else cask = CaskLoader.load_from_file(cask.installed_caskfile) @@ -50,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) @@ -63,9 +61,9 @@ module Hbc def list_installed installed_casks = Hbc.installed - if @one + if one? puts installed_casks.map(&:to_s) - elsif @versions + elsif versions? puts installed_casks.map(&self.class.method(:format_versioned)) elsif !installed_casks.empty? puts Formatter.columns(installed_casks.map(&:to_s)) 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 0000000000..84126caa56 --- /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 c56e28e28d..7877ead05d 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb @@ -1,23 +1,20 @@ module Hbc class CLI class Outdated < AbstractCommand - def initialize(*args) - @cask_tokens = self.class.cask_tokens_from(args) + option "--greedy", :greedy, false + option "--quiet", :quiet, false - @greedy = args.include?("--greedy") - @verbose = ($stdout.tty? || CLI.verbose?) && !args.include?("--quiet") + def initialize(*) + super + self.verbose = ($stdout.tty? || verbose?) && !quiet? end def run - casks_to_check = if @cask_tokens.empty? - Hbc.installed - else - @cask_tokens.map(&CaskLoader.public_method(:load)) - end + 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}" - self.class.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 b675a79fa5..eb5f45c905 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb @@ -3,15 +3,16 @@ module Hbc class Reinstall < Install 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, - binaries: CLI.binaries?, - 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 @@ -22,7 +23,7 @@ module Hbc 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 255010baeb..b24091aef6 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 < AbstractCommand + def initialize(*args) + @args = args + end + def run - self.class.render_results(*self.class.search(*@args)) + 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 992e1bb136..078796e7e1 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/style.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/style.rb @@ -7,15 +7,7 @@ module Hbc "checks Cask style using RuboCop" end - attr_reader :args - def initialize(*args) - @cask_tokens = self.class.cask_tokens_from(args) - @fix = args.any? { |arg| arg =~ /^--(fix|(auto-?)?correct)$/ } - end - - def fix? - @fix - end + option "--fix", :fix, false def run install_rubocop @@ -35,12 +27,12 @@ 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 diff --git a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb index 55c733108d..33ee5afa9a 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb @@ -1,25 +1,26 @@ module Hbc class CLI class Uninstall < AbstractCommand - def initialize(*args) - @cask_tokens = self.class.cask_tokens_from(args) - raise CaskUnspecifiedError if @cask_tokens.empty? - @force = args.include? "--force" + option "--force", :force, false + + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? end def run - @cask_tokens.each do |cask_token| + 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, binaries: CLI.binaries?, 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 61a0df27ce..3c07ff9e88 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/zap.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/zap.rb @@ -1,16 +1,16 @@ module Hbc class CLI class Zap < AbstractCommand - def initialize(*args) - @cask_tokens = self.class.cask_tokens_from(args) - raise CaskUnspecifiedError if @cask_tokens.empty? + def initialize(*) + super + raise CaskUnspecifiedError if args.empty? end def run - @cask_tokens.each do |cask_token| + 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 d9addd6040..a783f764bf 100644 --- a/Library/Homebrew/cask/lib/hbc/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/installer.rb @@ -14,24 +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, binaries: true, 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? @@ -64,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 @@ -82,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 @@ -113,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, binaries: binaries?, force: true).uninstall + Installer.new(installed_cask, binaries: binaries?, verbose: verbose?, force: true).uninstall end def summary @@ -161,7 +172,7 @@ 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| @@ -199,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 @@ -264,7 +275,7 @@ module Hbc if dep.installed? puts "already installed" else - Installer.new(dep, force: false, binaries: binaries?, skip_cask_deps: true).install + Installer.new(dep, binaries: binaries?, verbose: verbose?, skip_cask_deps: true, force: false).install puts "done" end end @@ -340,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/cmd/cask.rb b/Library/Homebrew/cmd/cask.rb index 8a68b8d9a7..550081d46d 100644 --- a/Library/Homebrew/cmd/cask.rb +++ b/Library/Homebrew/cmd/cask.rb @@ -5,6 +5,6 @@ module Homebrew module_function def cask - Hbc::CLI.process(ARGV) + Hbc::CLI.run(*ARGV) end end From 02a1e2781fcb808b731fd04d18369ec5bfd3bc68 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 21 May 2017 02:32:46 +0200 Subject: [PATCH 28/28] Fix tests for `CLI::Options` DSL. --- Library/Homebrew/test/cask/cli/audit_spec.rb | 29 +++++----- Library/Homebrew/test/cask/cli/cat_spec.rb | 16 +++--- .../Homebrew/test/cask/cli/cleanup_spec.rb | 6 ++- Library/Homebrew/test/cask/cli/create_spec.rb | 26 ++++----- Library/Homebrew/test/cask/cli/edit_spec.rb | 10 ++-- Library/Homebrew/test/cask/cli/fetch_spec.rb | 2 +- Library/Homebrew/test/cask/cli/info_spec.rb | 4 +- .../Homebrew/test/cask/cli/install_spec.rb | 8 ++- .../Homebrew/test/cask/cli/options_spec.rb | 54 ++++++------------- .../Homebrew/test/cask/cli/outdated_spec.rb | 32 ++++++----- Library/Homebrew/test/cask/cli/style_spec.rb | 31 +---------- .../Homebrew/test/cask/cli/uninstall_spec.rb | 2 +- Library/Homebrew/test/cask/cli/zap_spec.rb | 5 +- Library/Homebrew/test/cask/cli_spec.rb | 24 ++++----- 14 files changed, 103 insertions(+), 146 deletions(-) diff --git a/Library/Homebrew/test/cask/cli/audit_spec.rb b/Library/Homebrew/test/cask/cli/audit_spec.rb index 312a267f8e..412db1481e 100644 --- a/Library/Homebrew/test/cask/cli/audit_spec.rb +++ b/Library/Homebrew/test/cask/cli/audit_spec.rb @@ -1,69 +1,64 @@ describe Hbc::CLI::Audit, :cask do - let(:auditor) { double } let(:cask) { double } describe "selection of Casks to audit" do it "audits all Casks if no tokens are given" do allow(Hbc).to receive(:all).and_return([cask, cask]) - expect(auditor).to receive(:audit).twice.and_return(true) + expect(Hbc::Auditor).to receive(:audit).twice.and_return(true) - run_audit + Hbc::CLI::Audit.run end it "audits specified Casks if tokens are given" do cask_token = "nice-app" expect(Hbc::CaskLoader).to receive(:load).with(cask_token).and_return(cask) - expect(auditor).to receive(:audit) + expect(Hbc::Auditor).to receive(:audit) .with(cask, audit_download: false, check_token_conflicts: false) .and_return(true) - run_audit(cask_token) + Hbc::CLI::Audit.run(cask_token) end end describe "rules for downloading a Cask" do it "does not download the Cask per default" do allow(Hbc::CaskLoader).to receive(:load).and_return(cask) - expect(auditor).to receive(:audit) + expect(Hbc::Auditor).to receive(:audit) .with(cask, audit_download: false, check_token_conflicts: false) .and_return(true) - run_audit("casktoken") + Hbc::CLI::Audit.run("casktoken") end it "download a Cask if --download flag is set" do allow(Hbc::CaskLoader).to receive(:load).and_return(cask) - expect(auditor).to receive(:audit) + expect(Hbc::Auditor).to receive(:audit) .with(cask, audit_download: true, check_token_conflicts: false) .and_return(true) - run_audit("casktoken", "--download") + Hbc::CLI::Audit.run("casktoken", "--download") end end describe "rules for checking token conflicts" do it "does not check for token conflicts per default" do allow(Hbc::CaskLoader).to receive(:load).and_return(cask) - expect(auditor).to receive(:audit) + expect(Hbc::Auditor).to receive(:audit) .with(cask, audit_download: false, check_token_conflicts: false) .and_return(true) - run_audit("casktoken") + Hbc::CLI::Audit.run("casktoken") end it "checks for token conflicts if --token-conflicts flag is set" do allow(Hbc::CaskLoader).to receive(:load).and_return(cask) - expect(auditor).to receive(:audit) + expect(Hbc::Auditor).to receive(:audit) .with(cask, audit_download: false, check_token_conflicts: true) .and_return(true) - run_audit("casktoken", "--token-conflicts") + Hbc::CLI::Audit.run("casktoken", "--token-conflicts") end end - - def run_audit(*args) - Hbc::CLI::Audit.new(*args, auditor: auditor).run - end end diff --git a/Library/Homebrew/test/cask/cli/cat_spec.rb b/Library/Homebrew/test/cask/cli/cat_spec.rb index daf6fb960f..28089b2f1f 100644 --- a/Library/Homebrew/test/cask/cli/cat_spec.rb +++ b/Library/Homebrew/test/cask/cli/cat_spec.rb @@ -1,6 +1,6 @@ describe Hbc::CLI::Cat, :cask do describe "given a basic Cask" do - let(:expected_output) { + let(:basic_cask_content) { <<-EOS.undent cask 'basic-cask' do version '1.2.3' @@ -17,19 +17,19 @@ describe Hbc::CLI::Cat, :cask do it "displays the Cask file content about the specified Cask" do expect { Hbc::CLI::Cat.run("basic-cask") - }.to output(expected_output).to_stdout + }.to output(basic_cask_content).to_stdout end - it "throws away additional Cask arguments and uses the first" do + it "can display multiple Casks" do expect { - Hbc::CLI::Cat.run("basic-cask", "local-caffeine") - }.to output(expected_output).to_stdout + Hbc::CLI::Cat.run("basic-cask", "basic-cask") + }.to output(basic_cask_content * 2).to_stdout end - it "throws away stray options" do + it "fails when option is unknown" do expect { Hbc::CLI::Cat.run("--notavalidoption", "basic-cask") - }.to output(expected_output).to_stdout + }.to raise_error(/invalid option/) end end @@ -51,7 +51,7 @@ describe Hbc::CLI::Cat, :cask do it "raises an exception" do expect { Hbc::CLI::Cat.run("--notavalidoption") - }.to raise_error(Hbc::CaskUnspecifiedError) + }.to raise_error(/invalid option/) end end end diff --git a/Library/Homebrew/test/cask/cli/cleanup_spec.rb b/Library/Homebrew/test/cask/cli/cleanup_spec.rb index 037fa1bd40..64e3ef49fe 100644 --- a/Library/Homebrew/test/cask/cli/cleanup_spec.rb +++ b/Library/Homebrew/test/cask/cli/cleanup_spec.rb @@ -2,7 +2,11 @@ describe Hbc::CLI::Cleanup, :cask do let(:cache_location) { Pathname.new(Dir.mktmpdir).realpath } let(:outdated_only) { false } - subject { described_class.new(*cask_tokens, cache_location: cache_location, outdated_only: outdated_only) } + subject { described_class.new(*cask_tokens, cache_location: cache_location) } + + before(:each) do + allow_any_instance_of(described_class).to receive(:outdated_only?).and_return(outdated_only) + end after do cache_location.rmtree diff --git a/Library/Homebrew/test/cask/cli/create_spec.rb b/Library/Homebrew/test/cask/cli/create_spec.rb index ef6944c327..d77b0a2aac 100644 --- a/Library/Homebrew/test/cask/cli/create_spec.rb +++ b/Library/Homebrew/test/cask/cli/create_spec.rb @@ -36,16 +36,10 @@ describe Hbc::CLI::Create, :cask do EOS end - it "throws away additional Cask arguments and uses the first" do - command = described_class.new("additional-cask", "another-cask") - expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("additional-cask")) - command.run - end - - it "throws away stray options" do - command = described_class.new("--notavalidoption", "yet-another-cask") - expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("yet-another-cask")) - command.run + it "raises an exception when more than one Cask is given" do + expect { + described_class.run("additional-cask", "another-cask") + }.to raise_error(/Only one Cask can be created at a time./) end it "raises an exception when the Cask already exists" do @@ -68,11 +62,17 @@ describe Hbc::CLI::Create, :cask do end end - describe "when no Cask is specified, but an invalid option" do - it "raises an exception" do + context "when an invalid option is specified" do + it "raises an exception when no Cask is specified" do expect { described_class.run("--notavalidoption") - }.to raise_error(Hbc::CaskUnspecifiedError) + }.to raise_error(/invalid option/) + end + + it "raises an exception" do + expect { + described_class.run("--notavalidoption", "yet-another-cask") + }.to raise_error(/invalid option/) end end end diff --git a/Library/Homebrew/test/cask/cli/edit_spec.rb b/Library/Homebrew/test/cask/cli/edit_spec.rb index a097f08940..5d5cbf4b93 100644 --- a/Library/Homebrew/test/cask/cli/edit_spec.rb +++ b/Library/Homebrew/test/cask/cli/edit_spec.rb @@ -9,10 +9,10 @@ describe Hbc::CLI::Edit, :cask do command.run end - it "throws away additional arguments and uses the first" do - command = described_class.new("local-caffeine", "local-transmission") - expect(command).to receive(:exec_editor).with(Hbc::CaskLoader.path("local-caffeine")) - command.run + it "raises an error when given more than one argument" do + expect { + described_class.new("local-caffeine", "local-transmission") + }.to raise_error(/Only one Cask can be created at a time./) end it "raises an exception when the Cask doesnt exist" do @@ -33,7 +33,7 @@ describe Hbc::CLI::Edit, :cask do it "raises an exception" do expect { described_class.run("--notavalidoption") - }.to raise_error(Hbc::CaskUnspecifiedError) + }.to raise_error(/invalid option/) end end end diff --git a/Library/Homebrew/test/cask/cli/fetch_spec.rb b/Library/Homebrew/test/cask/cli/fetch_spec.rb index 1571c2a701..9f3056631b 100644 --- a/Library/Homebrew/test/cask/cli/fetch_spec.rb +++ b/Library/Homebrew/test/cask/cli/fetch_spec.rb @@ -69,7 +69,7 @@ describe Hbc::CLI::Fetch, :cask do it "raises an exception" do expect { Hbc::CLI::Fetch.run("--notavalidoption") - }.to raise_error(Hbc::CaskUnspecifiedError) + }.to raise_error(/invalid option/) end end end diff --git a/Library/Homebrew/test/cask/cli/info_spec.rb b/Library/Homebrew/test/cask/cli/info_spec.rb index 2f70a0b967..bffe900ec4 100644 --- a/Library/Homebrew/test/cask/cli/info_spec.rb +++ b/Library/Homebrew/test/cask/cli/info_spec.rb @@ -45,7 +45,7 @@ describe Hbc::CLI::Info, :cask do it "throws away stray options" do expect { Hbc::CLI::Info.run("--notavalidoption", "local-caffeine", "local-transmission") - }.to output(expected_output).to_stdout + }.to raise_error(/invalid option/) end end @@ -102,7 +102,7 @@ describe Hbc::CLI::Info, :cask do it "raises an exception" do expect { Hbc::CLI::Info.run("--notavalidoption") - }.to raise_error(Hbc::CaskUnspecifiedError) + }.to raise_error(/invalid option/) end end end diff --git a/Library/Homebrew/test/cask/cli/install_spec.rb b/Library/Homebrew/test/cask/cli/install_spec.rb index 219b9522e4..b1b26c8675 100644 --- a/Library/Homebrew/test/cask/cli/install_spec.rb +++ b/Library/Homebrew/test/cask/cli/install_spec.rb @@ -40,7 +40,7 @@ describe Hbc::CLI::Install, :cask do end expect { - Hbc::CLI::Install.run("local-transmission", "") + Hbc::CLI::Install.run("local-transmission") }.to output(/Warning: A Cask for local-transmission is already installed./).to_stderr end @@ -115,7 +115,11 @@ describe Hbc::CLI::Install, :cask do end describe "with an invalid option" do - with_options.call(["--notavalidoption"]) + it "raises an error" do + expect { + Hbc::CLI::Install.run("--notavalidoption") + }.to raise_error(/invalid option/) + end end end end diff --git a/Library/Homebrew/test/cask/cli/options_spec.rb b/Library/Homebrew/test/cask/cli/options_spec.rb index eb0bf1a49d..98eb05f7e9 100644 --- a/Library/Homebrew/test/cask/cli/options_spec.rb +++ b/Library/Homebrew/test/cask/cli/options_spec.rb @@ -1,6 +1,6 @@ describe Hbc::CLI, :cask do it "supports setting the appdir" do - Hbc::CLI.process_options %w[help --appdir=/some/path/foo] + Hbc::CLI.new.process_options("help", "--appdir=/some/path/foo") expect(Hbc.appdir).to eq(Pathname.new("/some/path/foo")) end @@ -8,13 +8,13 @@ describe Hbc::CLI, :cask do it "supports setting the appdir from ENV" do ENV["HOMEBREW_CASK_OPTS"] = "--appdir=/some/path/bar" - Hbc::CLI.process_options %w[help] + Hbc::CLI.new.process_options("help") expect(Hbc.appdir).to eq(Pathname.new("/some/path/bar")) end it "supports setting the prefpanedir" do - Hbc::CLI.process_options %w[help --prefpanedir=/some/path/foo] + Hbc::CLI.new.process_options("help", "--prefpanedir=/some/path/foo") expect(Hbc.prefpanedir).to eq(Pathname.new("/some/path/foo")) end @@ -22,13 +22,13 @@ describe Hbc::CLI, :cask do it "supports setting the prefpanedir from ENV" do ENV["HOMEBREW_CASK_OPTS"] = "--prefpanedir=/some/path/bar" - Hbc::CLI.process_options %w[help] + Hbc::CLI.new.process_options("help") expect(Hbc.prefpanedir).to eq(Pathname.new("/some/path/bar")) end it "supports setting the qlplugindir" do - Hbc::CLI.process_options %w[help --qlplugindir=/some/path/foo] + Hbc::CLI.new.process_options("help", "--qlplugindir=/some/path/foo") expect(Hbc.qlplugindir).to eq(Pathname.new("/some/path/foo")) end @@ -36,13 +36,13 @@ describe Hbc::CLI, :cask do it "supports setting the qlplugindir from ENV" do ENV["HOMEBREW_CASK_OPTS"] = "--qlplugindir=/some/path/bar" - Hbc::CLI.process_options %w[help] + Hbc::CLI.new.process_options("help") expect(Hbc.qlplugindir).to eq(Pathname.new("/some/path/bar")) end it "supports setting the colorpickerdir" do - Hbc::CLI.process_options %w[help --colorpickerdir=/some/path/foo] + Hbc::CLI.new.process_options("help", "--colorpickerdir=/some/path/foo") expect(Hbc.colorpickerdir).to eq(Pathname.new("/some/path/foo")) end @@ -50,13 +50,13 @@ describe Hbc::CLI, :cask do it "supports setting the colorpickerdir from ENV" do ENV["HOMEBREW_CASK_OPTS"] = "--colorpickerdir=/some/path/bar" - Hbc::CLI.process_options %w[help] + Hbc::CLI.new.process_options("help") expect(Hbc.colorpickerdir).to eq(Pathname.new("/some/path/bar")) end it "supports setting the dictionarydir" do - Hbc::CLI.process_options %w[help --dictionarydir=/some/path/foo] + Hbc::CLI.new.process_options("help", "--dictionarydir=/some/path/foo") expect(Hbc.dictionarydir).to eq(Pathname.new("/some/path/foo")) end @@ -64,13 +64,13 @@ describe Hbc::CLI, :cask do it "supports setting the dictionarydir from ENV" do ENV["HOMEBREW_CASK_OPTS"] = "--dictionarydir=/some/path/bar" - Hbc::CLI.process_options %w[help] + Hbc::CLI.new.process_options("help") expect(Hbc.dictionarydir).to eq(Pathname.new("/some/path/bar")) end it "supports setting the fontdir" do - Hbc::CLI.process_options %w[help --fontdir=/some/path/foo] + Hbc::CLI.new.process_options("help", "--fontdir=/some/path/foo") expect(Hbc.fontdir).to eq(Pathname.new("/some/path/foo")) end @@ -78,13 +78,13 @@ describe Hbc::CLI, :cask do it "supports setting the fontdir from ENV" do ENV["HOMEBREW_CASK_OPTS"] = "--fontdir=/some/path/bar" - Hbc::CLI.process_options %w[help] + Hbc::CLI.new.process_options("help") expect(Hbc.fontdir).to eq(Pathname.new("/some/path/bar")) end it "supports setting the servicedir" do - Hbc::CLI.process_options %w[help --servicedir=/some/path/foo] + Hbc::CLI.new.process_options("help", "--servicedir=/some/path/foo") expect(Hbc.servicedir).to eq(Pathname.new("/some/path/foo")) end @@ -92,42 +92,22 @@ describe Hbc::CLI, :cask do it "supports setting the servicedir from ENV" do ENV["HOMEBREW_CASK_OPTS"] = "--servicedir=/some/path/bar" - Hbc::CLI.process_options %w[help] + Hbc::CLI.new.process_options("help") expect(Hbc.servicedir).to eq(Pathname.new("/some/path/bar")) end it "allows additional options to be passed through" do - rest = Hbc::CLI.process_options %w[edit foo --create --appdir=/some/path/qux] + rest = Hbc::CLI.new.process_options("edit", "foo", "--create", "--appdir=/some/path/qux") expect(Hbc.appdir).to eq(Pathname.new("/some/path/qux")) expect(rest).to eq(%w[edit foo --create]) end - describe "when a mandatory argument is missing" do - it "shows a user-friendly error message" do - expect { - Hbc::CLI.process_options %w[install -f] - }.to raise_error(ArgumentError) - end - end - - describe "given an ambiguous option" do - it "shows a user-friendly error message" do - expect { - Hbc::CLI.process_options %w[edit -c] - }.to raise_error(ArgumentError) - end - end - describe "--help" do it "sets the Cask help method to true" do - begin - Hbc::CLI.process_options %w[foo --help] - expect(Hbc::CLI.help?).to be true - ensure - Hbc::CLI.help = false - end + command = Hbc::CLI.new("foo", "--help") + expect(command.help?).to be true end end end diff --git a/Library/Homebrew/test/cask/cli/outdated_spec.rb b/Library/Homebrew/test/cask/cli/outdated_spec.rb index a0f13009de..3d9e9bdebd 100644 --- a/Library/Homebrew/test/cask/cli/outdated_spec.rb +++ b/Library/Homebrew/test/cask/cli/outdated_spec.rb @@ -13,13 +13,13 @@ describe Hbc::CLI::Outdated, :cask do shutup do installed.each { |cask| InstallHelper.install_with_caskfile(cask) } end - allow(Hbc::CLI).to receive(:verbose?).and_return(true) + allow_any_instance_of(described_class).to receive(:verbose?).and_return(true) end describe 'without --greedy it ignores the Casks with "vesion latest" or "auto_updates true"' do it "checks all the installed Casks when no token is provided" do expect { - Hbc::CLI::Outdated.run + described_class.run }.to output(<<-EOS.undent).to_stdout local-caffeine (1.2.2) != 1.2.3 local-transmission (2.60) != 2.61 @@ -28,7 +28,7 @@ describe Hbc::CLI::Outdated, :cask do it "checks only the tokens specified in the command line" do expect { - Hbc::CLI::Outdated.run("local-caffeine") + described_class.run("local-caffeine") }.to output(<<-EOS.undent).to_stdout local-caffeine (1.2.2) != 1.2.3 EOS @@ -36,26 +36,32 @@ describe Hbc::CLI::Outdated, :cask do it 'ignores "auto_updates" and "latest" Casks even when their tokens are provided in the command line' do expect { - Hbc::CLI::Outdated.run("local-caffeine", "auto-updates", "version-latest-string") + described_class.run("local-caffeine", "auto-updates", "version-latest-string") }.to output(<<-EOS.undent).to_stdout local-caffeine (1.2.2) != 1.2.3 EOS end end - it "lists only the names (no versions) of the outdated Casks with --quiet" do - expect { - Hbc::CLI::Outdated.run("--quiet") - }.to output(<<-EOS.undent).to_stdout - local-caffeine - local-transmission - EOS + describe "--quiet overrides --verbose" do + before do + allow_any_instance_of(described_class).to receive(:verbose?).and_call_original + end + + it "lists only the names (no versions) of the outdated Casks with --quiet" do + expect { + described_class.run("--verbose", "--quiet") + }.to output(<<-EOS.undent).to_stdout + local-caffeine + local-transmission + EOS + end end describe "with --greedy it checks additional Casks" do it 'includes the Casks with "auto_updates true" or "version latest" with --greedy' do expect { - Hbc::CLI::Outdated.run("--greedy") + described_class.run("--greedy") }.to output(<<-EOS.undent).to_stdout auto-updates (2.57) != 2.61 local-caffeine (1.2.2) != 1.2.3 @@ -69,7 +75,7 @@ describe Hbc::CLI::Outdated, :cask do InstallHelper.install_with_caskfile(cask) expect { - Hbc::CLI::Outdated.run("--greedy") + described_class.run("--greedy") }.to output(<<-EOS.undent).to_stdout local-caffeine (1.2.2) != 1.2.3 local-transmission (2.60) != 2.61 diff --git a/Library/Homebrew/test/cask/cli/style_spec.rb b/Library/Homebrew/test/cask/cli/style_spec.rb index 1302c8352f..7d250c1669 100644 --- a/Library/Homebrew/test/cask/cli/style_spec.rb +++ b/Library/Homebrew/test/cask/cli/style_spec.rb @@ -77,7 +77,7 @@ describe Hbc::CLI::Style, :cask do subject { cli.cask_paths } before do - allow(described_class).to receive(:cask_tokens_from).and_return(tokens) + allow(cli).to receive(:args).and_return(tokens) end context "when no cask tokens are given" do @@ -147,33 +147,4 @@ describe Hbc::CLI::Style, :cask do expect(subject).to include("--auto-correct", *default_args) end end - - describe "#fix?" do - subject { cli.fix? } - - context "when --fix is passed as an argument" do - let(:args) { ["adium", "--fix"] } - it { is_expected.to be_truthy } - end - - context "when --correct is passed as an argument" do - let(:args) { ["adium", "--correct"] } - it { is_expected.to be_truthy } - end - - context "when --auto-correct is passed as an argument" do - let(:args) { ["adium", "--auto-correct"] } - it { is_expected.to be_truthy } - end - - context "when --auto-correct is misspelled as --autocorrect" do - let(:args) { ["adium", "--autocorrect"] } - it { is_expected.to be_truthy } - end - - context "when no flag equivalent to --fix is passed as an argument" do - let(:args) { ["adium"] } - it { is_expected.to be_falsey } - end - end end diff --git a/Library/Homebrew/test/cask/cli/uninstall_spec.rb b/Library/Homebrew/test/cask/cli/uninstall_spec.rb index 4089c47b46..bc1f526134 100644 --- a/Library/Homebrew/test/cask/cli/uninstall_spec.rb +++ b/Library/Homebrew/test/cask/cli/uninstall_spec.rb @@ -203,7 +203,7 @@ describe Hbc::CLI::Uninstall, :cask do it "raises an exception" do expect { Hbc::CLI::Uninstall.run("--notavalidoption") - }.to raise_error(Hbc::CaskUnspecifiedError) + }.to raise_error(/invalid option/) end end end diff --git a/Library/Homebrew/test/cask/cli/zap_spec.rb b/Library/Homebrew/test/cask/cli/zap_spec.rb index 58992deb53..f3af0e66f3 100644 --- a/Library/Homebrew/test/cask/cli/zap_spec.rb +++ b/Library/Homebrew/test/cask/cli/zap_spec.rb @@ -18,8 +18,7 @@ describe Hbc::CLI::Zap, :cask do expect(transmission).to be_installed shutup do - Hbc::CLI::Zap.run("--notavalidoption", - "local-caffeine", "local-transmission") + Hbc::CLI::Zap.run("local-caffeine", "local-transmission") end expect(caffeine).not_to be_installed @@ -67,7 +66,7 @@ describe Hbc::CLI::Zap, :cask do it "raises an exception" do expect { Hbc::CLI::Zap.run("--notavalidoption") - }.to raise_error(Hbc::CaskUnspecifiedError) + }.to raise_error(/invalid option/) end end end diff --git a/Library/Homebrew/test/cask/cli_spec.rb b/Library/Homebrew/test/cask/cli_spec.rb index 9bf14fd892..d5bfba754d 100644 --- a/Library/Homebrew/test/cask/cli_spec.rb +++ b/Library/Homebrew/test/cask/cli_spec.rb @@ -13,7 +13,7 @@ describe Hbc::CLI, :cask do ]) end - context ".process" do + context "::run" do let(:noop_command) { double("CLI::Noop") } before do @@ -30,37 +30,35 @@ describe Hbc::CLI, :cask do version_command = double("CLI::Version") allow(described_class).to receive(:lookup_command).with("--version").and_return(version_command) expect(described_class).to receive(:run_command).with(version_command) - described_class.process(["--version"]) + described_class.run("--version") end it "prints help output when subcommand receives `--help` flag" do - 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 + command = Hbc::CLI.new("noop", "--help") + expect(described_class).to receive(:run_command).with("help") + command.run + expect(command.help?).to eq(true) end it "respects the env variable when choosing what appdir to create" do allow(ENV).to receive(:[]) allow(ENV).to receive(:[]).with("HOMEBREW_CASK_OPTS").and_return("--appdir=/custom/appdir") expect(Hbc).to receive(:appdir=).with(Pathname.new("/custom/appdir")) - described_class.process("noop") + described_class.run("noop") end it "respects the env variable when choosing a non-default Caskroom location" do allow(ENV).to receive(:[]) allow(ENV).to receive(:[]).with("HOMEBREW_CASK_OPTS").and_return("--caskroom=/custom/caskdir") expect(Hbc).to receive(:caskroom=).with(Pathname.new("/custom/caskdir")) - described_class.process("noop") + described_class.run("noop") end it "exits with a status of 1 when something goes wrong" do allow(described_class).to receive(:lookup_command).and_raise(Hbc::CaskError) - expect(described_class).to receive(:exit).with(1) - described_class.process("noop") + command = Hbc::CLI.new("noop") + expect(command).to receive(:exit).with(1) + command.run end end