Class: StackOperations::MkFlatten
- Inherits:
-
MkTree
- Object
- MkTree
- StackOperations::MkFlatten
show all
- Defined in:
- lib/mk_stack/mk_stack.rb
Instance Method Summary
collapse
Methods inherited from MkTree
#mk_dir_tree, #print_tree
Constructor Details
#initialize(options, args) ⇒ MkFlatten
Returns a new instance of MkFlatten.
203
204
205
206
207
|
# File 'lib/mk_stack/mk_stack.rb', line 203
def initialize(options, args)
@options = options
@target_dir = args[0] || "."
@target_dir = @target_dir.chomp("/")
end
|
Instance Method Details
#build_virtual_tree ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/mk_stack/mk_stack.rb', line 208
def build_virtual_tree
search_dirs = Dir.glob(File.join(@target_dir, "**/_stack_*_*")).select { |d| File.directory?(d) }
root_stacks = Dir.glob(File.join(@target_dir, "_stack_*_*")).select { |d| File.directory?(d) }
all_stacks = (root_stacks + search_dirs).uniq
tree = {}
all_stacks.map { |d| File.basename(d) }.sort.each do |d|
tree[d] = {}
end
tree
end
|
#flatten_moves ⇒ Object
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/mk_stack/mk_stack.rb', line 219
def flatten_moves
search_dirs = Dir.glob(File.join(@target_dir, "**/_stack_*_*")).select { |d| File.directory?(d) }
root_stacks = Dir.glob(File.join(@target_dir, "_stack_*_*")).select { |d| File.directory?(d) }
stacks = (root_stacks + search_dirs).uniq
stacks.sort_by! { |d| -d.count('/') }
moves = []
stacks.each do |src|
dest = File.join(@target_dir, File.basename(src))
moves << [src, dest] unless src == dest
end
moves
end
|
#run ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/mk_stack/mk_stack.rb', line 234
def run
puts "Before flatten:"
puts @target_dir == "." ? "." : "#{File.basename(@target_dir)}/"
print_tree(mk_dir_tree(@target_dir, 1, 999, dirs_only: true))
moves = flatten_moves
puts "\nOperations:"
prefix = @options[:dryrun] ? "[Dry Run] " : ""
if moves.empty?
puts "#{prefix}nothing to flatten"
else
moves.each do |src, dest|
puts "#{prefix}mv #{src} (to) #{dest}"
end
end
puts "\nAfter flatten:"
puts "."
print_tree(build_virtual_tree)
unless @options[:dryrun]
moves.each do |src, dest|
FileUtils.mv(src, dest)
end
puts "\nExecution completed."
end
end
|