Class: Spree::FileUtilz

Inherits:
Object show all
Defined in:
lib/spree/file_utilz.rb

Class Method Summary collapse

Class Method Details

.mirror_files(source, destination, create_backups = false) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spree/file_utilz.rb', line 17

def self.mirror_files(source, destination, create_backups = false)
  # TODO: use Rake::FileList#pathmap?    
  if File.directory?(source)
    source_files  = Dir[source + "/**/*"]
    source_dirs   = source_files.select { |d| File.directory?(d) }
    source_files -= source_dirs
  elsif File.exist? source
    source_dirs   = []
    source_files  = [source]
    source        = File.dirname source
  else
    raise "Could not mirror #{source} - entity does not exist"
  end

  unless source_files.empty?
    base_target_dir = File.join(destination)
    base_target_dir = File.dirname base_target_dir unless File.directory? base_target_dir
    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(base_target_dir, 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(base_target_dir, file.gsub(source, ''))
      unless File.exist?(target) && self.same_contents(file, target)
        # WAS FileUtils.identical?(file, target), but we want to test contents too
        if create_backups && File.exist?(target)
          File.rename(target, target + '~') 
        end
        FileUtils.cp(file, target)
      end 
    rescue Exception => e
      raise "Could not copy #{file} to #{target}: \n" + e 
    end
  end  
end

.mirror_with_backup(source, destination) ⇒ Object

added: do mirroring with backup saving turned on



13
14
15
# File 'lib/spree/file_utilz.rb', line 13

def self.mirror_with_backup(source, destination)
  self.mirror_files(source, destination, true)
end

.same_contents(source, destination) ⇒ Object

for windows users…



65
66
67
# File 'lib/spree/file_utilz.rb', line 65

def self.same_contents(source,destination)
  File.read(source) == File.read(destination)
end