Module: Cask::Caskroom

Extended by:
Utils::Output::Mixin
Defined in:
cask/caskroom.rb

Overview

This module is part of an internal API. This module may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Helper functions for interacting with the Caskroom directory.

Constant Summary collapse

CASKFILE_EXTENSIONS =

This constant is part of an internal API. This constant may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%w[internal.json json rb].freeze

Class Method Summary collapse

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_upgradable

Class Method Details

.any_casks_installed?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


37
38
39
# File 'cask/caskroom.rb', line 37

def self.any_casks_installed?
  paths.any?
end

.cask_installed?(token) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


42
43
44
# File 'cask/caskroom.rb', line 42

def self.cask_installed?(token)
  !cask_installed_version(token).nil?
end

.cask_installed_caskfile(token, old_tokens: []) ⇒ Pathname?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'cask/caskroom.rb', line 47

def self.cask_installed_caskfile(token, old_tokens: [])
  # Check if the cask is installed with an old name.
  [token, *old_tokens].map { |cask_token| token_from_full_token(cask_token) }.uniq.each do |cask_token|
    caskroom_path = path/cask_token
    next if !caskroom_path.directory? || caskroom_path.symlink?

    timestamped_path = Pathname.glob((caskroom_path/".metadata/*/*").to_s).max_by { |p| p.basename.to_s }
    next unless timestamped_path

    caskfile = CASKFILE_EXTENSIONS.map { |ext| timestamped_path/"Casks/#{cask_token}.#{ext}" }
                                  .find(&:exist?)
    return caskfile if caskfile
  end

  nil
end

.cask_installed_version(token, old_tokens: []) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



65
66
67
68
69
# File 'cask/caskroom.rb', line 65

def self.cask_installed_version(token, old_tokens: [])
  return unless (caskfile = cask_installed_caskfile(token, old_tokens:))

  caskfile.dirname.dirname.dirname.basename.to_s
end

.caskroom_group_correct?(path) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


114
115
116
117
118
119
# File 'cask/caskroom.rb', line 114

def self.caskroom_group_correct?(path)
  group = Etc.getgrnam(expected_caskroom_group)
  return false if group.nil?

  path.stat.gid == group.gid
end

.casks(config: nil) ⇒ Array<Cask>

This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Get all installed casks.

Parameters:

  • config (Config, nil) (defaults to: nil)

Returns:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'cask/caskroom.rb', line 130

def self.casks(config: nil)
  tokens.sort.filter_map do |token|
    # This is nested so that the rescue can catch errors from both branches
    begin
      CaskLoader.load_prefer_installed(token, config:, warn: false)
    rescue TapCaskAmbiguityError => e
      e.loaders.fetch(0).load(config:)
    end
  rescue Homebrew::UntrustedTapError
    # If the tap is untrusted the only place we can load the cask from is the installed cask file, if it exists.
    begin
      CaskLoader::FromInstalledPathLoader.try_new(token, warn: false)&.load(config:)
    rescue
      nil
    end
  rescue
    # Don't blow up because of a single unavailable cask.
    nil
  end.select(&:installed?)
end

.chgrp_path(path, sudo) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



109
110
111
# File 'cask/caskroom.rb', line 109

def self.chgrp_path(path, sudo)
  SystemCommand.run("chgrp", args: [expected_caskroom_group, path], sudo:)
end

.corrupt_cask_dirsArray<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Return tokens for Caskroom directories missing expected installed metadata.

Returns:



73
74
75
# File 'cask/caskroom.rb', line 73

def self.corrupt_cask_dirs
  paths.filter_map { |p| p.basename.to_s unless cask_with_metadata?(p) }
end

.ensure_caskroom_existsvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'cask/caskroom.rb', line 91

def self.ensure_caskroom_exists
  return if path.exist?

  sudo = !path.parent.writable?

  if sudo && !ENV.key?("SUDO_ASKPASS") && $stdout.tty?
    ohai "Creating Caskroom directory: #{path}",
         "We'll set permissions properly so we won't need sudo in the future."
  end

  SystemCommand.run("mkdir", args: ["-p", path], sudo:)
  SystemCommand.run("chmod", args: ["g+rwx", path], sudo:)
  SystemCommand.run("chown", args: [User.current.to_s, path], sudo:)

  chgrp_path(path, sudo) unless caskroom_group_correct?(path)
end

.expected_caskroom_groupString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



122
123
124
# File 'cask/caskroom.rb', line 122

def self.expected_caskroom_group
  "admin"
end

.pathPathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



17
18
19
# File 'cask/caskroom.rb', line 17

def self.path
  @path ||= T.let(HOMEBREW_PREFIX/"Caskroom", T.nilable(Pathname))
end

.tokensArray<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Return all tokens for installed casks.

Returns:



32
33
34
# File 'cask/caskroom.rb', line 32

def self.tokens
  paths.map { |path| path.basename.to_s }
end