From fed2824c7f834d62a56286973a10a7e0f2d2eb40 Mon Sep 17 00:00:00 2001
From: phinze <paul.t.hinze@gmail.com>
Date: Sat, 11 May 2013 19:20:07 -0500
Subject: [PATCH] add bona fide `brew cask create` command

the create command opens up an editor with template to get started

remove --create override flag from `brew cask edit`

hopefully this will be more straightforward for contributors

refs #306
---
 lib/cask/cli/create.rb  | 34 ++++++++++++++++++++++
 lib/cask/cli/edit.rb    |  4 +--
 lib/cask/exceptions.rb  | 11 +++++++
 test/cli/create_test.rb | 64 +++++++++++++++++++++++++++++++++++++++++
 test/cli/edit_test.rb   |  7 -----
 5 files changed, 111 insertions(+), 9 deletions(-)
 create mode 100644 test/cli/create_test.rb

diff --git a/lib/cask/cli/create.rb b/lib/cask/cli/create.rb
index e69de29bb..f58910c97 100644
--- a/lib/cask/cli/create.rb
+++ b/lib/cask/cli/create.rb
@@ -0,0 +1,34 @@
+module Cask::CLI::Create
+  def self.run(*arguments)
+    cask_name, *_ = *arguments
+    cask_path = Cask.path(cask_name)
+
+    if cask_path.exist?
+      raise CaskAlreadyCreatedError.new cask_name
+    end
+
+    File.open(cask_path, 'w') do |f|
+      f.write template(cask_name)
+    end
+
+    exec_editor cask_path
+  end
+
+  def self.template(cask_name);
+    cask_class = cask_name.split('-').map(&:capitalize).join
+    <<-EOS.undent
+      class #{cask_class} < Cask
+        url ''
+        homepage ''
+        version ''
+        sha1 ''
+        link :app, ''
+      end
+    EOS
+  end
+
+  def self.help
+    "creates a cask of the given name and opens it in an editor"
+  end
+end
+
diff --git a/lib/cask/cli/edit.rb b/lib/cask/cli/edit.rb
index 23d5de3a0..886901789 100644
--- a/lib/cask/cli/edit.rb
+++ b/lib/cask/cli/edit.rb
@@ -2,8 +2,8 @@ module Cask::CLI::Edit
   def self.run(*arguments)
     cask_name, *args = *arguments
     cask_path = Cask.path(cask_name)
-    unless cask_path.exist? || args.include?('--create')
-      raise CaskUnavailableError, "#{cask_name}, add --create to make a new cask with this name"
+    unless cask_path.exist?
+      raise CaskUnavailableError, "#{cask_name}, use `brew cask create #{cask_name}` to make a new cask with this name"
     end
     exec_editor cask_path
   end
diff --git a/lib/cask/exceptions.rb b/lib/cask/exceptions.rb
index 3f68370f6..72520d80e 100644
--- a/lib/cask/exceptions.rb
+++ b/lib/cask/exceptions.rb
@@ -19,3 +19,14 @@ class CaskUnavailableError < RuntimeError
     "No available cask for #{name}"
   end
 end
+
+class CaskAlreadyCreatedError < RuntimeError
+  attr_reader :name
+  def initialize name
+    @name = name
+  end
+
+  def to_s
+    "Cask for #{name} already exists. Use `brew cask edit #{name}` to see it."
+  end
+end
diff --git a/test/cli/create_test.rb b/test/cli/create_test.rb
new file mode 100644
index 000000000..1b36ef670
--- /dev/null
+++ b/test/cli/create_test.rb
@@ -0,0 +1,64 @@
+require 'test_helper'
+
+module RecordCreateorCalls
+  def exec_editor(*command)
+    editor_commands << command
+  end
+
+  def reset!
+    @editor_commands = []
+  end
+
+  def editor_commands
+    @editor_commands ||= []
+  end
+end
+
+module Cask::CLI::Create
+  extend RecordCreateorCalls
+end
+
+describe Cask::CLI::Create do
+  before { Cask::CLI::Create.reset! }
+
+  after {
+    %w[ new-cask additional-cask another-cask ].each do |cask|
+      path = Cask.path(cask)
+      path.delete if path.exist?
+    end
+  }
+
+  it 'opens the editor for the specified cask' do
+    Cask::CLI::Create.run('new-cask')
+    Cask::CLI::Create.editor_commands.must_equal [
+      [Cask.path('new-cask')]
+    ]
+  end
+
+  it 'drops a template down for the specified cask' do
+    Cask::CLI::Create.run('new-cask')
+    template = File.read(Cask.path('new-cask'))
+    template.must_equal <<-TEMPLATE.undent
+      class NewCask < Cask
+        url ''
+        homepage ''
+        version ''
+        sha1 ''
+        link :app, ''
+      end
+    TEMPLATE
+  end
+
+  it 'throws away additional arguments and uses the first' do
+    Cask::CLI::Create.run('additional-cask', 'another-cask')
+    Cask::CLI::Create.editor_commands.must_equal [
+      [Cask.path('additional-cask')]
+    ]
+  end
+
+  it 'raises an exception when the cask already exists' do
+    lambda {
+      Cask::CLI::Create.run('caffeine')
+    }.must_raise CaskAlreadyCreatedError
+  end
+end
diff --git a/test/cli/edit_test.rb b/test/cli/edit_test.rb
index 0a00e56c6..a57bc5604 100644
--- a/test/cli/edit_test.rb
+++ b/test/cli/edit_test.rb
@@ -42,11 +42,4 @@ describe Cask::CLI::Edit do
       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