Class: Cask::Cmd

Inherits:
Object
  • Object
show all
Includes:
Options
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/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/options.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

Defined Under Namespace

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

Constant Summary collapse

ALIASES =
{
  "ls"       => "list",
  "homepage" => "home",
  "-S"       => "search",    # verb starting with "-" is questionable
  "up"       => "update",
  "instal"   => "install",   # gem does the same
  "uninstal" => "uninstall",
  "rm"       => "uninstall",
  "remove"   => "uninstall",
  "abv"      => "info",
  "dr"       => "doctor",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

#process_arguments

Constructor Details

#initialize(*args) ⇒ Cmd

Returns a new instance of Cmd



126
127
128
# File 'brew/Library/Homebrew/cask/cmd.rb', line 126

def initialize(*args)
  @args = process_options(*args)
end

Class Method Details

.command_classesObject



74
75
76
77
78
79
# File 'brew/Library/Homebrew/cask/cmd.rb', line 74

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

.commandsObject



81
82
83
# File 'brew/Library/Homebrew/cask/cmd.rb', line 81

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

.lookup_command(command_name) ⇒ Object



85
86
87
88
89
# File 'brew/Library/Homebrew/cask/cmd.rb', line 85

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

.nice_listing(cask_list) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'brew/Library/Homebrew/cask/cmd.rb', line 170

def self.nice_listing(cask_list)
  cask_taps = {}
  cask_list.each do |c|
    user, repo, token = c.split "/"
    repo.sub!(/^homebrew-/i, "")
    cask_taps[token] ||= []
    cask_taps[token].push "#{user}/#{repo}"
  end
  list = []
  cask_taps.each do |token, taps|
    if taps.length == 1
      list.push token
    else
      taps.each { |r| list.push [r, token].join "/" }
    end
  end
  list.sort
end

.run(*args) ⇒ Object



122
123
124
# File 'brew/Library/Homebrew/cask/cmd.rb', line 122

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

.run_command(command, *args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'brew/Library/Homebrew/cask/cmd.rb', line 91

def self.run_command(command, *args)
  return command.run(*args) if command.respond_to?(:run)

  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
    require external_ruby_cmd

    klass = begin
      const_get(command.to_s.capitalize.to_sym)
    rescue NameError
      # External command is a stand-alone Ruby script.
      return
    end

    return klass.run(*args)
  end

  if external_command = which("brewcask-#{command}", path)
    exec external_command, *ARGV[1..-1]
  end

  NullCommand.new(command, *args).run
end

Instance Method Details

#detect_command_and_arguments(*args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'brew/Library/Homebrew/cask/cmd.rb', line 130

def detect_command_and_arguments(*args)
  command = args.find do |arg|
    if self.class.commands.include?(arg)
      true
    else
      break unless arg.start_with?("-")
    end
  end

  if index = args.index(command)
    args.delete_at(index)
  end

  [*command, *args]
end

#process_options(*args) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'brew/Library/Homebrew/cask/cmd.rb', line 189

def process_options(*args)
  exclude_regex = /^\-\-#{Regexp.union(*Config::DEFAULT_DIRS.keys.map(&Regexp.public_method(:escape)))}=/

  all_args = Shellwords.shellsplit(ENV.fetch("HOMEBREW_CASK_OPTS", ""))
                       .reject { |arg| arg.match?(exclude_regex) } + args

  non_options = []

  if idx = all_args.index("--")
    non_options += all_args.drop(idx)
    all_args = all_args.first(idx)
  end

  remaining = all_args.select do |arg|
    !process_arguments([arg]).empty?
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument, OptionParser::AmbiguousOption
    true
  end

  remaining + non_options
end

#runObject



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

def run
  command_name, *args = detect_command_and_arguments(*@args)
  command = if help?
    args.unshift(command_name) unless command_name.nil?
    "help"
  else
    self.class.lookup_command(command_name)
  end

  MacOS.full_version = ENV["MACOS_VERSION"] unless ENV["MACOS_VERSION"].nil?

  Tap.default_cask_tap.install unless Tap.default_cask_tap.installed?
  self.class.run_command(command, *args)
rescue CaskError, MethodDeprecatedError, ArgumentError, OptionParser::InvalidOption => e
  onoe e.message
  $stderr.puts e.backtrace if ARGV.debug?
  exit 1
rescue StandardError, ScriptError, NoMemoryError => e
  onoe e.message
  $stderr.puts Utils.error_message_with_suggestions
  $stderr.puts e.backtrace
  exit 1
end