aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/cli/reinstall.rb
blob: 3560a4795b391603080156d40248a93dd61cd2ab (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
module Hbc
  class CLI
    class Reinstall < Install
      def self.install_casks(cask_tokens, force, skip_cask_deps, require_sha)
        count = 0
        cask_tokens.each do |cask_token|
          begin
            cask = Hbc.load(cask_token)

            if cask.installed?
              # use copy of cask for uninstallation to avoid 'No such file or directory' bug
              installed_cask = cask
              latest_installed_version = installed_cask.timestamped_versions.last

              unless latest_installed_version.nil?
                latest_installed_cask_file = installed_cask.metadata_master_container_path
                                                           .join(latest_installed_version
                                                           .join(File::Separator),
                                                           "Casks", "#{cask_token}.rb")

                # use the same cask file that was used for installation, if possible
                installed_cask = CaskLoader.load_from_file(latest_installed_cask_file) if latest_installed_cask_file.exist?
              end

              # Always force uninstallation, ignore method parameter
              Installer.new(installed_cask, force: true).uninstall
            end

            Installer.new(cask,
                          force:          force,
                          skip_cask_deps: skip_cask_deps,
                          require_sha:    require_sha).install
            count += 1
          rescue CaskUnavailableError => e
            warn_unavailable_with_suggestion cask_token, e
          rescue CaskNoShasumError => e
            opoo e.message
            count += 1
          end
        end
        count.zero? ? nil : count == cask_tokens.length
      end

      def self.help
        "reinstalls the given Cask"
      end
    end
  end
end