Module: Utils::Path Private

Included in:
Cask::DSL, Cask::DSL::Base, Formula
Defined in:
utils/path.rb

Overview

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

Helpers for Homebrew path handling and package path validation.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.child_of?(parent, child) ⇒ 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)


10
11
12
13
14
15
# File 'utils/path.rb', line 10

def self.child_of?(parent, child)
  parent_pathname = Pathname(parent).expand_path
  child_pathname = Pathname(child).expand_path
  child_pathname.ascend { |p| return true if p == parent_pathname }
  false
end

.formula_any_version_installed?(formula_names) ⇒ Boolean

Whether any installed keg for one or more formula names has an install receipt.

Parameters:

Returns:

  • (Boolean)


96
97
98
# File 'utils/path.rb', line 96

def self.formula_any_version_installed?(formula_names)
  formula_installed_prefixes(formula_names).any? { |keg| (keg/"INSTALL_RECEIPT.json").file? }
end

.formula_installed_prefixes(formula_names) ⇒ Array<Pathname>

The installed prefix directories for one or more formula names.

Parameters:

Returns:



85
86
87
88
89
90
# File 'utils/path.rb', line 85

def self.formula_installed_prefixes(formula_names)
  Array(formula_names).map { |formula_name| HOMEBREW_CELLAR/Utils.name_from_full_name(formula_name) }
                      .select(&:directory?)
                      .flat_map(&:subdirs)
                      .sort_by(&:basename)
end

.formula_opt_bin(formula_name) ⇒ Pathname

The bin directory under the stable install path for a given formula name.

Parameters:

Returns:



37
38
39
# File 'utils/path.rb', line 37

def self.formula_opt_bin(formula_name)
  formula_opt_prefix(formula_name)/"bin"
end

.formula_opt_bin_env(formula_name, *paths) ⇒ Hash{String => String}

An environment hash with PATH prepended by a formula's stable bin directory.

Parameters:

Returns:



120
121
122
# File 'utils/path.rb', line 120

def self.formula_opt_bin_env(formula_name, *paths)
  { "PATH" => formula_opt_bin_path(formula_name, *paths).to_s }
end

.formula_opt_bin_path(formula_name, *paths) ⇒ PATH

The current PATH with a formula's stable bin directory prepended.

Parameters:

Returns:



112
113
114
# File 'utils/path.rb', line 112

def self.formula_opt_bin_path(formula_name, *paths)
  PATH.new(formula_opt_bin(formula_name), *paths, ENV.fetch("PATH"))
end

.formula_opt_lib(formula_name) ⇒ Pathname

The lib directory under the stable install path for a given formula name.

Parameters:

Returns:



53
54
55
# File 'utils/path.rb', line 53

def self.formula_opt_lib(formula_name)
  formula_opt_prefix(formula_name)/"lib"
end

.formula_opt_libexec(formula_name) ⇒ Pathname

The libexec directory under the stable install path for a given formula name.

Parameters:

Returns:



69
70
71
# File 'utils/path.rb', line 69

def self.formula_opt_libexec(formula_name)
  formula_opt_prefix(formula_name)/"libexec"
end

.formula_opt_prefix(formula_name) ⇒ Pathname

The stable install path for a given formula name.

Parameters:

Returns:



21
22
23
# File 'utils/path.rb', line 21

def self.formula_opt_prefix(formula_name)
  HOMEBREW_PREFIX/"opt/#{Utils.name_from_full_name(formula_name)}"
end

.loadable_package_path?(path, package_type) ⇒ 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)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'utils/path.rb', line 125

def self.loadable_package_path?(path, package_type)
  return true unless Homebrew::EnvConfig.forbid_packages_from_paths?

  path_realpath = path.realpath.to_s
  path_string = path.to_s

  allowed_paths = [trusted_package_root("#{HOMEBREW_LIBRARY}/Taps/")]
  allowed_paths << if package_type == :formula
    trusted_package_root(HOMEBREW_CELLAR)
  else
    trusted_package_root(Cask::Caskroom.path)
  end

  return true if !path_realpath.end_with?(".rb") && !path_string.end_with?(".rb")
  return true if allowed_paths.any? { |path| path_realpath.start_with?(path) }
  return true if allowed_paths.any? { |path| path_string.start_with?(path) }

  # Looks like a local path, Ruby file and not a tap.
  if path_string.include?("./") || path_string.end_with?(".rb") || path_string.count("/") != 2
    package_type_plural = Utils.pluralize(package_type.to_s, 2)
    path_realpath_if_different = " (#{path_realpath})" if path_realpath != path_string
    create_flag = " --cask" if package_type == :cask

    raise <<~WARNING
      Homebrew requires #{package_type_plural} to be in a tap, rejecting:
        #{path_string}#{path_realpath_if_different}

      To create a tap, run e.g.
        brew tap-new <user|org>/<repository>
      To create a #{package_type} in a tap run e.g.
        brew create#{create_flag} <url> --tap=<user|org>/<repository>
    WARNING
  else
    # Looks like a tap, let's quietly reject but not error.
    path_string.count("/") != 2
  end
end

Instance Method Details

#formula_any_version_installed?(formula_names) ⇒ Boolean

Whether any installed keg for one or more formula names has an install receipt.

Parameters:

Returns:

  • (Boolean)


104
105
106
# File 'utils/path.rb', line 104

def formula_any_version_installed?(formula_names)
  Utils::Path.formula_any_version_installed?(formula_names)
end

#formula_opt_bin(formula_name) ⇒ Pathname

The bin directory under the stable install path for a given formula name.

Parameters:

Returns:



45
46
47
# File 'utils/path.rb', line 45

def formula_opt_bin(formula_name)
  Utils::Path.formula_opt_bin(formula_name)
end

#formula_opt_lib(formula_name) ⇒ Pathname

The lib directory under the stable install path for a given formula name.

Parameters:

Returns:



61
62
63
# File 'utils/path.rb', line 61

def formula_opt_lib(formula_name)
  Utils::Path.formula_opt_lib(formula_name)
end

#formula_opt_libexec(formula_name) ⇒ Pathname

The libexec directory under the stable install path for a given formula name.

Parameters:

Returns:



77
78
79
# File 'utils/path.rb', line 77

def formula_opt_libexec(formula_name)
  Utils::Path.formula_opt_libexec(formula_name)
end

#formula_opt_prefix(formula_name) ⇒ Pathname

The stable install path for a given formula name.

Parameters:

Returns:



29
30
31
# File 'utils/path.rb', line 29

def formula_opt_prefix(formula_name)
  Utils::Path.formula_opt_prefix(formula_name)
end