Module: Homebrew::MissingFormula Private

Defined in:
missing_formula.rb,
extend/os/mac/missing_formula.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.

Helper module for checking if there is a reason a formula is missing.

Class Method Summary collapse

Class Method Details

.cask_reason(name, silent: false, show_info: false) ⇒ 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:

  • name (String)
  • silent (Boolean) (defaults to: false)
  • show_info (Boolean) (defaults to: false)

Returns:



24
25
26
27
28
# File 'extend/os/mac/missing_formula.rb', line 24

def cask_reason(name, silent: false, show_info: false)
  return if silent

  suggest_command(name, show_info ? "info" : "install")
end

.deleted_reason(name, silent: false) ⇒ 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:

  • name (String)
  • silent (Boolean) (defaults to: false)

Returns:



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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'missing_formula.rb', line 134

def deleted_reason(name, silent: false)
  path = Formulary.path name
  return if File.exist? path

  tap = Tap.from_path(path)
  return if tap.nil? || !File.exist?(tap.path)

  relative_path = path.relative_path_from tap.path

  tap.path.cd do
    unless silent
      ohai "Searching for a previously deleted formula (in the last month)..."
      if (tap.path/".git/shallow").exist?
        opoo <<~EOS
          #{tap} is shallow clone. To get its complete history, run:
            git -C "$(brew --repo #{tap})" fetch --unshallow

        EOS
      end
    end

    # Optimization for the core tap which has many monthly commits
    if tap.core_tap?
      # Check if the formula has been deleted in the last month.
      diff_command = ["git", "diff", "--diff-filter=D", "--name-only",
                      "@{'1 month ago'}", "--", relative_path]
      deleted_formula = Utils.popen_read(*diff_command)

      if deleted_formula.blank?
        ofail "No previously deleted formula found." unless silent
        return
      end
    end

    # Find commit where formula was deleted in the last month.
    log_command = "git log --since='1 month ago' --diff-filter=D " \
                  "--name-only --max-count=1 " \
                  "--format=%H\\\\n%h\\\\n%B -- #{relative_path}"
    hash, short_hash, *commit_message, relative_path =
      Utils.popen_read(log_command).gsub("\\n", "\n").lines.map(&:chomp)

    if hash.blank? || short_hash.blank? || relative_path.blank?
      ofail "No previously deleted formula found." unless silent
      return
    end

    commit_message = commit_message.reject(&:empty?).join("\n  ")

    commit_message.sub!(/ \(#(\d+)\)$/, " (#{tap.issues_url}/\\1)")
    commit_message.gsub!(/(Closes|Fixes) #(\d+)/, "\\1 #{tap.issues_url}/\\2")

    <<~EOS
      #{name} was deleted from #{tap.name} in commit #{short_hash}:
        #{commit_message}

      To show the formula before removal, run:
        git -C "$(brew --repo #{tap})" show #{short_hash}^:#{relative_path}

      If you still use this formula, consider creating your own tap:
        #{Formatter.url("https://docs.brew.sh/How-to-Create-and-Maintain-a-Tap")}
    EOS
  end
end

.disallowed_reason(name) ⇒ String? Also known as: generic_disallowed_reason

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:



12
13
14
15
16
17
18
19
20
21
# File 'extend/os/mac/missing_formula.rb', line 12

def disallowed_reason(name)
  case name.downcase
  when "xcode"
    <<~EOS
      Xcode can be installed from the App Store.
    EOS
  else
    generic_disallowed_reason(name)
  end
end

.reason(name, silent: false, show_info: false) ⇒ 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:

  • name (String)
  • silent (Boolean) (defaults to: false)
  • show_info (Boolean) (defaults to: false)

Returns:



11
12
13
14
# File 'missing_formula.rb', line 11

def reason(name, silent: false, show_info: false)
  cask_reason(name, silent:, show_info:) || disallowed_reason(name) ||
    tap_migration_reason(name) || deleted_reason(name, silent:)
end

.suggest_command(name, command) ⇒ 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:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'extend/os/mac/missing_formula.rb', line 31

def suggest_command(name, command)
  suggestion = <<~EOS
    Found a cask named "#{name}" instead. Try
      brew #{command} --cask #{name}

  EOS
  case command
  when "install"
    Cask::CaskLoader.load(name)
  when "uninstall"
    cask = Cask::Caskroom.casks.find { |installed_cask| installed_cask.to_s == name }
    raise Cask::CaskUnavailableError, name if cask.nil?
  when "info"
    cask = Cask::CaskLoader.load(name)
    suggestion = <<~EOS
      Found a cask named "#{name}" instead.

      #{Cask::Info.get_info(cask)}
    EOS
  else
    return
  end
  suggestion
rescue Cask::CaskUnavailableError
  nil
end

.tap_migration_reason(name) ⇒ 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:



99
100
101
102
103
104
105
106
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 'missing_formula.rb', line 99

def tap_migration_reason(name)
  message = T.let(nil, T.nilable(String))

  Tap.each do |old_tap|
    new_tap = old_tap.tap_migrations[name]
    next unless new_tap

    new_tap_user, new_tap_repo, new_tap_new_name = new_tap.split("/")
    new_tap_name = "#{new_tap_user}/#{new_tap_repo}"

    message = <<~EOS
      It was migrated from #{old_tap} to #{new_tap}.
    EOS
    break if new_tap_name == CoreTap.instance.name

    install_cmd = if new_tap_name.start_with?("homebrew/cask")
      "install --cask"
    else
      "install"
    end
    new_tap_new_name ||= name

    message += <<~EOS
      You can access it again by running:
        brew tap #{new_tap_name}
      And then you can install it by running:
        brew #{install_cmd} #{new_tap_new_name}
    EOS
    break
  end

  message
end