Module: DsUtils

Defined in:
lib/ds_utils.rb,
lib/ds_utils/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.create_do_fileObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ds_utils.rb', line 45

def self.create_do_file
  @do_file = "flatten_directory_#{timestamp}.rb"
  
  File.open(@do_file, 'w', 0744) {|file|
    file.puts "#!/usr/bin/env ruby"
    @move_files.each {|ll|
      file.puts "File.rename '#{ll}', '#{move_to_name(ll)}'"
    }
    @subdirectories.sort{|a,b| b.count(File::SEPARATOR) - a.count(File::SEPARATOR)}.each {|sd|
      file.puts "Dir.rmdir('#{sd}')"
    }
  }
  
end

.create_undo_fileObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ds_utils.rb', line 60

def self.create_undo_file
  @undo_file = "unflatten_directory_#{timestamp}.rb"
  
  File.open(@undo_file, 'w', 0744) {|file|
    file.puts "#!/usr/bin/env ruby"
    file.puts "require 'fileutils'"
    @subdirectories.each {|sd|
      file.puts "FileUtils.mkdir_p '#{sd}' unless Dir.exist?('#{sd}')"
    } 
    @move_files.each {|ll|
      file.puts "File.rename '#{move_to_name(ll)}', '#{ll}'"
    }
  }
end

.flatten(thisdir, allowed_extensions_arr = []) ⇒ Object

Move tiles from subdirectories of topdir to topdir



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ds_utils.rb', line 5

def self.flatten(thisdir,allowed_extensions_arr=[])
  @do_listing = []
  @undo_listing = []
  @move_files = []
  
  flatten_recurse(thisdir,allowed_extensions_arr)
  
  @subdirectories = @move_files.map{|f| File.dirname(f)}.uniq
  
  create_do_file
  create_undo_file
  
  puts "Please examine the contents of file #{@do_file} and then execute it with the command:"
  puts "          ./#{@do_file}"
  puts "To undo the changes, please execute:"
  puts "          ./#{@undo_file}"

  return [@do_file, @undo_file]
end

.flatten_recurse(thisdir, allowed_extensions_arr = []) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ds_utils.rb', line 26

def self.flatten_recurse(thisdir,allowed_extensions_arr=[])
  Dir.foreach(thisdir) do |filename|
    abspath = File.join(thisdir,filename)
    if (!filename.eql?(".") and !filename.eql?("..") and (allowed_extensions_arr.empty? || allowed_extensions_arr.include?(File.extname(filename))))
      # If the listed file is a directory, then flatten it and move contents up to this directory
      if File.directory?(abspath)
        #full_file_path = File.join(File.absolute_path(thisdir),filename)
        self.flatten_recurse(abspath,allowed_extensions_arr)
        #rename_and_move_contents(abspath,File.absolute_path(thisdir),allowed_extensions_arr)
      else 
        @move_files << abspath
      end
    else
      puts "skipped: #{abspath}"
    end
  end
end

.move_to_name(filename) ⇒ Object



75
76
77
78
# File 'lib/ds_utils.rb', line 75

def self.move_to_name(filename)
  split_string_array = filename.split(File::SEPARATOR)
  return split_string_array[0] + File::SEPARATOR + split_string_array[1..-1].join('_')
end

.timestampObject



80
81
82
# File 'lib/ds_utils.rb', line 80

def self.timestamp
  return Time.now.strftime("%Y_%m_%d_%Y%m%dT%H%M%S%z")
end