Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Create full target path if :mkpath is true #10

wants to merge 3 commits into from

2 participants


Yet another feature I needed, should be possible to let Guard::Copy create the full target path if needed, conveniently piggybacking on existing :mkpath option. Let me know if you think this is a good way of implementing it, but it does the job perfectly for me.

My use case is bombing out the entire build folder on each Guard start to make sure that it's clean, but in this case I need Guard::Copy to be able to create stuff from scratch.


I have to think about this one a bit.

Because the pattern can be a glob, this would fail in that case. It's probably not that big of a deal, but I don't like having options that can fail depending on how they're used. Maybe check the pattern for wildcard characters, and issue a warning in that case.


Missed that one, I should have RTFM :)

Just pushed a commit, checking for the presence of :glob parameter now and also the order of parameters is more logical now.


Having just reread your comment, I'm not checking for the wildcard in pattern, is it possible to have it without the :glob option?


The :glob option controls how multiple matches are handled, with :all being the default if nothing is specified.

So if you had :mkpath on and used a glob, you'd end up with `my/path/*', or whatever your pattern was.

I think it's pretty unlikely that someone would use both options, but I'd like to guard against it. Perhaps a Target#glob? method that checks if the pattern contains certain characters.

Just documenting it might be good enough, but I'd like to think about it some before doing that.

@marcisme marcisme referenced this pull request from a commit
@marcisme create target directory when :mkpath is set
This change addresses issue #10.

  * move resolution failure warning to Target class
  * use Target#pattern without checking file system when :mkpath is true

Let me know if what's on master now works for you.

I may still add some pattern checks before releasing, but the functionality should be the same.


Oh nice, just tried and it works perfectly for me!

Any chance getting a new version of the gem out once you're happy with these new features? :) :shipit:


I've released 0.0.7. Thanks for your contributions.

@marcisme marcisme closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 4, 2012
  1. @daaain

    Creating target folder path if it doesn't exist.

    Daniel Demmel authored daaain committed
  2. @daaain
Commits on Dec 6, 2012
  1. @daaain

    Won't try to create target folder if it's using a glob pattern; also …

    daaain authored
    …formatting spec to use 'context'.
This page is out of date. Refresh to see the latest.
4 lib/guard/copy.rb
@@ -150,6 +150,10 @@ def validate_to_file(to_file)
def resolve_targets
@targets.each do |target|
+ if !options[:glob] && options[:mkpath] && !
+"creating directory #{target.pattern}") if options[:verbose]
+ FileUtils.mkpath(target.pattern)
+ end
unless target.resolve
UI.warning("Guard::Copy - '#{target.pattern}' does not match a valid directory")
5 lib/guard/copy/target.rb
@@ -37,6 +37,11 @@ def absolute?
+ # @return [String] path pattern
+ def pattern
+ @pattern
+ end
11 spec/guard/copy_spec.rb
@@ -127,6 +127,17 @@ module Guard
+ context 'when :mkpath option is true' do
+ it 'creates full path for missing :to directory' do
+ dir('source')
+ guard =[], :from => 'source', :to => 'target/my/long/path', :mkpath => true, :verbose => true)
+ UI.should_receive(:info).with("creating directory target/my/long/path")
+ UI.should_not_receive(:warning).with("Guard::Copy - 'target/my/long/path' does not match a valid directory")
+ guard.start
+ File.should be_directory('target/my/long/path')
+ end
+ end
it 'throws :task_has_failed when :to contains a file' do
Something went wrong with that request. Please try again.