Class: Edployify::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/edployify/template.rb

Constant Summary collapse

TEMPLATES_DIR =
File.expand_path('../../../templates', __FILE__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, templates_base, project_base) ⇒ Template

Returns a new instance of Template.



20
21
22
23
# File 'lib/edployify/template.rb', line 20

def initialize(path, templates_base, project_base)
	@template = File.join(templates_base, path)
	@target = File.join(project_base, path.sub(/\.erb$/, ''))
end

Class Method Details

.dirsObject



47
48
49
# File 'lib/edployify/template.rb', line 47

def dirs
	@@dirs ||= init(:dirs)
end

.generate_templates(local_binding) ⇒ Object



57
58
59
60
61
# File 'lib/edployify/template.rb', line 57

def generate_templates(local_binding)
	templates.each do |template|
		template.generate(local_binding)
	end
end

.make_dirsObject



51
52
53
54
55
# File 'lib/edployify/template.rb', line 51

def make_dirs
	dirs.each do |dir|
		dir.mkdir
	end
end

.templatesObject



43
44
45
# File 'lib/edployify/template.rb', line 43

def templates
	@@templates ||= init(:templates)
end

Instance Method Details

#generate(local_binding) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/edployify/template.rb', line 25

def generate(local_binding)
    content = ::ERB.new(IO.read(@template)).result(local_binding)
  if File.exists?(@target)
    warn "[skip] '#{@target}' already exists"
  elsif File.exists?(@target.downcase)
    warn "[skip] '#{@target.downcase}' exists, which could conflict with `#{@target}'"
  else
    unless File.exists?(File.dirname(@target))
      puts "[add] making directory '#{File.dirname(@target)}'"
      FileUtils.mkdir(File.dirname(@target))
    end
    puts "[add] writing '#{@target}'"
    File.open(@target, "w") { |f| f.write(content) }
  end
end