Class: Tipsy::Utils::Tree

Inherits:
Object
  • Object
show all
Includes:
System
Defined in:
lib/tipsy/utils/tree.rb

Overview

Class for processing file trees. Copies a source tree to a destination tree excluding specific files and directories.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from System

#copy_file, #copy_folder, #copy_tree, #empty_dir?, #enumerate_tree, #log_action, #make_file, #mkdir_p, #normalize_path, #rm_rf, #skip_file?, #skip_path?, #unlink

Constructor Details

#initialize(src, dest, excl = nil) ⇒ Tree

Returns a new instance of Tree.



16
17
18
19
20
# File 'lib/tipsy/utils/tree.rb', line 16

def initialize(src, dest, excl = nil)
  @source_path = normalize_path(::Pathname.new(src))
  @dest_path   = normalize_path(::Pathname.new(dest))
  @excludes = (excl || ['.svn', '.git', '.gitignore', '.sass-cache', 'config.erb', '.rb', '.DS_Store'])
end

Instance Attribute Details

#dest_pathObject (readonly)

Returns the value of attribute dest_path.



13
14
15
# File 'lib/tipsy/utils/tree.rb', line 13

def dest_path
  @dest_path
end

#excludesObject

Returns the value of attribute excludes.



14
15
16
# File 'lib/tipsy/utils/tree.rb', line 14

def excludes
  @excludes
end

#filesObject (readonly)

Returns the value of attribute files.



13
14
15
# File 'lib/tipsy/utils/tree.rb', line 13

def files
  @files
end

#foldersObject (readonly)

Returns the value of attribute folders.



13
14
15
# File 'lib/tipsy/utils/tree.rb', line 13

def folders
  @folders
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



13
14
15
# File 'lib/tipsy/utils/tree.rb', line 13

def source_path
  @source_path
end

Instance Method Details

#collect!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tipsy/utils/tree.rb', line 22

def collect!
  @folders, @files = [], []
  ::Find.find(source_path.to_s).each do |path|
    next if path == source_path.to_s
    if excluded?(path) || excluded_path?(path)
      ::Find.prune if ::File.directory?(path)
      next
    end
    if ::File.directory?(path)
      @folders << normalize_path(::Pathname.new(path))
    else
      @files << normalize_path(::Pathname.new(path))
    end
  end
end

#copy!Object



38
39
40
41
42
43
44
45
46
# File 'lib/tipsy/utils/tree.rb', line 38

def copy!
  collect! if folders.nil? || files.nil?
  folders.each do |folder|
    mkdir_p(folder.to_s.gsub(source_path.to_s, dest_path.to_s))
  end
  files.each do |file|
    copy_file(file.to_s, file.to_s.gsub(source_path.to_s, dest_path.to_s))
  end
end

#excluded?(file, against = nil) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
# File 'lib/tipsy/utils/tree.rb', line 54

def excluded?(file, against = nil)
  against ||= excludes
  return true if file.to_s == '.' || file.to_s == '..' || against.include?(file)
  check = ::Pathname.new(file)
  return true if against.include?(check.basename.to_s)
  !against.detect do |exc| 
    (check.basename == exc || check.extname == exc)
  end.nil?
end

#excluded_path?(src) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/tipsy/utils/tree.rb', line 48

def excluded_path?(src)
  path = ::Pathname.new(src)
  rel  = path.relative_path_from(source_path).to_s
  excludes.detect{ |route| route == src || route == rel.to_s }
end