Class: Homebrew::Cmd::WhichFormula Private

Inherits:
AbstractCommand show all
Defined in:
cmd/which-formula.rb,
sorbet/rbi/dsl/homebrew/cmd/which_formula.rbi

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

Defined Under Namespace

Classes: Args

Constant Summary collapse

ENDPOINT =

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

"internal/executables.txt"
DATABASE_FILE =

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

T.let((Homebrew::API::HOMEBREW_CACHE_API/ENDPOINT).freeze, Pathname)

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Methods included from Utils::Output::Mixin

#odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::Cmd::WhichFormula::Args

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.



10
# File 'sorbet/rbi/dsl/homebrew/cmd/which_formula.rbi', line 10

def args; end

#download_and_cache_executables_file!(skip_update: false) ⇒ 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:

  • skip_update (Boolean) (defaults to: false)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'cmd/which-formula.rb', line 37

def download_and_cache_executables_file!(skip_update: false)
  if DATABASE_FILE.exist? && !DATABASE_FILE.empty? &&
     (skip_update || (Time.now - Homebrew::EnvConfig.api_auto_update_secs.to_i) < DATABASE_FILE.mtime)
    return
  end

  url = "#{Homebrew::EnvConfig.api_domain}/#{ENDPOINT}"

  if ENV.fetch("CI", false)
    max_time = nil # allow more time in CI
    retries = Homebrew::EnvConfig.curl_retries.to_i
  else
    max_time = 10 # seconds
    retries = nil # do not retry by default
  end

  args = Utils::Curl.curl_args(max_time:, retries:)
  args += %W[
    --compressed
    --speed-limit #{ENV.fetch("HOMEBREW_CURL_SPEED_LIMIT")}
    --speed-time #{ENV.fetch("HOMEBREW_CURL_SPEED_TIME")}
  ]
  args.prepend("--time-cond", DATABASE_FILE.to_s) if DATABASE_FILE.exist? && !DATABASE_FILE.empty?

  Utils::Curl.curl_download(*args, url, to: DATABASE_FILE)
  FileUtils.touch(DATABASE_FILE, mtime: Time.now)
end

#explain_formulae_install(cmd, formulae) ⇒ 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.

Output explanation of how to get 'cmd' by installing one of the providing formulae.

Parameters:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'cmd/which-formula.rb', line 84

def explain_formulae_install(cmd, formulae)
  formulae.reject! { |f| reject_formula? f }

  return if formulae.blank?

  if formulae.size == 1
    puts <<~EOS
      The program '#{cmd}' is currently not installed. You can install it by typing:
        brew install #{formulae.first}
    EOS
  else
    puts <<~EOS
      The program '#{cmd}' can be found in the following formulae:
        * #{formulae * "\n  * "}
      Try: brew install <selected formula>
    EOS
  end
end

#matches(cmd) ⇒ Array<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:



66
67
68
# File 'cmd/which-formula.rb', line 66

def matches(cmd)
  DATABASE_FILE.readlines.select { |line| line.include?(cmd) }.map(&:chomp)
end

#reject_formula?(name) ⇒ 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.

Test if we have to reject the given formula, i.e. not suggest it.

Parameters:

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
# File 'cmd/which-formula.rb', line 72

def reject_formula?(name)
  f = begin
    Formula[name]
  rescue
    nil
  end
  f.nil? || f.latest_version_installed? || f.requirements.any? { |r| r.required? && !r.satisfied? }
end

#runvoid

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.



28
29
30
31
32
33
34
# File 'cmd/which-formula.rb', line 28

def run
  # NOTE: It probably doesn't make sense to use that on multiple commands since
  # each one might print multiple formulae
  args.named.each do |command|
    which_formula command, explain: args.explain?, skip_update: args.skip_update?
  end
end

#which_formula(cmd, explain: false, skip_update: false) ⇒ 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.

if 'explain' is false, print all formulae that can be installed to get the given command. If it's true, print them in human-readable form with an help text.

Parameters:

  • cmd (String)
  • explain (Boolean) (defaults to: false)
  • skip_update (Boolean) (defaults to: false)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'cmd/which-formula.rb', line 107

def which_formula(cmd, explain: false, skip_update: false)
  download_and_cache_executables_file!(skip_update: skip_update)

  cmd = cmd.downcase

  formulae = (matches cmd).filter_map do |m|
    formula, cmds_text = m.split(":", 2)
    next if formula.nil? || cmds_text.nil?

    cmds = cmds_text.split
    formula if !cmds.nil? && cmds.include?(cmd)
  end

  return if formulae.blank?

  formulae.map! do |formula|
    formula.sub(/\(.*\)$/, "")
  end

  if explain
    explain_formulae_install(cmd, formulae)
  else
    puts formulae * "\n"
  end
end