|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# License: MIT |
| 5 | +# The license text can be found in Library/Homebrew/command-not-found/LICENSE |
| 6 | + |
| 7 | +require "abstract_command" |
| 8 | +require "api" |
| 9 | + |
| 10 | +module Homebrew |
| 11 | + module Cmd |
| 12 | + class WhichFormula < AbstractCommand |
| 13 | + ENDPOINT = "internal/executables.txt" |
| 14 | + DATABASE_FILE = T.let((Homebrew::API::HOMEBREW_CACHE_API/ENDPOINT).freeze, Pathname) |
| 15 | + |
| 16 | + cmd_args do |
| 17 | + description <<~EOS |
| 18 | + Show which formula(e) provides the given command. |
| 19 | + EOS |
| 20 | + switch "--explain", |
| 21 | + description: "Output explanation of how to get <command> by installing one of the providing formulae." |
| 22 | + switch "--skip-update", |
| 23 | + description: "Skip updating the executables database if any version exists on disk, no matter how old." |
| 24 | + named_args :command, min: 1 |
| 25 | + end |
| 26 | + |
| 27 | + sig { override.void } |
| 28 | + def run |
| 29 | + # NOTE: It probably doesn't make sense to use that on multiple commands since |
| 30 | + # each one might print multiple formulae |
| 31 | + args.named.each do |command| |
| 32 | + which_formula command, explain: args.explain?, skip_update: args.skip_update? |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + sig { params(skip_update: T::Boolean).void } |
| 37 | + def download_and_cache_executables_file!(skip_update: false) |
| 38 | + if DATABASE_FILE.exist? && !DATABASE_FILE.empty? && |
| 39 | + (skip_update || (Time.now - Homebrew::EnvConfig.api_auto_update_secs.to_i) < DATABASE_FILE.mtime) |
| 40 | + return |
| 41 | + end |
| 42 | + |
| 43 | + url = "#{Homebrew::EnvConfig.api_domain}/#{ENDPOINT}" |
| 44 | + |
| 45 | + if ENV.fetch("CI", false) |
| 46 | + max_time = nil # allow more time in CI |
| 47 | + retries = Homebrew::EnvConfig.curl_retries.to_i |
| 48 | + else |
| 49 | + max_time = 10 # seconds |
| 50 | + retries = nil # do not retry by default |
| 51 | + end |
| 52 | + |
| 53 | + args = Utils::Curl.curl_args(max_time:, retries:) |
| 54 | + args += %W[ |
| 55 | + --compressed |
| 56 | + --speed-limit #{ENV.fetch("HOMEBREW_CURL_SPEED_LIMIT")} |
| 57 | + --speed-time #{ENV.fetch("HOMEBREW_CURL_SPEED_TIME")} |
| 58 | + ] |
| 59 | + args.prepend("--time-cond", DATABASE_FILE.to_s) if DATABASE_FILE.exist? && !DATABASE_FILE.empty? |
| 60 | + |
| 61 | + Utils::Curl.curl_download(*args, url, to: DATABASE_FILE) |
| 62 | + FileUtils.touch(DATABASE_FILE, mtime: Time.now) |
| 63 | + end |
| 64 | + |
| 65 | + sig { params(cmd: String).returns(T::Array[String]) } |
| 66 | + def matches(cmd) |
| 67 | + DATABASE_FILE.readlines.select { |line| line.include?(cmd) }.map(&:chomp) |
| 68 | + end |
| 69 | + |
| 70 | + # Test if we have to reject the given formula, i.e. not suggest it. |
| 71 | + sig { params(name: String).returns(T::Boolean) } |
| 72 | + def reject_formula?(name) |
| 73 | + f = begin |
| 74 | + Formula[name] |
| 75 | + rescue |
| 76 | + nil |
| 77 | + end |
| 78 | + f.nil? || f.latest_version_installed? || f.requirements.any? { |r| r.required? && !r.satisfied? } |
| 79 | + end |
| 80 | + |
| 81 | + # Output explanation of how to get 'cmd' by installing one of the providing |
| 82 | + # formulae. |
| 83 | + sig { params(cmd: String, formulae: T::Array[String]).void } |
| 84 | + def explain_formulae_install(cmd, formulae) |
| 85 | + formulae.reject! { |f| reject_formula? f } |
| 86 | + |
| 87 | + return if formulae.blank? |
| 88 | + |
| 89 | + if formulae.size == 1 |
| 90 | + puts <<~EOS |
| 91 | + The program '#{cmd}' is currently not installed. You can install it by typing: |
| 92 | + brew install #{formulae.first} |
| 93 | + EOS |
| 94 | + else |
| 95 | + puts <<~EOS |
| 96 | + The program '#{cmd}' can be found in the following formulae: |
| 97 | + * #{formulae * "\n * "} |
| 98 | + Try: brew install <selected formula> |
| 99 | + EOS |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + # if 'explain' is false, print all formulae that can be installed to get the |
| 104 | + # given command. If it's true, print them in human-readable form with an help |
| 105 | + # text. |
| 106 | + sig { params(cmd: String, explain: T::Boolean, skip_update: T::Boolean).void } |
| 107 | + def which_formula(cmd, explain: false, skip_update: false) |
| 108 | + download_and_cache_executables_file!(skip_update: skip_update) |
| 109 | + |
| 110 | + cmd = cmd.downcase |
| 111 | + |
| 112 | + formulae = (matches cmd).filter_map do |m| |
| 113 | + formula, cmds_text = m.split(":", 2) |
| 114 | + next if formula.nil? || cmds_text.nil? |
| 115 | + |
| 116 | + cmds = cmds_text.split |
| 117 | + formula if !cmds.nil? && cmds.include?(cmd) |
| 118 | + end |
| 119 | + |
| 120 | + return if formulae.blank? |
| 121 | + |
| 122 | + formulae.map! do |formula| |
| 123 | + formula.sub(/\(.*\)$/, "") |
| 124 | + end |
| 125 | + |
| 126 | + if explain |
| 127 | + explain_formulae_install(cmd, formulae) |
| 128 | + else |
| 129 | + puts formulae * "\n" |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments