Module: RIEL::DirExt::ClassMethods

Defined in:
lib/riel/dir.rb

Instance Method Summary collapse

Instance Method Details

#homeObject



15
16
17
18
19
20
21
22
23
# File 'lib/riel/dir.rb', line 15

def home
  ENV["HOME"] || begin
                   hd = ENV["HOMEDRIVE"]
                   hp = ENV["HOMEPATH"]
                   if hd || hp
                     (hd || "") + (hp || "\\")
                   end
                 end
end

#move_and_copy_files(dir, move_files, copy_files) ⇒ Object

Moves and copies files to the given directory, creating it if it does not exist.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/riel/dir.rb', line 85

def move_and_copy_files dir, move_files, copy_files
  dir.mkdir unless dir.exist?

  move_files.each do |mfile|
    File.move mfile.to_s, dir.to_s
  end

  copy_files.each do |cfile|
    File.copy cfile.to_s, dir.to_s
  end
end

#remove_if_empty(dir, args = Hash.new) ⇒ Object

Removes directories containing no files or files matching only those in args, which are basenames.



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/riel/dir.rb', line 28

def remove_if_empty dir, args = Hash.new
  deletable = args[:deletable] || Array.new
  verbose   = args[:verbose]
  level     = args[:level] || 0

  subargs = args.dup
  subargs[:level] = level + 1

  dir = Pathname.new(dir) unless dir.kind_of?(Pathname)

  if level <= 1 && verbose
    puts "dir: #{dir}"
  end

  if dir.readable?
    dir.children.sort.each do |child|
      if child.exist? && child.directory?
        self.remove_if_empty child, subargs
      end
    end
    
    if dir.expand_path == Pathname.pwd.expand_path
      puts "skipping current directory: #{dir}" if verbose
    else
      can_delete = dir.children.all? do |x| 
        bname = x.basename.to_s
        deletable.any? do |del|
          if del.kind_of? String
            bname == del
          elsif del.kind_of? Regexp
            del.match bname
          else
            false
          end
        end
      end

      if can_delete
        dir.children.each do |x|
          puts "removing file: #{x}" if verbose
          x.delete
        end

        puts "removing directory: #{dir}" if verbose

        dir.delete
      else
        # puts "#{dir} not empty"
      end
    end
  elsif verbose
    puts "#{dir} not readable"
  end
end