Module: Tipsy::Utils::System

Extended by:
ActiveSupport::Concern
Included in:
Runners::Compiler, Runners::Generator, SystemTest, Tree
Defined in:
lib/tipsy/utils/system.rb

Instance Method Summary collapse

Instance Method Details

#copy_file(source, destination) ⇒ Object

Copies from one location to another



31
32
33
34
35
# File 'lib/tipsy/utils/system.rb', line 31

def copy_file(source, destination)
  return true if skip_file?(source)
  log_action("copy", destination)
  ::FileUtils.cp(source, destination)
end

#copy_folder(dirname) ⇒ Object

Makes a matching folder in destination from source



40
41
42
43
44
# File 'lib/tipsy/utils/system.rb', line 40

def copy_folder(dirname)
  return true if skip_path?(dirname)
  log_action("copy", dirname)
  ::FileUtils.mkdir_p(dirname)
end

#copy_tree(src, dest) ⇒ Object

Iterate through a file tree and process each file and folder.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tipsy/utils/system.rb', line 79

def copy_tree(src, dest)
  ::Dir.foreach(src) do |file|
    next if excluded?(file)
    source       = ::File.join(src, file)
    destination  = ::File.join(dest, file)

    if ::File.directory?(source)
      copy_folder(destination)
      copy_tree(source, destination)
    else
      copy_file(source, destination)
    end
  end
end

#empty_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/tipsy/utils/system.rb', line 70

def empty_dir?(path)
  ::Dir.entries(path).reject do |ent| 
    ent == "." || ent == ".." || ent == path
  end.empty?
end

#enumerate_tree(path, &block) ⇒ Object

Collect the entire tree of files and folders and yield to a block



108
109
110
111
112
# File 'lib/tipsy/utils/system.rb', line 108

def enumerate_tree(path, &block)
  ::Find.find(path) do |path|
    yield path
  end
end

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

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
# File 'lib/tipsy/utils/system.rb', line 18

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

#excludesObject



10
11
12
# File 'lib/tipsy/utils/system.rb', line 10

def excludes
  @_excludes ||= ['.svn', '.git', '.gitignore', '.sass-cache', 'config.erb', '.rb', '.DS_Store']
end

#excludes=(arr) ⇒ Object



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

def excludes=(arr)
  @_excludes = arr
end

#log_action(name, action) ⇒ Object



114
115
116
# File 'lib/tipsy/utils/system.rb', line 114

def log_action(name, action)
  ::Tipsy.logger.action(name, action.to_s.gsub(Tipsy.root, ""))
end

#make_file(path, content) ⇒ Object



55
56
57
58
# File 'lib/tipsy/utils/system.rb', line 55

def make_file(path, content)
  ::FileUtils.touch(path) unless ::File.exists?(path)
  ::File.open(path, 'w').puts(content)
end

#mkdir_p(path) ⇒ Object

Basic alias



49
50
51
52
53
# File 'lib/tipsy/utils/system.rb', line 49

def mkdir_p(path)
  return true if ::File.exists?(path)
  log_action("create", path)
  ::FileUtils.mkdir_p(path)
end

#normalize_path(pname) ⇒ Object



118
119
120
121
122
# File 'lib/tipsy/utils/system.rb', line 118

def normalize_path(pname)
  pname = ::Pathname.new(pname) if pname.is_a?(String)
  return pname if pname.absolute?
  ::Pathname.new(::File.join(Tipsy.root, pname.to_s))
end

#rm_rf(path) ⇒ Object



60
61
62
63
# File 'lib/tipsy/utils/system.rb', line 60

def rm_rf(path)
  log_action('delete', path)
  ::FileUtils.rm_rf(path)
end

#skip_file?(filename) ⇒ Boolean

Override in specific runner as necessary. Allows more fine-grained control per file.

Returns:

  • (Boolean)


102
103
104
# File 'lib/tipsy/utils/system.rb', line 102

def skip_file?(filename)
  false
end

#skip_path?(dirname) ⇒ Boolean

Override in specific runner as necessary Allows more fine-grained control per directory.

Returns:

  • (Boolean)


96
97
98
# File 'lib/tipsy/utils/system.rb', line 96

def skip_path?(dirname)
  false
end


65
66
67
68
# File 'lib/tipsy/utils/system.rb', line 65

def unlink(file)
  log_action('delete', file)
  ::File.unlink(file)
end