Module: Utils::Path Private

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

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)


8
9
10
11
12
13
# File 'utils/path.rb', line 8

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

.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)


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
# File 'utils/path.rb', line 16

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