diff --git a/HACKING.md b/HACKING.md index 1b6be39638e..119175e468a 100644 --- a/HACKING.md +++ b/HACKING.md @@ -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-`. (So long as `` 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-.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 diff --git a/lib/cask/cli.rb b/lib/cask/cli.rb index 434cf9424f8..65885ec7dac 100644 --- a/lib/cask/cli.rb +++ b/lib/cask/cli.rb @@ -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