Skip to content

Commit

Permalink
create casks with brew cask edit foo --create
Browse files Browse the repository at this point in the history
  • Loading branch information
phinze committed Apr 4, 2013
1 parent 0fbe7a1 commit bf33643
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
24 changes: 18 additions & 6 deletions lib/cask/cli.rb
Expand Up @@ -38,17 +38,29 @@ def self.nice_listing(cask_list)
}
list.sort
end

def self.process_options(args)
allrgs = Shellwords.shellsplit(ENV['HOMEBREW_CASK_OPTS'] || "") + args
OptionParser.new do |opts|

def self.parser
@parser ||= OptionParser.new do |opts|
opts.on("--appdir=MANDATORY") do |v|
Cask.appdir = Pathname.new File.expand_path(v)
end
end.parse!(allrgs)
return allrgs
end
end

def self.process_options(args)
all_args = Shellwords.shellsplit(ENV['HOMEBREW_CASK_OPTS'] || "") + args
remaining = []
while !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)
Expand Down
6 changes: 4 additions & 2 deletions lib/cask/cli/edit.rb
@@ -1,8 +1,10 @@
module Cask::CLI::Edit
def self.run(*arguments)
cask_name, *_ = *arguments
cask_name, *args = *arguments
cask_path = Cask.path(cask_name)
raise CaskUnavailableError, cask_name + ".rb" if cask_path.nil? || !cask_path.file?
unless cask_path.exist? || args.include?('--create')
raise CaskUnavailableError, "#{cask_name}, add --create to make a new cask with this name"
end
exec_editor cask_path
end

Expand Down
13 changes: 13 additions & 0 deletions test/cli/edit_test.rb
Expand Up @@ -36,4 +36,17 @@ module Cask::CLI::Edit
[Cask.path('adium')]
]
end

it 'raises an exception when the cask doesnt exist' do
lambda {
Cask::CLI::Edit.run('notacask')
}.must_raise CaskUnavailableError
end

it 'allows new casks to be created with the --create flag' do
Cask::CLI::Edit.run('brand-spankin-new', '--create')
Cask::CLI::Edit.editor_commands.must_equal [
[Cask.tapspath.join(Cask.default_tap, 'Casks', 'brand-spankin-new.rb')]
]
end
end
21 changes: 12 additions & 9 deletions test/cli/options_test.rb
Expand Up @@ -2,21 +2,24 @@

describe Cask::CLI do
it "supports setting the appdir" do
shutup do
Cask::CLI.process %w{help --appdir=/some/path}
end
Cask::CLI.process_options %w{help --appdir=/some/path/foo}

Cask.appdir.must_equal Pathname.new File.expand_path "/some/path"
Cask.appdir.must_equal Pathname('/some/path/foo')
end

it "supports setting the appdir from ENV" do
ENV['HOMEBREW_CASK_OPTS'] = "--appdir=/some/path"
ENV['HOMEBREW_CASK_OPTS'] = "--appdir=/some/path/bar"

shutup do
Cask::CLI.process %w{help}
end
Cask::CLI.process_options %w{help}

Cask.appdir.must_equal Pathname.new File.expand_path "/some/path"
Cask.appdir.must_equal Pathname('/some/path/bar')
end

it "allows additional options to be passed through" do
rest = Cask::CLI.process_options %w{edit foo --create --appdir=/some/path/qux}

Cask.appdir.must_equal Pathname('/some/path/qux')
rest.must_equal %w{edit foo --create}
end

after do
Expand Down

0 comments on commit bf33643

Please sign in to comment.