Class: Cask::Cmd Private

Inherits:
Object
  • Object
show all
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::Doctor    => "brew doctor --verbose",
  Cmd::Home      => "brew home",
  Cmd::List      => "brew list --cask",
  Cmd::Outdated  => "brew outdated --cask",
  Cmd::Reinstall => "brew reinstall",
  Cmd::Upgrade   => "brew upgrade --cask",
}.freeze
OPTIONS =

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.

[
  [:flag, "--appdir=", {
    description: "Target location for Applications. " \
                 "Default: `#{Config::DEFAULT_DIRS[:appdir]}`",
  }],
  [:flag, "--colorpickerdir=", {
    description: "Target location for Color Pickers. " \
                 "Default: `#{Config::DEFAULT_DIRS[:colorpickerdir]}`",
  }],
  [:flag, "--prefpanedir=", {
    description: "Target location for Preference Panes. " \
                 "Default: `#{Config::DEFAULT_DIRS[:prefpanedir]}`",
  }],
  [:flag, "--qlplugindir=", {
    description: "Target location for QuickLook Plugins. " \
                 "Default: `#{Config::DEFAULT_DIRS[:qlplugindir]}`",
  }],
  [:flag, "--mdimporterdir=", {
    description: "Target location for Spotlight Plugins. " \
                 "Default: `#{Config::DEFAULT_DIRS[:mdimporterdir]}`",
  }],
  [:flag, "--dictionarydir=", {
    description: "Target location for Dictionaries. " \
                  "Default: `#{Config::DEFAULT_DIRS[:dictionarydir]}`",
  }],
  [:flag, "--fontdir=", {
    description: "Target location for Fonts. " \
                 "Default: `#{Config::DEFAULT_DIRS[:fontdir]}`",
  }],
  [:flag, "--servicedir=", {
    description: "Target location for Services. " \
                 "Default: `#{Config::DEFAULT_DIRS[:servicedir]}`",
  }],
  [:flag, "--input_methoddir=", {
    description: "Target location for Input Methods. " \
                 "Default: `#{Config::DEFAULT_DIRS[:input_methoddir]}`",
  }],
  [:flag, "--internet_plugindir=", {
    description: "Target location for Internet Plugins. " \
                 "Default: `#{Config::DEFAULT_DIRS[:internet_plugindir]}`",
  }],
  [:flag, "--audio_unit_plugindir=", {
    description: "Target location for Audio Unit Plugins. " \
                "Default: `#{Config::DEFAULT_DIRS[:audio_unit_plugindir]}`",
  }],
  [:flag, "--vst_plugindir=", {
    description: "Target location for VST Plugins. " \
                "Default: `#{Config::DEFAULT_DIRS[:vst_plugindir]}`",
  }],
  [:flag, "--vst3_plugindir=", {
    description: "Target location for VST3 Plugins. " \
                 "Default: `#{Config::DEFAULT_DIRS[:vst3_plugindir]}`",
  }],
  [:flag, "--screen_saverdir=", {
    description: "Target location for Screen Savers. " \
                 "Default: `#{Config::DEFAULT_DIRS[:screen_saverdir]}`",
  }],
  [:comma_array, "--language", {
    description: "Set language of the Cask to install. The first matching " \
                 "language is used, otherwise the default language on the Cask. " \
                 "The default value is the `language of your system`",
  }],
].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.



189
190
191
# File 'brew/Library/Homebrew/cask/cmd.rb', line 189

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.



181
182
183
# File 'brew/Library/Homebrew/cask/cmd.rb', line 181

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.



164
165
166
167
168
169
# File 'brew/Library/Homebrew/cask/cmd.rb', line 164

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.



171
172
173
# File 'brew/Library/Homebrew/cask/cmd.rb', line 171

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

.descriptionObject

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.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'brew/Library/Homebrew/cask/cmd.rb', line 63

def self.description
  max_command_length = Cmd.commands.map(&:length).max

  command_lines = Cmd.command_classes
                     .select(&:visible?)
                     .map do |klass|
    "  - #{"`#{klass.command_name}`".ljust(max_command_length + 2)}  #{klass.short_description}\n"
  end

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

    Commands:
    #{command_lines.join}

    See also: `man brew`
  EOS
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.



175
176
177
178
179
# File 'brew/Library/Homebrew/cask/cmd.rb', line 175

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.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'brew/Library/Homebrew/cask/cmd.rb', line 146

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

        #{Cmd.description}
      EOS
    end

    OPTIONS.each do |option|
      send(*option)
    end
  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.



185
186
187
# File 'brew/Library/Homebrew/cask/cmd.rb', line 185

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.



221
222
223
224
225
226
227
228
229
230
231
232
# File 'brew/Library/Homebrew/cask/cmd.rb', line 221

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.



208
209
210
211
212
213
214
215
216
217
218
219
# File 'brew/Library/Homebrew/cask/cmd.rb', line 208

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.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'brew/Library/Homebrew/cask/cmd.rb', line 193

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.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'brew/Library/Homebrew/cask/cmd.rb', line 234

def run
  argv = @argv

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

  Config::DEFAULT_DIRS.each_key do |name|
    Config.global.public_send(:"#{name}=", args[name]) if args[name]
  end

  Config.global.languages = args.language if args.language

  Tap.default_cask_tap.install unless Tap.default_cask_tap.installed?

  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])
    odeprecated "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