Permalink
Browse files

Merge pull request #130 from ianwhite/master

Adds ignore_paths option to DSL [#104] [#129]
  • Loading branch information...
2 parents c922905 + d5b4c4e commit 0d3acdf94741ecedca520ce798bd923b32559677 Rémy Coutable committed Sep 1, 2011
Showing with 60 additions and 6 deletions.
  1. +3 −0 README.md
  2. +4 −0 lib/guard/dsl.rb
  3. +13 −5 lib/guard/listener.rb
  4. +12 −0 spec/guard/dsl_spec.rb
  5. +28 −1 spec/guard/listener_spec.rb
View
@@ -255,10 +255,13 @@ Optional:
* The `#watch` method allows you to define which files are supervised by this guard. An optional block can be added to overwrite the paths sent to the guard's `#run_on_change` method or to launch any arbitrary command.
* The `#group` method allows you to group several guards together. Groups to be run can be specified with the Guard DSL option `--group` (or `-g`). This comes in handy especially when you have a huge Guardfile and want to focus your development on a certain part. Guards that don't belong to a group are considered global and are always run.
+* The `#ignore_paths` method allows you to ignore top level directories altogether. This comes is handy when you have large amounts of non-source data in you project. By default .bundle, .git, log, tmp, and vendor are ignored. Currently it is only possible to ignore the immediate descendants of the watched directory.
Example:
``` ruby
+ignore_paths 'foo', 'bar'
+
group 'backend' do
guard 'bundler' do
watch('Gemfile')
View
@@ -134,5 +134,9 @@ def watch(pattern, &action)
@watchers << ::Guard::Watcher.new(pattern, action)
end
+ def ignore_paths(*paths)
+ UI.info "Ignoring paths: #{paths.join(', ')}"
+ ::Guard.listener.ignore_paths.push(*paths)
+ end
end
end
View
@@ -9,8 +9,9 @@ module Guard
autoload :Polling, 'guard/listeners/polling'
class Listener
+ DefaultIgnorePaths = %w[. .. .bundle .git log tmp vendor]
- attr_reader :directory
+ attr_reader :directory, :ignore_paths
def self.select_and_init(*a)
if mac? && Darwin.usable?
@@ -29,6 +30,8 @@ def initialize(directory=Dir.pwd, options={})
@directory = directory.to_s
@sha1_checksums_hash = {}
@relativize_paths = options.fetch(:relativize_paths, true)
+ @ignore_paths = DefaultIgnorePaths
+ @ignore_paths |= options[:ignore_paths] if options[:ignore_paths]
update_last_event
end
@@ -77,14 +80,19 @@ def relativize_paths(paths)
def relativize_paths?
!!@relativize_paths
end
+
+ # return children of the passed dirs that are not in the ignore_paths list
+ def exclude_ignored_paths(dirs, ignore_paths = self.ignore_paths)
+ Dir.glob(dirs.map { |d| "#{d.sub(%r{/+$}, '')}/*" }, File::FNM_DOTMATCH).reject do |path|
+ ignore_paths.include?(File.basename(path))
+ end
+ end
private
def potentially_modified_files(dirs, options={})
- paths = Dir.glob(dirs.map { |d| "#{d.sub(%r{/+$}, '')}/*" }, File::FNM_DOTMATCH).reject do |path|
- %w[. .. .bundle .git log tmp vendor].include?(File.basename(path))
- end
-
+ paths = exclude_ignored_paths(dirs)
+
if options[:all]
paths.inject([]) do |array, path|
if File.file?(path)
View
@@ -215,6 +215,18 @@ def self.disable_user_config
end
end
+ describe "#ignore_paths" do
+ disable_user_config
+
+ it "adds the paths to the listener's ignore_paths" do
+ ::Guard.stub!(:listener).and_return(mock('Listener'))
+ ::Guard.listener.should_receive(:ignore_paths).and_return(ignore_paths = ['faz'])
+
+ subject.evaluate_guardfile(:guardfile_contents => "ignore_paths 'foo', 'bar'")
+ ignore_paths.should == ['faz', 'foo', 'bar']
+ end
+ end
+
describe "#group" do
disable_user_config
@@ -160,5 +160,32 @@
end
end
-
+
+ describe "#ignore_paths" do
+ it "defaults to the default ignore paths" do
+ subject.new.ignore_paths.should == Guard::Listener::DefaultIgnorePaths
+ end
+
+ it "can be added to via :ignore_paths option" do
+ listener = subject.new 'path', :ignore_paths => ['foo', 'bar']
+ listener.ignore_paths.should include('foo', 'bar')
+ end
+ end
+
+ describe "#exclude_ignored_paths [<dirs>]" do
+ let(:ignore_paths) { nil }
+ subject { described_class.new(@fixture_path, {:ignore_paths => ignore_paths}) }
+
+ it "returns children of <dirs>" do
+ subject.exclude_ignored_paths(["spec/fixtures"]).should =~ ["spec/fixtures/.dotfile", "spec/fixtures/folder1", "spec/fixtures/Guardfile"]
+ end
+
+ describe "when ignore_paths set to some of <dirs> children" do
+ let(:ignore_paths) { ['Guardfile', '.dotfile'] }
+
+ it "excludes the ignored paths" do
+ subject.exclude_ignored_paths(["spec/fixtures"]).should =~ ["spec/fixtures/folder1"]
+ end
+ end
+ end
end

0 comments on commit 0d3acdf

Please sign in to comment.