Permalink
Browse files

Add the ability to initialize all guards at once. Closes #227

  • Loading branch information...
1 parent eacb2d4 commit d2cdff9ecf160719cb69b4440e93ee769c22e573 @Maher4Ever committed Jan 27, 2012
Showing with 126 additions and 81 deletions.
  1. +38 −26 lib/guard.rb
  2. +13 −4 lib/guard/cli.rb
  3. +19 −4 spec/guard/cli_spec.rb
  4. +56 −47 spec/guard_spec.rb
View
@@ -23,44 +23,56 @@ module Guard
class << self
attr_accessor :options, :interactor, :listener, :lock
- # Creates the initial Guardfile template and/or add a Guard implementation
- # Guardfile template to an existing Guardfile.
+ # Creates the initial Guardfile template when it does not
+ # already exist.
#
- # @see Guard::Guard.init
+ # @see Guard::CLI.init
#
- # @param [String] guard_name the name of the Guard or template to initialize
- #
- def initialize_template(guard_name = nil)
+ def create_guardfile
if !File.exist?('Guardfile')
::Guard::UI.info "Writing new Guardfile to #{ Dir.pwd }/Guardfile"
FileUtils.cp(GUARDFILE_TEMPLATE, 'Guardfile')
- elsif guard_name.nil?
- ::Guard::UI.error "Guardfile already exists at #{ Dir.pwd }/Guardfile"
- exit 1
end
+ end
- if guard_name
- guard_class = ::Guard.get_guard_class(guard_name, true)
- if guard_class
- guard_class.init(guard_name)
- elsif File.exist?(File.join(HOME_TEMPLATES, guard_name))
- content = File.read('Guardfile')
- template = File.read(File.join(HOME_TEMPLATES, guard_name))
-
- File.open('Guardfile', 'wb') do |f|
- f.puts(content)
- f.puts("")
- f.puts(template)
- end
+ # Adds the Guardfile template of a Guard implementation
+ # to an existing Guardfile.
+ #
+ # @see Guard::CLI.init
+ #
+ # @param [String] guard_name the name of the Guard or template to initialize
+ #
+ def initialize_template(guard_name)
+ guard_class = ::Guard.get_guard_class(guard_name, true)
- ::Guard::UI.info "#{ guard_name } template added to Guardfile, feel free to edit it"
- else
- const_name = guard_name.downcase.gsub('-', '')
- UI.error "Could not load 'guard/#{ guard_name.downcase }' or '~/.guard/templates/#{ guard_name.downcase }' or find class Guard::#{ const_name.capitalize }"
+ if guard_class
+ guard_class.init(guard_name)
+ elsif File.exist?(File.join(HOME_TEMPLATES, guard_name))
+ content = File.read('Guardfile')
+ template = File.read(File.join(HOME_TEMPLATES, guard_name))
+
+ File.open('Guardfile', 'wb') do |f|
+ f.puts(content)
+ f.puts("")
+ f.puts(template)
end
+
+ ::Guard::UI.info "#{ guard_name } template added to Guardfile, feel free to edit it"
+ else
+ const_name = guard_name.downcase.gsub('-', '')
+ UI.error "Could not load 'guard/#{ guard_name.downcase }' or '~/.guard/templates/#{ guard_name.downcase }' or find class Guard::#{ const_name.capitalize }"
end
end
+ # Adds the templates of all installed Guard implementations
+ # to an existing Guardfile.
+ #
+ # @see Guard::CLI.init
+ #
+ def initialize_all_templates
+ guard_gem_names.each {|g| initialize_template(g) }
+ end
+
# Initialize the Guard singleton:
#
# - Initialize the internal Guard state.
View
@@ -104,16 +104,25 @@ def version
desc 'init [GUARD]', 'Generates a Guardfile at the current working directory, or insert the given GUARD to an existing Guardfile'
- # Appends the Guard template to the `Guardfile`, or creates an initial
- # `Guardfile` when no Guard name is passed.
+ # Initializes the templates of all installed Guards and adds them
+ # to the `Guardfile` when no Guard name is passed. When passed
+ # a guard name is does the same but only for that Guard.
#
- # @see Guard.initialize_template
+ # @see Guard::Guard.initialize_template
+ # @see Guard::Guard.initialize_all_templates
#
# @param [String] guard_name the name of the Guard to initialize
#
def init(guard_name = nil)
verify_bundler_presence
- ::Guard.initialize_template(guard_name)
+
+ ::Guard.create_guardfile
+
+ if guard_name.nil?
+ ::Guard::initialize_all_templates
+ else
+ ::Guard.initialize_template(guard_name)
+ end
end
desc 'show', 'Show all defined Guards and their options'
@@ -137,14 +137,29 @@
end
end
- describe '#init' do
- before { ::Guard.stub(:initialize_template) }
+ describe '#init', :focus do
+ before do
+ ::Guard.stub(:create_guardfile)
+ ::Guard.stub(:initialize_all_templates)
+ end
- it 'delegates to Guard::DslDescriber.list' do
- ::Guard.should_receive(:initialize_template)
+ it 'creates a Guardfile by delegating to Guard.create_guardfile' do
+ ::Guard.should_receive(:create_guardfile)
+ subject.init
+ end
+
+ it 'initializes the templates of all installed Guards by delegating to Guard.initialize_all_templates' do
+ ::Guard.should_receive(:initialize_all_templates)
subject.init
end
+ context 'when passed a guard name' do
+ it 'initializes the template of the passed Guard by delegating to Guard.initialize_template' do
+ ::Guard.should_receive(:initialize_template).with('rspec')
+ subject.init 'rspec'
+ end
+ end
+
context 'when running with Bundler' do
before do
@bundler_env = ENV['BUNDLE_GEMFILE']
View
@@ -7,73 +7,82 @@
File.exists?(Guard::GUARDFILE_TEMPLATE).should be_true
end
- describe ".initialize_template" do
+ describe ".create_guardfile" do
before { Dir.stub(:pwd).and_return "/home/user" }
- context "with a Guard name" do
- class Guard::Foo < Guard::Guard
+ context "with an existing Guardfile" do
+ before { File.should_receive(:exist?).and_return true }
+
+ it "does not copy the Guardfile template or notify the user" do
+ ::Guard::UI.should_not_receive(:info).with('Writing new Guardfile to /home/user/Guardfile')
+ FileUtils.should_not_receive(:cp).with(an_instance_of(String), 'Guardfile')
+
+ subject.create_guardfile
end
+ end
- context "with an existing Guardfile" do
- before { File.should_receive(:exist?).and_return true }
+ context "without an existing Guardfile" do
+ before { File.should_receive(:exist?).and_return false }
- it "initializes the Guard" do
- Guard::Foo.should_receive(:init)
- Guard.initialize_template('foo')
- end
+ it "copies the Guardfile template and notifies the user" do
+ ::Guard::UI.should_receive(:info)
+ FileUtils.should_receive(:cp)
+
+ subject.create_guardfile
end
+ end
+ end
- context "without an existing Guardfile" do
- before { File.should_receive(:exist?).and_return false }
+ describe ".initialize_template" do
+ context 'with an installed Guard implementation' do
+ let(:foo_guard) { double('Guard::Foo').as_null_object }
- it "copies the Guardfile template and initializes the Guard" do
- ::Guard::UI.should_receive(:info).with("Writing new Guardfile to /home/user/Guardfile")
- FileUtils.should_receive(:cp).with(an_instance_of(String), 'Guardfile')
- Guard::Foo.should_receive(:init)
- Guard.initialize_template('foo')
- end
+ before { ::Guard.should_receive(:get_guard_class).and_return(foo_guard) }
+
+ it "initializes the Guard" do
+ foo_guard.should_receive(:init)
+ subject.initialize_template('foo')
end
+ end
- context "with a user defined template" do
- let(:template) { File.join(Guard::HOME_TEMPLATES, '/bar') }
+ context "with a user defined template" do
+ let(:template) { File.join(Guard::HOME_TEMPLATES, '/bar') }
- before {
- File.should_receive(:exist?).with('Guardfile').and_return false
- File.should_receive(:exist?).with(template).and_return true
- }
+ before { File.should_receive(:exist?).with(template).and_return true }
- it "copies the Guardfile template and initializes the Guard" do
- FileUtils.should_receive(:cp).with(an_instance_of(String), 'Guardfile')
- File.should_receive(:read).with('Guardfile').and_return 'Guardfile content'
- File.should_receive(:read).with(template).and_return 'Template content'
- io = StringIO.new
- File.should_receive(:open).with('Guardfile', 'wb').and_yield io
- Guard.initialize_template('bar')
- io.string.should eql "Guardfile content\n\nTemplate content\n"
- end
+ it "copies the Guardfile template and initializes the Guard" do
+ File.should_receive(:read).with('Guardfile').and_return 'Guardfile content'
+ File.should_receive(:read).with(template).and_return 'Template content'
+ io = StringIO.new
+ File.should_receive(:open).with('Guardfile', 'wb').and_yield io
+ subject.initialize_template('bar')
+ io.string.should eql "Guardfile content\n\nTemplate content\n"
end
end
- context "without a Guard name" do
- context "with an existing Guardfile" do
- before { File.should_receive(:exist?).and_return true }
+ context "when the passed guard can't be found" do
+ before { File.should_receive(:exist?).and_return false }
- it "shows an error" do
- Guard.should_receive(:exit).with 1
- ::Guard::UI.should_receive(:error).with("Guardfile already exists at /home/user/Guardfile")
- Guard.initialize_template
- end
+ it "notifies the user about the problem" do
+ ::Guard::UI.should_receive(:error).with(
+ "Could not load 'guard/foo' or '~/.guard/templates/foo' or find class Guard::Foo"
+ )
+ subject.initialize_template('foo')
end
+ end
+ end
- context "without an existing Guardfile" do
- before { File.should_receive(:exist?).and_return false }
+ describe ".initialize_all_templates" do
+ let(:guards) { ['rspec', 'spork', 'phpunit'] }
- it "copies the Guardfile template" do
- ::Guard::UI.should_receive(:info).with("Writing new Guardfile to /home/user/Guardfile")
- FileUtils.should_receive(:cp).with(an_instance_of(String), 'Guardfile')
- Guard.initialize_template
- end
+ before { subject.should_receive(:guard_gem_names).and_return(guards) }
+
+ it "calls Guard.initialize_template on all installed guards" do
+ guards.each do |g|
+ subject.should_receive(:initialize_template).with(g)
end
+
+ subject.initialize_all_templates
end
end

0 comments on commit d2cdff9

Please sign in to comment.