Refactor brew cask reinstall

The implementation of the reinstall command was the same as
Installer#install, aside from the uninstall of the existing cask.
Moved this within the class to DRY up the implementation.
This commit is contained in:
Joshua McKinney 2017-03-18 17:48:20 -05:00
parent 76955b47bd
commit 367fdb9718
2 changed files with 22 additions and 26 deletions

View File

@ -7,30 +7,11 @@ module Hbc
begin begin
cask = CaskLoader.load(cask_token) cask = CaskLoader.load(cask_token)
installer = Installer.new(cask, Installer.new(cask,
force: force, force: force,
skip_cask_deps: skip_cask_deps, skip_cask_deps: skip_cask_deps,
require_sha: require_sha) require_sha: require_sha,
installer.print_caveats reinstall: true).install
installer.fetch
if cask.installed?
# use copy of cask for uninstallation to avoid 'No such file or directory' bug
installed_cask = cask
# use the same cask file that was used for installation, if possible
if (installed_caskfile = installed_cask.installed_caskfile).exist?
installed_cask = CaskLoader.load_from_file(installed_caskfile)
end
# Always force uninstallation, ignore method parameter
Installer.new(installed_cask, force: true).uninstall
end
installer.stage
installer.install_artifacts
installer.enable_accessibility_access
puts installer.summary
count += 1 count += 1
rescue CaskUnavailableError => e rescue CaskUnavailableError => e

View File

@ -18,12 +18,13 @@ module Hbc
PERSISTENT_METADATA_SUBDIRS = ["gpg"].freeze 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, require_sha: false, reinstall: false)
@cask = cask @cask = cask
@command = command @command = command
@force = force @force = force
@skip_cask_deps = skip_cask_deps @skip_cask_deps = skip_cask_deps
@require_sha = require_sha @require_sha = require_sha
@reinstall = reinstall
end end
def self.print_caveats(cask) def self.print_caveats(cask)
@ -76,13 +77,14 @@ module Hbc
def install def install
odebug "Hbc::Installer#install" odebug "Hbc::Installer#install"
if @cask.installed? && !force if @cask.installed? && !force && !@reinstall
raise CaskAlreadyInstalledAutoUpdatesError, @cask if @cask.auto_updates raise CaskAlreadyInstalledAutoUpdatesError, @cask if @cask.auto_updates
raise CaskAlreadyInstalledError, @cask raise CaskAlreadyInstalledError, @cask
end end
print_caveats print_caveats
fetch fetch
uninstall_if_neccessary
stage stage
install_artifacts install_artifacts
enable_accessibility_access enable_accessibility_access
@ -90,6 +92,19 @@ module Hbc
puts summary puts summary
end end
def uninstall_if_neccessary
return unless @cask.installed? && @reinstall
installed_cask = @cask
# use the same cask file that was used for installation, if possible
if (installed_caskfile = installed_cask.installed_caskfile).exist?
installed_cask = CaskLoader.load_from_file(installed_caskfile)
end
# Always force uninstallation, ignore method parameter
Installer.new(installed_cask, force: true).uninstall
end
def summary def summary
s = "" s = ""
s << "#{Emoji.install_badge} " if Emoji.enabled? s << "#{Emoji.install_badge} " if Emoji.enabled?