Class: Perennial::Generator

Inherits:
Object show all
Defined in:
lib/perennial/generator.rb

Defined Under Namespace

Classes: CommandEnv

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination, opts = {}) ⇒ Generator

Returns a new instance of Generator.



32
33
34
35
36
# File 'lib/perennial/generator.rb', line 32

def initialize(destination, opts = {})
  @destination_path = destination
  @template_path    = opts[:template_path] || File.join(Settings.library_root, "templates")
  puts "Starting generator for #{destination}"
end

Instance Attribute Details

#destination_pathObject

Returns the value of attribute destination_path.



30
31
32
# File 'lib/perennial/generator.rb', line 30

def destination_path
  @destination_path
end

#template_pathObject

Returns the value of attribute template_path.



30
31
32
# File 'lib/perennial/generator.rb', line 30

def template_path
  @template_path
end

Instance Method Details

#download(from, to) ⇒ Object



38
39
40
41
# File 'lib/perennial/generator.rb', line 38

def download(from, to)
  describe "Downloading #{from}"
  file to, open(from).read
end

#file(name, contents) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/perennial/generator.rb', line 50

def file(name, contents)
  dest_folder = File.dirname(name)
  folders(dest_folder) unless File.directory?(expand_destination_path(dest_folder))
  describe "Creating file #{name}"
  File.open(expand_destination_path(name), "w+") do |f|
    f.write(contents)
  end
end

#folders(*args) ⇒ Object



43
44
45
46
47
48
# File 'lib/perennial/generator.rb', line 43

def folders(*args)
  args.each do |f|
    describe "Creating folder #{f}"
    FileUtils.mkdir_p(expand_destination_path(f))
  end
end

#template(source, destination, environment = {}) ⇒ Object



59
60
61
62
63
64
# File 'lib/perennial/generator.rb', line 59

def template(source, destination, environment = {})
  describe "Processing template #{source}"
  raw_template = File.read(expand_template_path(source))
  processed_template = ERB.new(raw_template).result(binding_for(environment))
  file destination, processed_template
end