Copy files preserving directory structure with rake
Asked Answered
M

3

7

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>
Marylyn answered 30/9, 2012 at 20:49 Comment(2)
You could copy everything, then delete the ones that aren't .html. That would be easier, since you don't need to fiddle with paths.Noman
@d11wtq, this is not a good solution because in this case you need to do two extra things: copy unneeded files, then delete unneeded files.Marylyn
W
7

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
Woken answered 1/10, 2012 at 0:5 Comment(1)
Hi @Mark, I want pure Ruby solution, yeah. But I would like a more cleaner declarative approach. I thought that such automation is possible with Ruby/Rake stack without writing custom code. I would like something more declarative like: cp_r(<all *.html's in the tree>, target) without the need to write any weird methods. Looks like it's impossible with Std. Ruby/Rake stack - and I may need to fallback to bash or Ant.Marylyn
G
3

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}")
Gopherwood answered 30/9, 2012 at 23:49 Comment(1)
Hi @David, thanks for advice. I haven't heard of it before yesterday too :-) I would like a more cleaner solution which doesn't need to call system commands but work in pure Ruby, I thought that this could be solved in Ruby - otherwise I can write bash script instead.Marylyn
H
0

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
Hamilton answered 4/7, 2013 at 2:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.