Class: Giter8::FS

Inherits:
Object
  • Object
show all
Defined in:
lib/giter8/fs/fs.rb

Overview

FS implements filesystem-related methods for handling template directories

Class Method Summary collapse

Class Method Details

.binary?(path) ⇒ Boolean

Returns whether a given path contains a binary file.

Returns:

  • (Boolean)


17
18
19
# File 'lib/giter8/fs/fs.rb', line 17

def self.binary?(path)
  File.binary?(path)
end

.enumerate(path) ⇒ Object

Recursively enumerate paths inside a given path and returns a list of files, except “default.properties”.



23
24
25
26
27
28
29
30
# File 'lib/giter8/fs/fs.rb', line 23

def self.enumerate(path)
  original_path = path
  path = "#{path}/**/*" unless path.match?(%r{/\*})
  Dir.glob(path)
     .select { |e| File.stat(e).file? }
     .reject { |e| File.basename(e) == "default.properties" }
     .collect { |e| e[original_path.length + 1..] }
end

.handle_file_copy(from, to) ⇒ Object



32
33
34
# File 'lib/giter8/fs/fs.rb', line 32

def self.handle_file_copy(from, to)
  FileUtils.cp(from, to)
end

.handle_file_render(props, source, destination) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/giter8/fs/fs.rb', line 36

def self.handle_file_render(props, source, destination)
  f = File.open(source)
  rendered = Giter8.render_template(f, props)
  f.close

  File.write(destination, rendered, 0, mode: "w")
end

.render(props, input, output) ⇒ Object

Renders the contents of a given input directory into a destination, creating directories as required, using provided opts to render templates.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/giter8/fs/fs.rb', line 55

def self.render(props, input, output)
  FileUtils.mkdir_p output
  props = Giter8.parse_props(props)

  verbatim = []
  verbatim = props.fetch(:verbatim).split if props.key? :verbatim

  enumerate(input).each do |file|
    source = File.absolute_path(File.join(input, file))
    output_file_name = render_file_name(file, props)
    destination = File.absolute_path(File.join(output, output_file_name))
    FileUtils.mkdir_p File.dirname(destination)
    if verbatim?(source, verbatim)
      handle_file_copy(source, destination)
    else
      handle_file_render(props, source, destination)
    end
  end
end

.render_file_name(name, props) ⇒ Object

Optimistically attempt to render a file name. Returns the name verbatim in case parsing or rendering fails.



46
47
48
49
50
51
# File 'lib/giter8/fs/fs.rb', line 46

def self.render_file_name(name, props)
  ast = Giter8.parse_template(name)
  Giter8.render_template(ast, props)
rescue Giter8::Error
  name
end

.verbatim?(path, ignore_patterns) ⇒ Boolean

Returns whether the provided path must be copied verbatim by the renderer instead of being handled by parsers.

Returns:

  • (Boolean)


8
9
10
11
12
13
14
# File 'lib/giter8/fs/fs.rb', line 8

def self.verbatim?(path, ignore_patterns)
  return false if ignore_patterns.empty?

  ignore_patterns.any? do |pattern|
    File.fnmatch? pattern, path
  end
end