Class: Homebrew::AbstractSubcommand Abstract

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers
Includes:
Utils::Output::Mixin
Defined in:
abstract_subcommand.rb

Overview

This class is abstract.

It cannot be directly instantiated. Subclasses must implement the abstract methods below.

Subclass this to implement a subcommand for a brew command.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

#initialize(args, context: nil, targets: nil, quiet: false, cleanup: true) ⇒ 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.

Parameters:

  • args (CLI::Args)
  • context (T.untyped) (defaults to: nil)
  • targets (T.untyped) (defaults to: nil)
  • quiet (Boolean) (defaults to: false)
  • cleanup (Boolean) (defaults to: true)


105
106
107
108
109
110
111
112
113
# File 'abstract_subcommand.rb', line 105

def initialize(args, context: nil, targets: nil, quiet: false, cleanup: true)
  # `args` is frozen by `CLI::Parser#parse`, so `extend` needs an unfrozen clone
  # (which, unlike `dup`, keeps the singleton methods the parser defined on it).
  @args = T.let(args.clone(freeze: false).extend(self.class.args_module).freeze, CLI::Args)
  @context = context
  @targets = targets
  @quiet = quiet
  @cleanup = cleanup
end

Instance Attribute Details

#args ⇒ CLI::Args (readonly)

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.

Returns:



102
103
104
# File 'abstract_subcommand.rb', line 102

def args
  @args
end

#cleanup ⇒ Boolean (readonly)

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.

Returns:

  • (Boolean)


125
126
127
# File 'abstract_subcommand.rb', line 125

def cleanup
  @cleanup
end

#context ⇒ T.untyped (readonly)

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.

Returns:

  • (T.untyped)


116
117
118
# File 'abstract_subcommand.rb', line 116

def context
  @context
end

#quiet ⇒ Boolean (readonly)

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.

Returns:

  • (Boolean)


122
123
124
# File 'abstract_subcommand.rb', line 122

def quiet
  @quiet
end

#targets ⇒ T.untyped (readonly)

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.

Returns:

  • (T.untyped)


119
120
121
# File 'abstract_subcommand.rb', line 119

def targets
  @targets
end

Class Method Details

.args_module ⇒ T::Module[T.anything]

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.

A module extended onto this subcommand's args in #initialize, so is_a? genuinely holds for the type named in the generated RBI (see Tapioca::Compilers::SubcommandArgs) instead of only approximating it statically.

Returns:

  • (T::Module[T.anything])


53
54
55
# File 'abstract_subcommand.rb', line 53

def args_module
  @args_module ||= T.let(const_set(:Args, Module.new), T.nilable(T::Module[T.anything]))
end

.command ⇒ T.class_of(Homebrew::AbstractCommand)

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.

Returns:

Raises:

  • (TypeError)


42
43
44
45
46
47
# File 'abstract_subcommand.rb', line 42

def command
  found = Homebrew::AbstractCommand.subclasses.find { |candidate| subcommands_for(candidate).include?(self) }
  raise TypeError, "#{self} is not nested under a `Homebrew::AbstractCommand`" if found.nil?

  found
end

.define(parser) ⇒ 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:

Raises:

  • (TypeError)


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'abstract_subcommand.rb', line 66

def define(parser)
  parser_block = @parser_block
  raise TypeError, "subcommand arguments have not been defined" if parser_block.nil?

  parser.subcommand(
    subcommand_name,
    aliases:       @aliases || [],
    alias_options: @alias_options || {},
    default:       @default || false,
  ) do
    instance_eval(&parser_block)
  end
end

.define_all(parser, command:) ⇒ 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:



58
59
60
61
62
63
# File 'abstract_subcommand.rb', line 58

def define_all(parser, command:)
  subcommands_for(command).each do |subcommand|
    subcommand.define(parser)
    subcommand.args_module
  end
end

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

Returns:

Raises:

  • (TypeError)


20
21
22
23
24
25
26
27
28
29
# File 'abstract_subcommand.rb', line 20

def subcommand_name
  require "utils"

  class_name = name
  raise TypeError, "anonymous subcommands do not have names" if class_name.nil?

  Utils.underscore(class_name.split("::").fetch(-1))
       .tr("_", "-")
       .delete_suffix("-subcommand")
end

.subcommands_for(command) ⇒ Array<T.class_of(AbstractSubcommand)>

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:



32
33
34
35
36
37
38
39
# File 'abstract_subcommand.rb', line 32

def subcommands_for(command)
  namespace = "#{command.name}::"
  # Qualified so this behaves the same whether called on `AbstractSubcommand`
  # itself or (as `command` does below) on one of its subclasses.
  Homebrew::AbstractSubcommand.subclasses.select do |subcommand|
    subcommand.name&.start_with?(namespace)
  end
end

Instance Method Details

#run ⇒ void

This method is abstract.

This method returns an undefined value.

This method will be invoked when the subcommand is run.



131
# File 'abstract_subcommand.rb', line 131

def run; end