Class: Dir

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

Class Method Summary collapse

Class Method Details

.files(dir, opts = {}) ⇒ Object

Get all files in a folder ‘Dir.files(’./app/assets’)‘



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/overload/dir.rb', line 14

def self.files dir, opts = {}
  dir = dir.to_s
 
  list = (Dir.entries(dir) - ['.', '..'])
    .sort
    .reject { |el| File.directory?([dir, el].join('/')) }

  if opts[:ext] == false
    list = list.map{|f| f.sub(/\.\w+$/, '') }
  end

  list
end

.find(dir_path, opts = {}) ⇒ Object

Find files in child folders. All lists are allways sorted with idempotent function response. Example: get all js and coffee in ./app/assets and remove ./app and invert folder search list ‘Dir.find(’./app/assets’, ext: [:js, :coffee], root: ‘./app’, hash: true, invert: true)‘ shortuct to remove ./app and not use root param `Dir.find(’./app#assets’, ext: [:js, :coffee])‘



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
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/overload/dir.rb', line 34

def self.find dir_path, opts={}
  opts[:ext] ||= []
  opts[:ext] = [opts[:ext]] unless opts[:ext].is_a?(Array)
  opts[:ext] = opts[:ext].map(&:to_s)

  parts = dir_path.to_s.split('#')

  if parts[1]
    opts[:root] = parts[0] + '/'
    dir_path = dir_path.to_s.sub('#', '/')
  end

  glob = []
  glob.push 'echo'

  folders = ['%s/*' % dir_path]

  unless opts[:shallow]
    folders.push '%s/*/*'           % dir_path
    folders.push '%s/*/*/*'         % dir_path
    folders.push '%s/*/*/*/*'       % dir_path
    folders.push '%s/*/*/*/*/*'     % dir_path
    folders.push '%s/*/*/*/*/*/*'   % dir_path
    folders.push '%s/*/*/*/*/*/*/*' % dir_path
  end

  folders = folders.reverse if opts[:invert]

  glob += folders

  glob.push "| tr ' ' '\n'"

  files = `cd #{Dir.pwd} && #{glob.join(' ')}`
    .split("\n")
    .reject { |_| _.include?('/*') }
    .select { |_| _ =~ /\.\w+$/ }
    .select { |_| opts[:ext].first ? opts[:ext].include?(_.split('.').last) : true }
    .map { |_| opts[:root] ? _.sub(opts[:root], '') : _ }

  if opts[:hash]
    files = files.map do |full|
      parts  = full.split('/')
      file   = parts.pop
      fparts = file.split('.')

      {
        full: full,
        dir: parts.join('/'),
        file: file,
        ext: fparts.pop,
        name: fparts.join('.')
      }.to_hwia
    end
  end

  if block_given?
    files.map { |f| yield(f).gsub('%s', f) }.join(opts[:join] || $/)
  else
    files
  end
end

.folders(dir) ⇒ Object

Get list of folders in a folder ‘Dir.folders(’./app/assets’)‘



4
5
6
7
8
9
10
# File 'lib/overload/dir.rb', line 4

def self.folders dir
  dir = dir.to_s

  (Dir.entries(dir) - ['.', '..'])
    .sort
    .select { |el| File.directory?([dir, el].join('/')) }
end

.mkdir?(name) ⇒ Boolean

Returns:



113
114
115
# File 'lib/overload/dir.rb', line 113

def self.mkdir? name
  FileUtils.mkdir_p name
end

.require_all(folder, opts = {}) ⇒ Object

Requires all found ruby files in a folder, deep search into child folders ‘Dir.require_all(’./app’)‘



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/overload/dir.rb', line 98

def self.require_all folder, opts = {}
  list = Dir.find(folder, ext: :rb) unless list.is_a?(Array)
  list
    .select{ |o| o.index('.rb') }
    .select{ |o| opts[:skip] ? !o.include?(opts[:skip]) : true }
    .each do |ruby_file|
      begin
        require ruby_file
      rescue Exception => error
        Lux.info "Dir.require_all('#{folder}') # error in #{ruby_file}"
        raise error
      end
    end
end