Class: Mint::Copier

Inherits:
Object
  • Object
show all
Defined in:
lib/proutils/mint/copier.rb

Overview

Mint Copier provides a factility for performing interactive, managed copies.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, options = nil) ⇒ Copier

New copier.



72
73
74
75
76
77
78
79
80
81
# File 'lib/proutils/mint/copier.rb', line 72

def initialize(source, destination, options=nil)
  @source     = [source].flatten
  @destination = destination

  @actions     = []

  options.each do |k,v|
    send("#{k}=",v)
  end
end

Instance Attribute Details

#actionsObject (readonly)

Stores actions to be performed upon commit.



68
69
70
# File 'lib/proutils/mint/copier.rb', line 68

def actions
  @actions
end

#destinationObject

Directory (or a file, if copying one file) in which to store copied source.



52
53
54
# File 'lib/proutils/mint/copier.rb', line 52

def destination
  @destination
end

#dryrunObject

Just pretend to do the commit action.



56
57
58
# File 'lib/proutils/mint/copier.rb', line 56

def dryrun
  @dryrun
end

#forceObject

Force provided an extra “dangerous” option of “(W)rite all”.



60
61
62
# File 'lib/proutils/mint/copier.rb', line 60

def force
  @force
end

#skipObject

Automatically skip all overwrites.



64
65
66
# File 'lib/proutils/mint/copier.rb', line 64

def skip
  @skip
end

#sourceObject

Source paths to copy. This can be a glob or an array of globs.



48
49
50
# File 'lib/proutils/mint/copier.rb', line 48

def source
  @source
end

#systemObject

Look for source file(s) in system locations?



44
45
46
# File 'lib/proutils/mint/copier.rb', line 44

def system
  @system
end

Instance Method Details

#common_lookupObject

Non-system lookup.



118
119
120
121
122
123
124
125
126
127
# File 'lib/proutils/mint/copier.rb', line 118

def common_lookup
  copies = []
  source.each do |src|
    files = Dir.glob(File.join(path, src))
    unless files.empty?
      copies << [nil, files]
    end
  end
  copies
end

#copiesObject

Files to copy.



91
92
93
94
95
96
97
# File 'lib/proutils/mint/copier.rb', line 91

def copies
  if system?
    system_lookup
  else
    common_lookup
  end
end

#copyObject

Copy files.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/proutils/mint/copier.rb', line 131

def copy
  puts "KEY: (d)iff (r)eplace (s)kip (a)ll (q)uit"
  copies.each do |dir, file|
    path = dir ? File.join(dir, file) : file
    if File.file?(path)
      copy_file(dir, file)
    elsif File.directory?(path)
      dirs, files = *partition(path)
      # make empty directories
      dirs.each do |d|
        if File.directory?(File.join(destination,d))
          skip_dir(d)
        else
          pth = File.join(path,d)
          entries = Dir.entries(pth) - IGNORE
          make_dir(path, d) if entries.empty?
        end
      end
      # copy files in directories
      files.each do |f|
        copy_file(path, f)
      end
    else
      raise ArgumentError, "unsupported file object -- #{path}"
    end
  end
  commit
end

#dryrun?Boolean

Returns:

  • (Boolean)


85
# File 'lib/proutils/mint/copier.rb', line 85

def dryrun?  ; @dryrun  ; end

#force?Boolean

Returns:

  • (Boolean)


86
# File 'lib/proutils/mint/copier.rb', line 86

def force?   ; @force   ; end

#skip?Boolean

Returns:

  • (Boolean)


87
# File 'lib/proutils/mint/copier.rb', line 87

def skip?    ; @skip    ; end

#system?Boolean

Returns:

  • (Boolean)


83
# File 'lib/proutils/mint/copier.rb', line 83

def system?  ; @system  ; end

#system_lookupObject

Look up file in system locations.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/proutils/mint/copier.rb', line 101

def system_lookup
  copies = []
  Mint.paths.each do |path|
    source.each do |src|
      jpath = File.join(path, src)
      files = Dir.glob(jpath)
      files.each do |file|
        file = file.sub(path+'/','')
        copies << [path, file]
      end
    end
  end
  copies
end