Module: Lash::Files

Defined in:
lib/lash/files.rb

Class Method Summary collapse

Class Method Details

.get_top_level_directories(basedir) ⇒ Array

Gets all the top level directories in the given base_path

Parameters:

  • basedir (String)

    the root directory to search

Returns:

  • (Array)

    an array of expanded paths for all directories found



40
41
42
43
44
45
# File 'lib/lash/files.rb', line 40

def self.get_top_level_directories( basedir )
  Dir.entries( basedir ).collect do |path|
    path = File.join( basedir, path )
    File.basename( path )[0] == ?. || !File.directory?( path ) ? nil : path # not dot directories or files
  end - [nil]
end

.recursive_file_list(basedir, ext) ⇒ Array

Collects an array of all files in basedir with the given ext.

Parameters:

  • basedir (String)

    the root directory to search

  • ext (String)

    extension to filter results by

Returns:

  • (Array)

    an array of expanded paths for each file in basedir and any of it's sub directories



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lash/files.rb', line 11

def self.recursive_file_list( basedir, ext )
  unless ext.is_a? Regexp
    ext = ".#{ext}" if ext && ext[0] != ?.
    ext ||= ""
  end
  
  files = []
  return files unless File.exist? basedir
  Find.find( basedir ) do |path|
    if FileTest.directory?( path )
      if File.basename( path )[0] == ?. # Skip dot directories
        Find.prune
      else
        next
      end
    end
    if ext.is_a? Regexp
      files << path if ext.match( path )
    else
      files << path if File.extname( path ) == ext
    end
  end
  files.sort
end

.relative_to(path, root) ⇒ Object

Gets the relative path from root to path of path is a subdirectory of root, otherwise returns path



48
49
50
51
52
53
# File 'lib/lash/files.rb', line 48

def self.relative_to( path, root )
  path = File.expand_path( path )
  root = File.expand_path( root )
  return path unless path.start_with? root
  path[ root.length + 1, path.length ]
end