Reputation: 4850
My goal is to copy a set of files specified by a pattern to the target dir. The files in source directory can have subdirs.
I tried:
cp_r(Dir.glob('**/*.html'), @target_dir):
and
cp_r(FileList['**/*.html'], @target_dir):
but neither work.
it only works when I do something like:
cp_r(Dir['.'], @target_dir):
But I need to copy only *.html files not anything else.
I need what
cp --parents
Command does
Any advice using existing Ruby/Rake methods?
UPDATE Looks like thing which is easier to do with Ant, is not possible with Ruby/Rake stack - may be I would need to look into something else. I don't want to write custom code to make it work in Ruby. I just thought about Ruby/Rake as appropriate solution for that.
UPDATE 2 This is 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>
Upvotes: 7
Views: 4854
Reputation: 849
This could 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
Upvotes: 0
Reputation: 37517
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
Upvotes: 7
Reputation: 87406
I haven't heard of cp --parents
, but if it does what you want then there is no shame in just using it from your Rakefile, like this:
system("cp --parents #{your} #{args}")
Upvotes: 3