From 33580c283a5122424edbfa6a1f70e6c11bf29b36 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 20 May 2017 02:27:13 +0200 Subject: [PATCH] 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