summarylogtreecommitdiffstats
path: root/compile-templates
blob: 917738249b9d5a4df662040cd2a4aa8f8430dda5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
require 'optparse'
require 'erb'

options = {}
OptionParser.new do |parser|
    parser.banner = "Usage: compile-templates.rb [options] <template-files...>"

    parser.on("--pkgname=PKGNAME", "Name of package") do |v|
        options[:pkgname] = v
    end

    parser.on("--pkgdesc=PKGDESC", "Description of package") do |v|
        options[:pkgdesc] = v
    end
end.parse!

raise 'pkgname not provided' unless options.has_key?(:pkgname)
raise 'pkgdesc not provided' unless options.has_key?(:pkgdesc)

templates = ARGV

raise 'templates not provided' unless templates.length > 0

class TemplateVariables
    attr_reader :pkgname, :pkgdesc

    def initialize(pkgname, pkgdesc)
        @pkgname = pkgname
        @pkgdesc = pkgdesc
    end

    def get_binding
        binding()
    end
end

templateVariables = TemplateVariables.new(options[:pkgname], options[:pkgdesc])

templates.each do |templateFilename|
    template = File.read(templateFilename)
    renderer = ERB.new(template)
    rendered = renderer.result(templateVariables.get_binding)

    templateFilenameWithoutSuffix = templateFilename.delete_suffix('.erb')
    outputFilename = templateFilenameWithoutSuffix.sub 'PKGNAME', templateVariables.pkgname

    output = File.new(outputFilename, 'w')
    output.puts(rendered)
    output.close

    puts "compiled template \"#{templateFilename}\" to \"#{outputFilename}\""
end