Class: Hbc::CLI

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
Library/Homebrew/cask/lib/hbc/cli.rb,
Library/Homebrew/cask/lib/hbc/cli/cat.rb,
Library/Homebrew/cask/lib/hbc/cli/zap.rb,
Library/Homebrew/cask/lib/hbc/cli/edit.rb,
Library/Homebrew/cask/lib/hbc/cli/home.rb,
Library/Homebrew/cask/lib/hbc/cli/info.rb,
Library/Homebrew/cask/lib/hbc/cli/list.rb,
Library/Homebrew/cask/lib/hbc/cli/audit.rb,
Library/Homebrew/cask/lib/hbc/cli/fetch.rb,
Library/Homebrew/cask/lib/hbc/cli/style.rb,
Library/Homebrew/cask/lib/hbc/cli/create.rb,
Library/Homebrew/cask/lib/hbc/cli/doctor.rb,
Library/Homebrew/cask/lib/hbc/cli/search.rb,
Library/Homebrew/cask/lib/hbc/cli/cleanup.rb,
Library/Homebrew/cask/lib/hbc/cli/install.rb,
Library/Homebrew/cask/lib/hbc/cli/options.rb,
Library/Homebrew/cask/lib/hbc/cli/upgrade.rb,
Library/Homebrew/cask/lib/hbc/cli/outdated.rb,
Library/Homebrew/cask/lib/hbc/cli/--version.rb,
Library/Homebrew/cask/lib/hbc/cli/reinstall.rb,
Library/Homebrew/cask/lib/hbc/cli/uninstall.rb,
Library/Homebrew/cask/lib/hbc/cli/internal_dump.rb,
Library/Homebrew/cask/lib/hbc/cli/internal_help.rb,
Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb,
Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb,
Library/Homebrew/cask/lib/hbc/cli/abstract_internal_command.rb,
Library/Homebrew/cask/lib/hbc/cli/internal_appcast_checkpoint.rb,
Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb

Defined Under Namespace

Modules: Options Classes: AbstractCommand, AbstractInternalCommand, Audit, Cat, Cleanup, Create, Doctor, Edit, Fetch, Home, Info, Install, InternalAppcastCheckpoint, InternalAuditModifiedCasks, InternalDump, InternalHelp, InternalStanza, List, NullCommand, Outdated, Reinstall, Search, Style, Uninstall, Upgrade, Version, Zap

Constant Summary

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

included, #process_arguments

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI



135
136
137
# File 'Library/Homebrew/cask/lib/hbc/cli.rb', line 135

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

Class Method Details

.command_classesObject



74
75
76
77
78
79
# File 'Library/Homebrew/cask/lib/hbc/cli.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 'Library/Homebrew/cask/lib/hbc/cli.rb', line 81

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

.lookup_command(command_name) ⇒ Object



85
86
87
88
89
# File 'Library/Homebrew/cask/lib/hbc/cli.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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'Library/Homebrew/cask/lib/hbc/cli.rb', line 182

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



131
132
133
# File 'Library/Homebrew/cask/lib/hbc/cli.rb', line 131

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

.run_command(command, *args) ⇒ Object



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
121
122
123
124
125
126
127
128
129
# File 'Library/Homebrew/cask/lib/hbc/cli.rb', line 95

def self.run_command(command, *args)
  if command.respond_to?(:run)
    # usual case: built-in command verb
    command.run(*args)
  elsif require?(which("brewcask-#{command}.rb", ENV["HOMEBREW_PATH"]))
    # external command as Ruby library on PATH, Homebrew-style
  elsif command.to_s.include?("/") && require?(command.to_s)
    # external command as Ruby library with literal path, useful
    # for development and troubleshooting
    sym = File.basename(command.to_s, ".rb").capitalize
    klass = begin
              const_get(sym)
            rescue NameError
              nil
            end

    if klass.respond_to?(:run)
      # invoke "run" on a Ruby library which follows our coding conventions
      # other Ruby libraries must do everything via "require"
      klass.run(*args)
    end
  elsif external_command = which("brewcask-#{command}", ENV["HOMEBREW_PATH"])
    # arbitrary external executable on PATH, Homebrew-style
    exec external_command, *ARGV[1..-1]
  elsif Pathname.new(command.to_s).executable? &&
        command.to_s.include?("/") &&
        !command.to_s.match(/\.rb$/)
    # arbitrary external executable with literal path, useful
    # for development and troubleshooting
    exec command, *ARGV[1..-1]
  else
    # failure
    NullCommand.new(command, *args).run
  end
end

.should_init?(command) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'Library/Homebrew/cask/lib/hbc/cli.rb', line 91

def self.should_init?(command)
  command.is_a?(Class) && !command.abstract? && command.needs_init?
end

Instance Method Details

#detect_command_and_arguments(*args) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'Library/Homebrew/cask/lib/hbc/cli.rb', line 139

def detect_command_and_arguments(*args)
  command = args.detect 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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'Library/Homebrew/cask/lib/hbc/cli.rb', line 201

def process_options(*args)
  all_args = Shellwords.shellsplit(ENV["HOMEBREW_CASK_OPTS"] || "") + 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|
    begin
      !process_arguments([arg]).empty?
    rescue OptionParser::InvalidOption, OptionParser::MissingArgument, OptionParser::AmbiguousOption
      true
    end
  end

  remaining + non_options
end

#runObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'Library/Homebrew/cask/lib/hbc/cli.rb', line 155

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?

  Hbc.default_tap.install unless Hbc.default_tap.installed?
  Hbc.init if self.class.should_init?(command)
  self.class.run_command(command, *args)
rescue CaskError, ArgumentError, OptionParser::InvalidOption => e
  msg = e.message
  msg << e.backtrace.join("\n").prepend("\n") if ARGV.debug?
  onoe msg
  exit 1
rescue StandardError, ScriptError, NoMemoryError => e
  msg = "#{e.message}\n"
  msg << Utils.error_message_with_suggestions
  msg << e.backtrace.join("\n")
  onoe msg
  exit 1
end