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/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/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, ExternalCommand, ExternalRubyCommand, Fetch, Help, Home, Info, Install, InternalHelp, InternalStanza, List, NullCommand, Outdated, Reinstall, Style, Uninstall, Upgrade, Zap

Constant Summary collapse

ALIASES =
{
  "ls"       => "list",
  "homepage" => "home",
  "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.



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

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, nil)
end

.nice_listing(cask_list) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'brew/Library/Homebrew/cask/cmd.rb', line 163

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



91
92
93
# File 'brew/Library/Homebrew/cask/cmd.rb', line 91

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

Instance Method Details

#detect_external_command(*args) ⇒ Object



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

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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'brew/Library/Homebrew/cask/cmd.rb', line 114

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'brew/Library/Homebrew/cask/cmd.rb', line 99

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

#process_options(*args) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'brew/Library/Homebrew/cask/cmd.rb', line 182

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

  non_options = []

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

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

  all_args = cask_opts + args

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

  remaining + non_options
end

#runObject



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

def run
  MacOS.full_version = ENV["MACOS_VERSION"] unless ENV["MACOS_VERSION"].nil?
  Tap.default_cask_tap.install unless Tap.default_cask_tap.installed?

  args = @args.dup
  command, args = detect_internal_command(*args) || detect_external_command(*args) || [NullCommand.new, args]

  if help?
    Help.new(command.command_name).run
  else
    command.run(*args)
  end
rescue CaskError, MethodDeprecatedError, ArgumentError, OptionParser::InvalidOption => e
  onoe e.message
  $stderr.puts e.backtrace if Homebrew.args.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