Class: Dir

Inherits:
Object show all
Defined in:
lib/roebe/core/dir.rb

Overview

#

require ‘roebe/core/dir.rb’

#

CHANGELOG zu class Dir

10.06.2006: Added exists?
04.11.2005: Hinzugefügt Dir.empty? "somedir"
            returned true wenn ein dir leer is.
#

Class Method Summary collapse

Class Method Details

.chdir_if_exists(input) ⇒ Object

#

Dir.chdir_if_exists

change dir if it exists, and only if it exists

Dir.chdir_if_exists
#


98
99
100
# File 'lib/roebe/core/dir.rb', line 98

def chdir_if_exists(input)
  Dir.chdir(input) if File.exist?(input) 
end

.content(url = Dir.pwd) ⇒ Object

#

alias to entries

#


105
106
107
# File 'lib/roebe/core/dir.rb', line 105

def content(url = Dir.pwd)
  Dir.entries(url)
end

.delete_if_exists(dir, be_verbose = true) ⇒ Object

#

Dir.delete_if_exists

Remove a directory if it exists via this method here.

Usage Example:

Dir.delete_if_exists this_dir
#


68
69
70
71
72
73
# File 'lib/roebe/core/dir.rb', line 68

def self.delete_if_exists(dir, be_verbose = true)
  if File.exist? dir
    puts "Deleting Directory #{dir}" if be_verbose
    FileUtils.rm_rf dir
  end
end

.empty?(which_dir = Dir.pwd) ⇒ Boolean

#

empty?

Returns true if a dir is empty, i.e. if there is no file in it.

Dir.empty?
#

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/roebe/core/dir.rb', line 87

def empty?(which_dir = Dir.pwd)
  _ = Dir[which_dir+'/**']
  _ = _.size < 1 ? true : false
end

.mkdir_unless_exists(name_of_directory, mode = 0755) ⇒ Object

#

Dir.mkdir_unless_exists

Create a dir only if it does not exist already.

Usage example:

Dir.mkdir_unless_exists
#


51
52
53
54
55
56
57
58
# File 'lib/roebe/core/dir.rb', line 51

def self.mkdir_unless_exists(name_of_directory, mode = 0755)
  if File.exist?(name_of_directory)
    # e '  - (Harmless Notice. You can ignore this.) Cannot create directory, '+
    # "Directory #{name_of_directory} already exists."
  else
    Dir.mkdir(name_of_directory, mode)
  end
end

.mkdir_unless_exists_and_chdir(name_of_directory, mode = 0755) ⇒ Object

#

Dir.mkdir_unless_exists_and_chdir

Wrapper to the above. Dir.mkdir_unless_exists_and_chdir

#


36
37
38
39
# File 'lib/roebe/core/dir.rb', line 36

def self.mkdir_unless_exists_and_chdir(name_of_directory, mode = 0755)
  mkdir_unless_exists(name_of_directory, mode)
  Dir.chdir(name_of_directory)
end

.recursive(dir = Dir.pwd) ⇒ Object

#

Dir.recursive

A handy function to list a directory recursively.

#


114
115
116
117
118
119
120
121
122
123
# File 'lib/roebe/core/dir.rb', line 114

def Dir.recursive(dir = Dir.pwd)
  list = Array.new
  foreach(dir) { |entry|
    if entry != '.' and entry != '..' and entry != '.Build' and entry != ".Filelist"
      list << "#{dir}/#{entry}"
      list += recursive("#{dir}/#{entry}") if FileTest.directory?("#{dir}/#{entry}")
    end
  }
  list
end

.size?(subdir_part) ⇒ Boolean

#

Dir.size?

Gives you the size of the given subdir_part. Will invoke number_with_delimiter to get a nicer listing - see below for that.

Usage Examples:

Dir.size?(ENV['J']) # => 1_396_436_232
#

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/roebe/core/dir.rb', line 24

def self.size?(subdir_part)
  subdir_part = subdir_part+'/**/*'
  size = Dir[subdir_part].inject(0) { |sum, entry| sum + File.size(entry) }
  return size.to_s.number_with_delimiter
end