homebrew-cask/lib/cask/cli.rb

214 lines
5.7 KiB
Ruby

class Cask::CLI; end
require 'optparse'
require 'shellwords'
require 'cask/cli/alfred'
require 'cask/cli/audit'
require 'cask/cli/cat'
require 'cask/cli/checklinks'
require 'cask/cli/cleanup'
require 'cask/cli/create'
require 'cask/cli/doctor'
require 'cask/cli/edit'
require 'cask/cli/fetch'
require 'cask/cli/home'
require 'cask/cli/info'
require 'cask/cli/install'
require 'cask/cli/list'
require 'cask/cli/search'
require 'cask/cli/uninstall'
require 'cask/cli/update'
class Cask::CLI
ISSUES_URL = "https://github.com/caskroom/homebrew-cask/issues"
def self.commands
Cask::CLI.constants - [:NullCommand, :ISSUES_URL, "NullCommand", "ISSUES_URL"]
end
def self.lookup_command(command_string)
aliases = {
'ls' => 'list',
'homepage' => 'home',
'-S' => 'search',
'up' => 'update',
'instal' => 'install', # gem does the same
'rm' => 'uninstall',
'remove' => 'uninstall',
'abv' => 'info',
'dr' => 'doctor',
# aliases from Homebrew that we don't (yet) support
# 'ln' => 'link',
# 'configure' => 'diy',
# '--repo' => '--repository',
# 'environment' => '--env',
# '-c1' => '--config',
}
command_string = aliases[command_string] if aliases.key?(command_string)
if command_string && Cask::CLI.const_defined?(command_string.capitalize)
Cask::CLI.const_get(command_string.capitalize)
else
command_string
end
end
def self.run_command(command, *rest)
if command.respond_to?(:run)
command.run(*rest)
elsif which "brewcask-#{command}"
exec "brewcask-#{command}", *ARGV[1..-1]
elsif require? which("brewcask-#{command}.rb").to_s
exit 0
else
Cask::CLI::NullCommand.new(command).run
end
end
def self.process(arguments)
command_string, *rest = *arguments
rest = process_options(rest)
Cask.init
command = lookup_command(command_string)
run_command(command, *rest)
rescue CaskError, ChecksumMismatchError => e
onoe e
$stderr.puts e.backtrace if Cask.debug
exit 1
rescue StandardError, ScriptError, NoMemoryError => e
onoe e
puts "#{Tty.white}Please report this bug:"
puts " #{Tty.em}#{ISSUES_URL}#{Tty.reset}"
puts e.backtrace
exit 1
end
def self.nice_listing(cask_list)
casks = {}
cask_list.each { |c|
user, repo, name = c.split '/'
repo.sub!(/^homebrew-/i, '')
casks[name] ||= []
casks[name].push "#{user}/#{repo}"
}
list = []
casks.each { |name,taps|
if taps.length == 1
list.push name
else
taps.each { |r| list.push [r,name].join '/' }
end
}
list.sort
end
def self.parser
# If you modify these arguments, please update USAGE.md
@parser ||= OptionParser.new do |opts|
opts.on("--caskroom=MANDATORY") do |v|
Cask.caskroom = Pathname(v).expand_path
end
opts.on("--appdir=MANDATORY") do |v|
Cask.appdir = Pathname(v).expand_path
end
opts.on("--colorpickerdir=MANDATORY") do |v|
Cask.colorpickerdir = Pathname(v).expand_path
end
opts.on("--prefpanedir=MANDATORY") do |v|
Cask.prefpanedir = Pathname(v).expand_path
end
opts.on("--qlplugindir=MANDATORY") do |v|
Cask.qlplugindir = Pathname(v).expand_path
end
opts.on("--fontdir=MANDATORY") do |v|
Cask.fontdir = Pathname(v).expand_path
end
opts.on("--widgetdir=MANDATORY") do |v|
Cask.widgetdir = Pathname(v).expand_path
end
opts.on("--servicedir=MANDATORY") do |v|
Cask.servicedir = Pathname(v).expand_path
end
opts.on("--binarydir=MANDATORY") do |v|
Cask.binarydir = Pathname(v).expand_path
end
opts.on("--input_methoddir=MANDATORY") do |v|
Cask.input_methoddir = Pathname(v).expand_path
end
opts.on("--screen_saverdir=MANDATORY") do |v|
Cask.screen_saverdir = Pathname(v).expand_path
end
opts.on("--no-binaries") do |v|
Cask.no_binaries = true
end
opts.on("--debug") do |v|
Cask.debug = true
end
opts.on("--outdated") do |v|
Cask.outdated = true
end
end
end
def self.process_options(args)
all_args = Shellwords.shellsplit(ENV['HOMEBREW_CASK_OPTS'] || "") + args
remaining = []
until all_args.empty?
begin
head = all_args.shift
remaining.concat(parser.parse([head]))
rescue OptionParser::InvalidOption
remaining << head
retry
end
end
remaining
end
class NullCommand
def initialize(attempted_name)
@attempted_name = attempted_name
end
def run(*args)
purpose
if @attempted_name and @attempted_name != "help"
puts "!! "
puts "!! no command with name: #{@attempted_name}"
puts "!! "
end
usage
end
def purpose
puts <<-PURPOSE.undent
brew-cask provides a friendly homebrew-style CLI workflow for the
administration of Mac applications distributed as binaries.
PURPOSE
end
def usage
longest_command_size = Cask::CLI.commands.map(&:length).max
puts "Commands:\n\n"
Cask::CLI.commands.each do |c|
command = "#{c.downcase}".ljust(longest_command_size)
puts " #{command} #{_help_for(c)}"
end
puts %Q{\nSee also "man brew-cask"}
end
def help
''
end
def _help_for(command_string)
command = Cask::CLI.lookup_command(command_string)
if command.respond_to?(:help)
command.help
end
end
end
end