-
Notifications
You must be signed in to change notification settings - Fork 6
/
bidder_generator.go
80 lines (68 loc) · 2.28 KB
/
bidder_generator.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// The following directive is necessary to make the package coherent:
// build ignore
// This program generates matchers It can be invoked by running
// go generate
package main
import (
"io/ioutil"
"log"
"text/template"
"github.com/jessevdk/go-flags"
"github.com/vanilla-rtb/extensions/codegen"
)
func die(err error) {
if err != nil {
log.Fatal(err)
}
}
type Options struct {
InputTemplate flags.Filename `short:"i" long:"input-template" description:"InputTemplate file" default:"-"`
OutputDir flags.Filename `short:"o" long:"output-dir" description:"OutputDir file" default:"-"`
GeneratorType func(string) `short:"g" long:"generator-type" description:"GeneratorType" default:"matchers"`
Executable string `short:"e" long:"executable" description:"Executable name" default:"bidder"`
TargetingName string `short:"T" long:"targeting-name" description:"Directory for header files" default:"-"`
BuildType string `short:"B" long:"build_type" description:"Directory for header files" default:"APP"`
}
var options Options
var parser = flags.NewParser(&options, flags.Default)
type GenerateFunc func(*template.Template) error
var generatorTypes = map[string]GenerateFunc{
"cmake": func(t *template.Template) error {
return (&codegen.CmakeGenerator{
Template: t,
Executable: options.Executable,
OutpuDir: string(options.OutputDir),
}).Execute()
},
"app": func(t *template.Template) error {
return (&codegen.AppGenerator{
OutputDir: string(options.OutputDir),
AppName: options.Executable,
TargetingModel: options.TargetingName,
BuildType: options.BuildType,
Template: t,
}).Execute()
},
"matchers": func(t *template.Template) error {
return (&codegen.CacheGenerator{
OutputDir: string(options.OutputDir),
Template: t,
}).Execute()
},
}
func main() {
var generateFunc GenerateFunc
options.GeneratorType = func(gen string) {
var ok bool
if generateFunc, ok = generatorTypes[gen]; !ok {
log.Fatalf("Unsupported generator-type %s\n", gen)
}
}
_, err := parser.Parse()
die(err)
templateContent, err := ioutil.ReadFile(string(options.InputTemplate))
die(err)
var tmpl = template.Must(template.New("").Funcs(codegen.FuncMap).Parse(string(templateContent)))
err = generateFunc(tmpl)
die(err)
}