Skip to content
Browse files

support '.' for root directory in :from

It was not currently possible to watch the root directory of a project,
as raised in issue #15. This change adds a magic string value of '.' to
identify that everything from the root project down should be copied,
which requires no substitution in the target paths.
  • Loading branch information...
1 parent 9840df1 commit 67be71b5d5120140c894232884acf5610d9ede87 @marcisme committed Jul 20, 2013
Showing with 25 additions and 2 deletions.
  1. +1 −1 README.md
  2. +11 −1 lib/guard/copy.rb
  3. +13 −0 spec/guard/copy_spec.rb
View
2 README.md
@@ -57,7 +57,7 @@ the directory specified by the `:from` option to that specified by the
### List of available options:
``` ruby
-:from => 'source' # directory to copy files from
+:from => 'source' # directory to copy files from; use '.' for root directory
:to => 'target' # directory or glob to copy files to; string or array
:glob => :newest # how to handle globs; default: :all
# :newest - copy to only the newest directory
View
12 lib/guard/copy.rb
@@ -5,6 +5,8 @@
module Guard
class Copy < Guard
+ COPY_FROM_PROJECT_ROOT_MAGIC_STRING = '.'
+
autoload :Target, 'guard/copy/target'
attr_reader :targets
@@ -83,12 +85,20 @@ def target_paths
def with_all_target_paths(paths)
paths.each do |from_path|
target_paths.each do |target_path|
- to_path = from_path.sub(@options[:from], target_path)
+ to_path = substitute_target_path(from_path, target_path)
yield(from_path, to_path)
end
end
end
+ def substitute_target_path(from_path, target_path)
+ if @options[:from] == COPY_FROM_PROJECT_ROOT_MAGIC_STRING
+ target_path
+ else
+ from_path.sub(@options[:from], target_path)
+ end
+ end
+
def normalize_watcher(watcher, from)
unless watcher.pattern.source =~ %r{^\^#{from}/.*}
normalized_source = watcher.pattern.source.sub(%r{^\^?(#{from})?/?}, "^#{from}/")
View
13 spec/guard/copy_spec.rb
@@ -267,6 +267,19 @@ module Guard
File.should be_file('t2/foo')
end
+ it 'copies files from the root directory to target directories' do
+ file('foo')
+ dir('t1')
+ dir('t2')
+ guard = Copy.new([], :from => '.', :to => ['t1', 't2'])
+ guard.start
+
+ guard.run_on_changes(['foo'])
+
+ File.should be_file('t1/foo')
+ File.should be_file('t2/foo')
+ end
+
it 'warns when a directory containing a "." is changed' do
dir('source/dotted.name')
dir('target/dotted.name')

0 comments on commit 67be71b

Please sign in to comment.
Something went wrong with that request. Please try again.