Module: Homebrew::Bundle::Remover Private

Extended by:
Utils::Output::Mixin
Defined in:
bundle/remover.rb

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.

Class Method Summary collapse

Methods included from Utils::Output::Mixin

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

Class Method Details

.find_formula_or_cask(name, raise_error: false) ⇒ Formula, ...

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)
  • raise_error (Boolean) (defaults to: false)

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'bundle/remover.rb', line 73

def self.find_formula_or_cask(name, raise_error: false)
  formula = begin
    Formulary.factory(name)
  rescue FormulaUnavailableError
    raise if raise_error
  end

  return formula if formula.present?

  begin
    ::Cask::CaskLoader.load(name)
  rescue ::Cask::CaskUnavailableError
    raise if raise_error
  end
end

.possible_names(formula_name, raise_error: true) ⇒ 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:

  • formula_name (String)
  • raise_error (Boolean) (defaults to: true)

Returns:



56
57
58
59
60
61
# File 'bundle/remover.rb', line 56

def self.possible_names(formula_name, raise_error: true)
  formula = find_formula_or_cask(formula_name, raise_error:)
  return [] if formula.nil? || !formula.is_a?(Formula)

  [formula_name, formula.name, formula.full_name, *formula.aliases, *formula.oldnames].compact.uniq
end

.remove(*args, type:, global:, file:) ⇒ 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:



12
13
14
15
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
53
# File 'bundle/remover.rb', line 12

def self.remove(*args, type:, global:, file:)
  require "bundle/brewfile"
  require "bundle/dumper"

  brewfile = Brewfile.read(global:, file:)
  content = brewfile.input
  entry_type = type.to_s if type != :none
  escaped_args = args.flat_map do |arg|
    names = if type == :brew
      possible_names(arg)
    else
      [arg]
    end

    names.uniq.map { |a| Regexp.escape(a) }
  end

  entry_regex = /#{entry_type}(\s+|\(\s*)"(#{escaped_args.join("|")})"/
  new_lines = T.let([], T::Array[String])

  content.split("\n").compact.each do |line|
    if line.match?(entry_regex)
      name = line[entry_regex, 2]
      remove_package_description_comment(new_lines, name)
    else
      new_lines << line
    end
  end

  new_content = "#{new_lines.join("\n")}\n"

  if content.chomp == new_content.chomp &&
     type == :none &&
     args.any? { |arg| possible_names(arg, raise_error: false).count > 1 }
    opoo "No matching entries found in Brewfile. Try again with `--formula` to match formula " \
         "aliases and old formula names."
    return
  end

  path = Dumper.brewfile_path(global:, file:)
  Dumper.write_file path, new_content
end

.remove_package_description_comment(lines, package_name) ⇒ 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:



64
65
66
67
68
69
70
# File 'bundle/remover.rb', line 64

def self.remove_package_description_comment(lines, package_name)
  comment = lines.last&.match(/^\s*#\s+(?<desc>.+)$/)&.[](:desc)
  return unless comment
  return if find_formula_or_cask(package_name)&.desc != comment

  lines.pop
end