Permalink
Browse files

Merge pull request #128 from tpope/user_config

Add a user guard config
  • Loading branch information...
2 parents b144514 + b59d6ac commit c92290526bc10b6a43ce6e0dbf39077b223346a4 @thibaudgg thibaudgg committed Aug 31, 2011
Showing with 53 additions and 4 deletions.
  1. +15 −0 README.md
  2. +11 −3 lib/guard/dsl.rb
  3. +5 −1 spec/guard/dsl_describer_spec.rb
  4. +22 −0 spec/guard/dsl_spec.rb
View
@@ -331,6 +331,21 @@ Group frontend:
livereload
```
+User config file
+----------------
+
+If a .guard.rb is found in your home directory, it will be appended to
+the Guardfile. This can be used for tasks you want guard to handle but
+other users probably don't. For example, indexing your source tree with
+[Ctags](http://ctags.sourceforge.net):
+
+``` ruby
+guard 'shell' do
+ watch(%r{^(?:app|lib)/.+\.rb$}) { `ctags -R` }
+end
+
+```
+
Create a new guard
------------------
View
@@ -7,7 +7,8 @@ def evaluate_guardfile(options = {})
options.is_a?(Hash) or raise ArgumentError.new("evaluate_guardfile not passed a Hash!")
@@options = options.dup
- instance_eval_guardfile(fetch_guardfile_contents)
+ fetch_guardfile_contents
+ instance_eval_guardfile(guardfile_contents_with_user_config)
UI.error "No guards found in Guardfile, please add at least one." if !::Guard.guards.nil? && ::Guard.guards.empty?
end
@@ -72,14 +73,17 @@ def fetch_guardfile_contents
UI.error "The command file(#{@@options[:guardfile]}) seems to be empty."
exit 1
end
-
- guardfile_contents
end
def guardfile_contents
@@options ? @@options[:guardfile_contents] : ""
end
+ def guardfile_contents_with_user_config
+ config = File.read(user_config_path) if File.exist?(user_config_path)
+ [guardfile_contents, config].join("\n")
+ end
+
def guardfile_path
@@options ? @@options[:guardfile_path] : ""
end
@@ -102,6 +106,10 @@ def home_guardfile_path
File.expand_path(File.join("~", ".Guardfile"))
end
+ def user_config_path
+ File.expand_path(File.join("~", ".guard.rb"))
+ end
+
end
def group(name, &guard_definition)
@@ -1,7 +1,11 @@
require 'spec_helper'
describe Guard::DslDescriber do
- before(:each) { ::Guard.stub!(:guards).and_return([mock('Guard')]) }
+ before(:each) do
+ ::Guard.stub!(:guards).and_return([mock('Guard')])
+ user_config_path = File.expand_path(File.join('~', '.guard.rb'))
+ File.stub(:exist?).with(user_config_path) { false }
+ end
subject { described_class }
View
@@ -5,12 +5,18 @@
before(:each) do
@local_guardfile_path = File.join(Dir.pwd, 'Guardfile')
@home_guardfile_path = File.expand_path(File.join("~", ".Guardfile"))
+ @user_config_path = File.expand_path(File.join("~", ".guard.rb"))
::Guard.stub!(:options).and_return(:debug => true)
::Guard.stub!(:guards).and_return([mock('Guard')])
end
+ def self.disable_user_config
+ before(:each) { File.stub(:exist?).with(@user_config_path) { false } }
+ end
+
describe "it should select the correct data source for Guardfile" do
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
+ disable_user_config
it "should use a string for initializing" do
Guard::UI.should_not_receive(:error)
@@ -51,6 +57,15 @@
lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
subject.guardfile_contents.should == "guard :foo"
end
+
+ it 'should append the user config file if present' do
+ fake_guardfile('/abc/Guardfile', "guard :foo")
+ fake_guardfile(@user_config_path, "guard :bar")
+ Guard::UI.should_not_receive(:error)
+ lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
+ subject.guardfile_contents_with_user_config.should == "guard :foo\nguard :bar"
+ end
+
end
it "displays an error message when no Guardfile is found" do
@@ -68,6 +83,7 @@
describe "correctly reads data from its valid data source" do
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
+ disable_user_config
it "reads correctly from a string" do
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
@@ -200,6 +216,8 @@
end
describe "#group" do
+ disable_user_config
+
it "evaluates only the specified string group" do
::Guard.should_receive(:add_guard).with(:pow, [], { :group => :default })
::Guard.should_receive(:add_guard).with(:test, [], { :group => :w })
@@ -242,6 +260,8 @@
end
describe "#guard" do
+ disable_user_config
+
it "loads a guard specified as a quoted string from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], { :group => :default })
@@ -274,6 +294,8 @@
end
describe "#watch" do
+ disable_user_config
+
it "should receive watchers when specified" do
guardfile_with_watchers = "guard 'test' do
watch('a') { 'b' }

0 comments on commit c922905

Please sign in to comment.