Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy files preserving directory structure with rake

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>
like image 224
Vladimir Avatar asked Sep 30 '12 20:09

Vladimir


2 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
like image 66
Mark Thomas Avatar answered Sep 21 '22 12:09

Mark Thomas


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}")
like image 40
David Grayson Avatar answered Sep 24 '22 12:09

David Grayson