aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/cli/upgrade.rb
blob: 808b64c4b7fd675b5768209ed32973f95cb687c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module Hbc
  class CLI
    class Upgrade < AbstractCommand
      option "--greedy", :greedy, false
      option "--quiet",  :quiet, false
      option "--force", :force, 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: lambda {
          Hbc.installed.select do |cask|
            cask.outdated?(greedy?)
          end
        }).select { |cask| cask.outdated?(true) }

        if outdated_casks.empty?
          oh1 "No Casks to upgrade"
          return
        end

        oh1 "Upgrading #{Formatter.pluralize(outdated_casks.length, "outdated package")}, with result:"
        puts outdated_casks.map { |f| "#{f.full_name} #{f.version}" } * ", "

        outdated_casks.each do |old_cask|
          odebug "Started upgrade process for Cask #{old_cask}"
          raise CaskNotInstalledError, old_cask unless old_cask.installed? || force?

          raise CaskUnavailableError.new(old_cask, "The Caskfile is missing!") if old_cask.installed_caskfile.nil?

          old_cask = CaskLoader.load(old_cask.installed_caskfile)

          old_cask_installer = Installer.new(old_cask, binaries: binaries?, verbose: verbose?, force: force?, upgrade: true)

          new_cask = CaskLoader.load(old_cask.to_s)

          new_cask_installer =
            Installer.new(new_cask, binaries:       binaries?,
                                    verbose:        verbose?,
                                    force:          force?,
                                    skip_cask_deps: skip_cask_deps?,
                                    require_sha:    require_sha?,
                                    upgrade: true)

          started_upgrade = false
          new_artifacts_installed = false

          begin
            # Start new Cask's installation steps
            new_cask_installer.check_conflicts

            new_cask_installer.fetch

            # Move the old Cask's artifacts back to staging
            old_cask_installer.start_upgrade
            # And flag it so in case of error
            started_upgrade = true

            # Install the new Cask
            new_cask_installer.stage

            new_cask_installer.install_artifacts
            new_artifacts_installed = true

            new_cask_installer.enable_accessibility_access

            # If successful, wipe the old Cask from staging
            old_cask_installer.finalize_upgrade
          rescue CaskError => e
            new_cask_installer.uninstall_artifacts if new_artifacts_installed
            new_cask_installer.purge_versioned_files
            old_cask_installer.revert_upgrade if started_upgrade
            raise e
          end
        end
      end

      def self.help
        "upgrades all outdated casks"
      end
    end
  end
end