Copy files preserving directory structure using rake

My goal is to copy the set of files specified by the template to the destination directory. Files in the source directory may have sub-folders.

I tried:

cp_r(Dir.glob('**/*.html'), @target_dir): 

and

 cp_r(FileList['**/*.html'], @target_dir): 

but does not work.

it only works when I do something like:

 cp_r(Dir['.'], @target_dir): 

But I only need to copy the * .html files.

I need what

 cp --parents 

Team executes

Any tips using existing Ruby / Rake methods?

UPDATE It seems that it is easier to do with Ant, it is not possible using the Ruby / Rake stack - maybe I will need to learn something else. I do not want to write my own code to make it work in Ruby. I just thought of Ruby / Rake as a suitable solution for this.

UPDATE 2 Here's how I do it with Ant

 <target name="buildeweb" description="Builds web site" depends="clean"> <mkdir dir="${build.dir.web}" /> <copy todir="${build.dir.web}" verbose="true"> <fileset dir="${source.dir.web}"> <include name="**/*.html" /> <include name="**/*.htm" /> </fileset> </copy> <chmod perm="a+x"> <fileset dir="${build.dir.web}"> <include name="**/*.html" /> <include name="**/*.htm" /> </fileset> </chmod> </target> 
+7
source share
3 answers

If you want pure Ruby, you can do this (with a little help from FileUtils in the standard library).

 require 'fileutils' Dir.glob('**/*.html').each do |file| dir, filename = File.dirname(file), File.basename(file) dest = File.join(@target_dir, dir) FileUtils.mkdir_p(dest) FileUtils.copy_file(file, File.join(dest,filename)) end 
+5
source

I have not heard of cp --parents , but if it does what you need, then there is no shame in just using it from your Rakefile, for example:

 system("cp --parents #{your} #{args}") 
0
source

This may be useful:

 # copy "files" to "dest" with any sub-folders after "src_root". def copy_and_preserve files, dest, src_root files.each {|f| f.slice! src_root # the files without src_root dir dest_dir = File.dirname(File.join(dest, f)) FileUtils.mkdir_p dest_dir # make dest dir FileUtils.cp(File.join(src_root, f), dest_dir, {:verbose => true}) } end 
0
source

All Articles