Class: Cask::Cmd Private

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Context
Defined in:
brew/Library/Homebrew/cask/cmd.rb,
brew/Library/Homebrew/cask/cmd/cat.rb,
brew/Library/Homebrew/cask/cmd/zap.rb,
brew/Library/Homebrew/cask/cmd/edit.rb,
brew/Library/Homebrew/cask/cmd/help.rb,
brew/Library/Homebrew/cask/cmd/home.rb,
brew/Library/Homebrew/cask/cmd/info.rb,
brew/Library/Homebrew/cask/cmd/list.rb,
brew/Library/Homebrew/cask/cmd/audit.rb,
brew/Library/Homebrew/cask/cmd/fetch.rb,
brew/Library/Homebrew/cask/cmd/style.rb,
brew/Library/Homebrew/cask/cmd/create.rb,
brew/Library/Homebrew/cask/cmd/doctor.rb,
brew/Library/Homebrew/cask/cmd/--cache.rb,
brew/Library/Homebrew/cask/cmd/install.rb,
brew/Library/Homebrew/cask/cmd/upgrade.rb,
brew/Library/Homebrew/cask/cmd/outdated.rb,
brew/Library/Homebrew/cask/cmd/reinstall.rb,
brew/Library/Homebrew/cask/cmd/uninstall.rb,
brew/Library/Homebrew/cask/cmd/internal_help.rb,
brew/Library/Homebrew/cask/cmd/internal_stanza.rb,
brew/Library/Homebrew/cask/cmd/abstract_command.rb,
brew/Library/Homebrew/cask/cmd/abstract_internal_command.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Implementation of the brew cask command-line interface.

Defined Under Namespace

Classes: AbstractCommand, AbstractInternalCommand, Audit, Cache, Cat, Create, Doctor, Edit, ExternalCommand, ExternalRubyCommand, Fetch, Help, Home, Info, Install, InternalHelp, InternalStanza, List, NullCommand, Outdated, Reinstall, Style, Uninstall, UnknownSubcommand, Upgrade, Zap

Constant Summary collapse

ALIASES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "ls"       => "list",
  "homepage" => "home",
  "instal"   => "install", # gem does the same
  "uninstal" => "uninstall",
  "rm"       => "uninstall",
  "remove"   => "uninstall",
  "abv"      => "info",
  "dr"       => "doctor",
}.freeze
DEPRECATED_COMMANDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  Cmd::Cache     => "brew --cache [--cask]",
  Cmd::Audit     => "brew audit [--cask]",
  Cmd::Cat       => "brew cat [--cask]",
  Cmd::Create    => "brew create --cask --set-name <name> <url>",
  Cmd::Doctor    => "brew doctor --verbose",
  Cmd::Edit      => "brew edit [--cask]",
  Cmd::Fetch     => "brew fetch [--cask]",
  Cmd::Help      => "brew help",
  Cmd::Home      => "brew home",
  Cmd::Info      => "brew info [--cask]",
  Cmd::Install   => "brew install [--cask]",
  Cmd::List      => "brew list [--cask]",
  Cmd::Outdated  => "brew outdated [--cask]",
  Cmd::Reinstall => "brew reinstall [--cask]",
  Cmd::Style     => "brew style",
  Cmd::Uninstall => "brew uninstall [--cask]",
  Cmd::Upgrade   => "brew upgrade [--cask]",
  Cmd::Zap       => "brew uninstall --zap [--cask]",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Context

current, current=, #debug?, #quiet?, #verbose?, #with_context

Constructor Details

#initialize(*args) ⇒ Cmd

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Cmd.



120
121
122
# File 'brew/Library/Homebrew/cask/cmd.rb', line 120

def initialize(*args)
  @argv = args
end

Class Method Details

.aliasesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



112
113
114
# File 'brew/Library/Homebrew/cask/cmd.rb', line 112

def self.aliases
  ALIASES
end

.command_classesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
98
99
100
# File 'brew/Library/Homebrew/cask/cmd.rb', line 95

def self.command_classes
  @command_classes ||= constants.map(&method(:const_get))
                                .select { |klass| klass.is_a?(Class) && klass < AbstractCommand }
                                .reject(&:abstract?)
                                .sort_by(&:command_name)
end

.commandsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



102
103
104
# File 'brew/Library/Homebrew/cask/cmd.rb', line 102

def self.commands
  @commands ||= command_classes.map(&:command_name)
end

.lookup_command(command_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



106
107
108
109
110
# File 'brew/Library/Homebrew/cask/cmd.rb', line 106

def self.lookup_command(command_name)
  @lookup ||= Hash[commands.zip(command_classes)]
  command_name = ALIASES.fetch(command_name, command_name)
  @lookup.fetch(command_name, nil)
end

.parser(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'brew/Library/Homebrew/cask/cmd.rb', line 77

def self.parser(&block)
  Homebrew::CLI::Parser.new do
    if block
      instance_eval(&block)
    else
      usage_banner <<~EOS
        `cask` <command> [<options>] [<cask>]

        Homebrew Cask provides a friendly CLI workflow for the administration of macOS applications distributed as binaries.

        See also: `man brew`
      EOS
    end

    cask_options
  end
end

.run(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



116
117
118
# File 'brew/Library/Homebrew/cask/cmd.rb', line 116

def self.run(*args)
  new(*args).run
end

Instance Method Details

#detect_external_command(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



152
153
154
155
156
157
158
159
160
161
162
163
# File 'brew/Library/Homebrew/cask/cmd.rb', line 152

def detect_external_command(*args)
  args.each_with_index do |arg, i|
    if command = find_external_command(arg)
      args.delete_at(i)
      return [command, args]
    elsif !arg.start_with?("-")
      break
    end
  end

  nil
end

#detect_internal_command(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'brew/Library/Homebrew/cask/cmd.rb', line 139

def detect_internal_command(*args)
  args.each_with_index do |arg, i|
    if command = self.class.lookup_command(arg)
      args.delete_at(i)
      return [command, args]
    elsif !arg.start_with?("-")
      break
    end
  end

  nil
end

#find_external_command(command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'brew/Library/Homebrew/cask/cmd.rb', line 124

def find_external_command(command)
  @tap_cmd_directories ||= Tap.cmd_directories
  @path ||= PATH.new(@tap_cmd_directories, ENV["HOMEBREW_PATH"])

  external_ruby_cmd = @tap_cmd_directories.map { |d| d/"brewcask-#{command}.rb" }
                                          .find(&:file?)
  external_ruby_cmd ||= which("brewcask-#{command}.rb", @path)

  if external_ruby_cmd
    ExternalRubyCommand.new(command, external_ruby_cmd)
  elsif external_command = which("brewcask-#{command}", @path)
    ExternalCommand.new(external_command)
  end
end

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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
# File 'brew/Library/Homebrew/cask/cmd.rb', line 165

def run
  argv = @argv

  args = self.class.parser.parse(argv, ignore_invalid_options: true)

  Tap.install_default_cask_tap_if_necessary

  command, argv = detect_internal_command(*argv) ||
                  detect_external_command(*argv) ||
                  [args.remaining.empty? ? NullCommand : UnknownSubcommand.new(args.remaining.first), argv]

  if (replacement = DEPRECATED_COMMANDS[command])
    odisabled "brew cask #{command.command_name}", replacement
  end

  if args.help?
    puts command.help
  else
    command.run(*argv)
  end
rescue CaskError, MethodDeprecatedError, ArgumentError => e
  onoe e.message
  $stderr.puts e.backtrace if args.debug?
  exit 1
end