Class: Shipyard::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/shipyard/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest_file, table) ⇒ Builder

Returns a new instance of Builder.



10
11
12
13
# File 'lib/shipyard/builder.rb', line 10

def initialize(manifest_file, table)
  @manifest = Manifest.new(manifest_file, table)
  @context = Context.new(@manifest)
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



8
9
10
# File 'lib/shipyard/builder.rb', line 8

def db
  @db
end

#tableObject (readonly)

Returns the value of attribute table.



7
8
9
# File 'lib/shipyard/builder.rb', line 7

def table
  @table
end

Instance Method Details

#generateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shipyard/builder.rb', line 15

def generate
  templates = Dir['*.erb']

  templates.each do |template|
    abort "No destination mapped for template '#{template}'" unless !!@manifest.destination_for(template)
  end

  puts "Rendering to output directory #{@manifest.output_dir}"
  templates.each do |filename|
    open(filename) do |file|
      # pass in the context object and render the template
      template = ERB.new(file.read)
      template.filename = filename
      code = template.result(@context.get_binding)
      
      # write the rendered file to the associated destination
      dst = @manifest.destination_for(filename)
      output_file = File.join(@manifest.output_dir, dst)
      File.makedirs(File.dirname(output_file))
      open(output_file, 'w') do |f|
        puts "\t#{filename} => #{output_file}"
        f.write(code)
      end
    end
  end
end