aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/cli/uninstall.rb
blob: ae15414b7274e96925d1522ca89e1baff35319ca (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
class Hbc::CLI::Uninstall < Hbc::CLI::Base
  def self.run(*args)
    cask_tokens = cask_tokens_from(args)
    raise Hbc::CaskUnspecifiedError if cask_tokens.empty?
    force = args.include? "--force"

    cask_tokens.each do |cask_token|
      odebug "Uninstalling Cask #{cask_token}"
      cask = Hbc.load(cask_token)

      raise Hbc::CaskNotInstalledError, cask unless cask.installed? || force

      latest_installed_version = cask.timestamped_versions.last

      unless latest_installed_version.nil?
        latest_installed_cask_file = 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
        cask = Hbc.load(latest_installed_cask_file) if latest_installed_cask_file.exist?
      end

      Hbc::Installer.new(cask, force: force).uninstall

      next if (versions = cask.versions).empty?

      single = versions.count == 1

      puts <<-EOS.undent
        #{cask_token} #{versions.join(', ')} #{single ? 'is' : 'are'} still installed.
        Remove #{single ? 'it' : 'them all'} with `brew cask uninstall --force #{cask_token}`.
      EOS
    end
  end

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