Class: Spree::FileUtilz

Inherits:
Object show all
Defined in:
lib/plugins/extension_patches/lib/asset_copy.rb

Class Method Summary collapse

Class Method Details

.mirror_files(source, destination) ⇒ Object

A general purpose method to mirror a directory (source) into a destination directory, including all files and subdirectories. Files will not be mirrored if they are identical already (checked via FileUtils#identical?).

Copyright © 2008 James Adam (The MIT License)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/plugins/extension_patches/lib/asset_copy.rb', line 11

def self.mirror_files(source, destination)
  return unless File.directory?(source)

  # TODO: use Rake::FileList#pathmap?    
  source_files = Dir[source + "/**/*"]
  source_dirs = source_files.select { |d| File.directory?(d) }
  source_files -= source_dirs

  unless source_files.empty?
    base_target_dir = File.join(destination)
    FileUtils.mkdir_p(base_target_dir)
  end

  source_dirs.each do |dir|
    # strip down these paths so we have simple, relative paths we can
    # add to the destination
    target_dir = File.join(destination, dir.gsub(source, ''))
    begin        
      FileUtils.mkdir_p(target_dir)
    rescue Exception => e
      raise "Could not create directory #{target_dir}: \n" + e
    end
  end

  source_files.each do |file|
    begin
      target = File.join(destination, file.gsub(source, ''))
      unless File.exist?(target) && FileUtils.identical?(file, target)
        FileUtils.cp(file, target)
      end 
    rescue Exception => e
      raise "Could not copy #{file} to #{target}: \n" + e 
    end
  end  
end