Class: ImagePicker::AssetCopier

Inherits:
Object
  • Object
show all
Defined in:
lib/image_picker/asset_copier.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.deleted_filesObject

Returns the value of attribute deleted_files.



13
14
15
# File 'lib/image_picker/asset_copier.rb', line 13

def deleted_files
  @deleted_files
end

.destinationObject

Returns the value of attribute destination.



13
14
15
# File 'lib/image_picker/asset_copier.rb', line 13

def destination
  @destination
end

.sourceObject

Returns the value of attribute source.



13
14
15
# File 'lib/image_picker/asset_copier.rb', line 13

def source
  @source
end

Class Method Details

.compare(file1, file2) ⇒ Object



54
55
56
57
# File 'lib/image_picker/asset_copier.rb', line 54

def self.compare(file1, file2)
  File.exists?(file1) && File.exists?(file2) &&
    Digest::MD5.hexdigest(File.read(file1)) == Digest::MD5.hexdigest(File.read(file2))
end

.copy(plugin_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/image_picker/asset_copier.rb', line 16

def self.copy(plugin_name)
  begin
    each_path do |path, dest_path, short_path| 
      if File.directory?(path)
        unless File.exists?(dest_path)
          FileUtils.mkdir_p(dest_path)
          log "Creating directory #{short_path} for #{plugin_name}"
        end
      elsif !compare(path, dest_path)
        FileUtils.cp(path, dest_path)
        log "Copying #{short_path} from #{plugin_name}"
      end
    end
  rescue Exception => e
    log "Error trying to copy files: #{e.inspect}"
    raise e
  end
  print_deletion_warnings(plugin_name)
end

.each_pathObject



78
79
80
81
82
83
84
# File 'lib/image_picker/asset_copier.rb', line 78

def self.each_path
  paths.each do |path|
    dest_path = path.gsub(source, destination)
    short_path = dest_path.gsub("#{destination}/", "")
    yield path, dest_path, short_path
  end
end

.log(msg) ⇒ Object



86
87
88
# File 'lib/image_picker/asset_copier.rb', line 86

def self.log(msg)
  puts msg
end

.pathsObject



68
69
70
71
72
73
74
75
76
# File 'lib/image_picker/asset_copier.rb', line 68

def self.paths
  returning [] do |paths|
    Find.find(source) do |path|
      Find.prune if path =~ /\/\..+/
      Find.prune if path =~ /(CVS|.svn|.git)/
      paths << path
    end
  end
end


59
60
61
62
63
64
65
66
# File 'lib/image_picker/asset_copier.rb', line 59

def self.print_deletion_warnings(plugin_name)
  File.open(deleted_files, "r") do |f|
    f.readlines.reject { |l| l =~ /^#/ || l.strip.blank? }.each do |l|
      log "WARNING: #{l} is no longer required by the #{plugin_name} plugin " <<
          "and can can be safely removed" if File.exists?(l)
    end
  end
end

.warn(plugin_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/image_picker/asset_copier.rb', line 36

def self.warn(plugin_name)
  each_path do |path, dest_path, short_path|
    next if File.directory?(path)
    reinstall = false
    if File.exists?(dest_path)
      unless compare(path, dest_path)
        log "WARNING: #{short_path} is out of date and needs to be reinstalled"
        reinstall = true
      end
    else
      reinstall = true
      log "WARNING: #{short_path} is missing and needs to be installed"
    end
    log "WARNING: Please run rake #{plugin_name}:install" if reinstall
  end
  print_deletion_warnings(plugin_name)
end