Support external commands beneath `brew cask`

This commit is contained in:
Roland Walker 2014-01-24 12:07:42 -05:00
parent ce615d54b9
commit 43362e4b3e
2 changed files with 37 additions and 8 deletions

View File

@ -161,4 +161,17 @@ line of an email. (See [CONTRIBUTING.md](CONTRIBUTING.md#commit-messages)).
A short but complete summary line helps the maintainers respond to your
pull request more quickly.
### External Commands
Advanced users may create their own external commands for homebrew-cask by
following conventions similar to external commands for git or Homebrew. An
external command may be any executable on your `$PATH` which follows the
form `brewcask-<command>`. (So long as `<command>` does not conflict with
an existing command verb.) The command will be invoked by `exec` and passed
any unprocessed arguments from the original command-line. An external
command may also be implemented as an executable Ruby file, on your `$PATH`,
which follows the form `brewcask-<command>.rb`. The Ruby file will be
`required` and will have full access to the Ruby environments of both
homebrew-cask and Homebrew.
# <3 THANK YOU! <3

View File

@ -21,19 +21,32 @@ class Cask::CLI
Cask::CLI.constants - ["NullCommand"]
end
def self.lookup_command(command)
if command && Cask::CLI.const_defined?(command.capitalize)
Cask::CLI.const_get(command.capitalize)
def self.lookup_command(command_string)
if command_string && Cask::CLI.const_defined?(command_string.capitalize)
Cask::CLI.const_get(command_string.capitalize)
else
Cask::CLI::NullCommand.new(command)
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, *rest = *arguments
command_string, *rest = *arguments
rest = process_options(rest)
Cask.init
lookup_command(command).run(*rest)
command = lookup_command(command_string)
run_command(command, *rest)
rescue CaskAlreadyInstalledError => e
opoo e
$stderr.puts e.backtrace if @debug
@ -151,8 +164,11 @@ class Cask::CLI
''
end
def _help_for(command)
Cask::CLI.lookup_command(command).help
def _help_for(command_string)
command = Cask::CLI.lookup_command(command_string)
if command.respond_to?(:help)
command.help
end
end
end
end