From 55727b789532fbfa7997929aa0506d7843eda3ce Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Sun, 29 Oct 2017 17:31:07 -0300 Subject: [PATCH] Hack a first working version of upgrade --- Library/Homebrew/cask/lib/hbc/cli.rb | 1 + Library/Homebrew/cask/lib/hbc/cli/upgrade.rb | 65 ++++++++++++++++ Library/Homebrew/cask/lib/hbc/installer.rb | 16 +++- .../Homebrew/test/cask/cli/upgrade_spec.rb | 78 +++++++++++++++++++ 4 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 Library/Homebrew/cask/lib/hbc/cli/upgrade.rb create mode 100644 Library/Homebrew/test/cask/cli/upgrade_spec.rb diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index e147c82809..215b59843d 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -21,6 +21,7 @@ require "hbc/cli/reinstall" require "hbc/cli/search" require "hbc/cli/style" require "hbc/cli/uninstall" +require "hbc/cli/upgrade" require "hbc/cli/--version" require "hbc/cli/zap" diff --git a/Library/Homebrew/cask/lib/hbc/cli/upgrade.rb b/Library/Homebrew/cask/lib/hbc/cli/upgrade.rb new file mode 100644 index 0000000000..49a538704b --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/cli/upgrade.rb @@ -0,0 +1,65 @@ +module Hbc + class CLI + class Upgrade < AbstractCommand + option "--greedy", :greedy, false + option "--quiet", :quiet, false + option "--force", :force, false + option "--force-update", :force_update, false + option "--skip-cask-deps", :skip_cask_deps, false + + def initialize(*) + super + self.verbose = ($stdout.tty? || verbose?) && !quiet? + end + + def run + outdated_casks = casks(alternative: -> { Hbc.installed }).find_all { |cask| cask.outdated?(greedy?) } + + if outdated_casks.empty? + oh1 "No packages to upgrade" + else + oh1 "Upgrading #{Formatter.pluralize(outdated_casks.length, "outdated package")}, with result:" + puts outdated_casks.map { |f| "#{f.full_name} #{f.version}" } * ", " + end + + outdated_casks.each do |old_cask| + odebug "Uninstalling Cask #{old_cask}" + + raise CaskNotInstalledError, old_cask unless old_cask.installed? || force? + + unless old_cask.installed_caskfile.nil? + # use the same cask file that was used for installation, if possible + old_cask = CaskLoader.load(old_cask.installed_caskfile) if old_cask.installed_caskfile.exist? + end + + old_cask_installer = Installer.new(old_cask, binaries: binaries?, verbose: verbose?, force: force?, upgrade: true) + + old_cask_installer.uninstall + + begin + odebug "Installing new version of Cask #{old_cask}" + + new_cask = CaskLoader.load(old_cask.to_s) + + Installer.new(new_cask, binaries: binaries?, + verbose: verbose?, + force: force?, + skip_cask_deps: skip_cask_deps?, + require_sha: require_sha?, + upgrade: true).install + + old_cask_installer.finalize_upgrade + rescue CaskUnavailableError => e + opoo e.message + rescue CaskAlreadyInstalledError => e + opoo e.message + end + end + end + + def self.help + "upgrades all outdated casks" + end + end + end +end diff --git a/Library/Homebrew/cask/lib/hbc/installer.rb b/Library/Homebrew/cask/lib/hbc/installer.rb index 1063f488bc..629a20f310 100644 --- a/Library/Homebrew/cask/lib/hbc/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/installer.rb @@ -19,7 +19,7 @@ module Hbc PERSISTENT_METADATA_SUBDIRS = ["gpg"].freeze - def initialize(cask, command: SystemCommand, force: false, skip_cask_deps: false, binaries: true, verbose: false, require_sha: false) + def initialize(cask, command: SystemCommand, force: false, skip_cask_deps: false, binaries: true, verbose: false, require_sha: false, upgrade: false) @cask = cask @command = command @force = force @@ -28,6 +28,7 @@ module Hbc @verbose = verbose @require_sha = require_sha @reinstall = false + @upgrade = upgrade end attr_predicate :binaries?, :force?, :skip_cask_deps?, :require_sha?, :verbose? @@ -82,7 +83,7 @@ module Hbc def install odebug "Hbc::Installer#install" - if @cask.installed? && !force? && !@reinstall + if @cask.installed? && !force? && !@reinstall && !@upgrade raise CaskAlreadyInstalledError, @cask end @@ -129,7 +130,7 @@ module Hbc installed_cask = installed_caskfile.exist? ? CaskLoader.load(installed_caskfile) : @cask # Always force uninstallation, ignore method parameter - Installer.new(installed_cask, binaries: binaries?, verbose: verbose?, force: true).uninstall + Installer.new(installed_cask, binaries: binaries?, verbose: verbose?, force: true, upgrade: @upgrade).uninstall end def summary @@ -368,6 +369,15 @@ module Hbc oh1 "Uninstalling Cask #{@cask}" disable_accessibility_access uninstall_artifacts + return if @upgrade + + purge_versioned_files + purge_caskroom_path if force? + end + + def finalize_upgrade + return unless @upgrade + purge_versioned_files purge_caskroom_path if force? end diff --git a/Library/Homebrew/test/cask/cli/upgrade_spec.rb b/Library/Homebrew/test/cask/cli/upgrade_spec.rb new file mode 100644 index 0000000000..532541ba7a --- /dev/null +++ b/Library/Homebrew/test/cask/cli/upgrade_spec.rb @@ -0,0 +1,78 @@ +require_relative "shared_examples/invalid_option" + +describe Hbc::CLI::Upgrade, :cask do + let(:installed) do + [ + Hbc::CaskLoader.load(cask_path("outdated/local-caffeine")), + Hbc::CaskLoader.load(cask_path("outdated/local-transmission")), + Hbc::CaskLoader.load(cask_path("outdated/auto-updates")), + ] + end + + it_behaves_like "a command that handles invalid options" + + before(:example) do + installed.each { |cask| InstallHelper.install_with_caskfile(cask) } + + allow_any_instance_of(described_class).to receive(:verbose?).and_return(true) + end + + describe 'without --greedy it ignores the Casks with "version latest" or "auto_updates true"' do + it "updates all the installed Casks when no token is provided" do + described_class.run + + expect(Hbc::CaskLoader.load("local-caffeine")).to be_installed + expect(Hbc.appdir.join("Caffeine.app")).to be_a_directory + expect(Hbc::CaskLoader.load("local-caffeine").versions).to include("1.2.3") + + expect(Hbc::CaskLoader.load("local-transmission")).to be_installed + expect(Hbc.appdir.join("Transmission.app")).to be_a_directory + expect(Hbc::CaskLoader.load("local-transmission").versions).to include("2.61") + end + + it "updates only the Casks specified in the command line" do + expect(Hbc::CaskLoader.load("local-caffeine").versions).to include("1.2.2") + expect(Hbc::CaskLoader.load("local-transmission").versions).to include("2.60") + + described_class.run("local-caffeine") + + expect(Hbc::CaskLoader.load("local-caffeine").versions).to include("1.2.3") + expect(Hbc::CaskLoader.load("local-caffeine").versions).to_not include("1.2.2") + expect(Hbc::CaskLoader.load("local-transmission").versions).to include("2.60") + end + + it 'ignores "auto_updates" and "latest" Casks even when their tokens are provided in the command line' do + expect(Hbc::CaskLoader.load("local-caffeine").versions).to include("1.2.2") + expect(Hbc::CaskLoader.load("auto-updates").versions).to include("2.57") + + described_class.run("local-caffeine", "auto-updates", "version-latest-string") + + expect(Hbc::CaskLoader.load("local-caffeine").versions).to include("1.2.3") + expect(Hbc::CaskLoader.load("auto-updates").versions).to include("2.57") + 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::CaskLoader.load("auto-updates").versions).to include("2.57") + expect(Hbc::CaskLoader.load("local-caffeine").versions).to include("1.2.2") + expect(Hbc::CaskLoader.load("local-transmission").versions).to include("2.60") + + described_class.run("--greedy") + + expect(Hbc::CaskLoader.load("auto-updates").versions).to include("2.61") + expect(Hbc::CaskLoader.load("local-caffeine").versions).to include("1.2.3") + expect(Hbc::CaskLoader.load("local-transmission").versions).to include("2.61") + end + + it 'does not include the Casks with "auto_updates true" when the version did not change' do + cask = Hbc::CaskLoader.load(cask_path("auto-updates")) + InstallHelper.install_with_caskfile(cask) + expect(Hbc::CaskLoader.load("auto-updates").versions).to include("2.61") + + described_class.run("auto-updates", "--greedy") + + expect(Hbc::CaskLoader.load("auto-updates").versions).to include("2.61") + end + end +end