blob: f8fe564cb7cec5f64e182f04675a67620be54bb8 [file] [log] [blame]
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001package blueprint
2
3import (
4 "fmt"
5 "path/filepath"
6)
7
8type Module interface {
9 GenerateBuildActions(ModuleContext)
10}
11
12type ModuleContext interface {
13 ModuleName() string
14 ModuleDir() string
15 Config() Config
16
17 ModuleErrorf(fmt string, args ...interface{})
18 PropertyErrorf(property, fmt string, args ...interface{})
19
20 Variable(name, value string)
21 Rule(name string, params RuleParams) Rule
22 Build(params BuildParams)
23
24 VisitDepsDepthFirst(visit func(Module))
25 VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
26}
27
28var _ ModuleContext = (*moduleContext)(nil)
29
30type moduleContext struct {
31 context *Context
32 config Config
33 module Module
34 scope *localScope
35 info *moduleInfo
36
37 errs []error
38
39 actionDefs localBuildActions
40}
41
42func (m *moduleContext) ModuleName() string {
43 return m.info.properties.Name
44}
45
46func (m *moduleContext) ModuleDir() string {
47 return filepath.Dir(m.info.relBlueprintFile)
48}
49
50func (m *moduleContext) Config() Config {
51 return m.config
52}
53
54func (m *moduleContext) ModuleErrorf(format string, args ...interface{}) {
55 m.errs = append(m.errs, &Error{
56 Err: fmt.Errorf(format, args...),
57 Pos: m.info.pos,
58 })
59}
60
61func (m *moduleContext) PropertyErrorf(property, format string,
62 args ...interface{}) {
63
64 pos, ok := m.info.propertyPos[property]
65 if !ok {
66 panic(fmt.Errorf("property %q was not set for this module", property))
67 }
68
69 m.errs = append(m.errs, &Error{
70 Err: fmt.Errorf(format, args...),
71 Pos: pos,
72 })
73}
74
75func (m *moduleContext) Variable(name, value string) {
76 v, err := m.scope.AddLocalVariable(name, value)
77 if err != nil {
78 panic(err)
79 }
80
81 m.actionDefs.variables = append(m.actionDefs.variables, v)
82}
83
84func (m *moduleContext) Rule(name string, params RuleParams) Rule {
85 // TODO: Verify that params.Pool is accessible in this module's scope.
86
87 r, err := m.scope.AddLocalRule(name, &params)
88 if err != nil {
89 panic(err)
90 }
91
92 m.actionDefs.rules = append(m.actionDefs.rules, r)
93
94 return r
95}
96
97func (m *moduleContext) Build(params BuildParams) {
98 // TODO: Verify that params.Rule is accessible in this module's scope.
99
100 def, err := parseBuildParams(m.scope, &params)
101 if err != nil {
102 panic(err)
103 }
104
105 m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def)
106}
107
108func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) {
109 m.context.visitDepsDepthFirst(m.module, visit)
110}
111
112func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool,
113 visit func(Module)) {
114
115 m.context.visitDepsDepthFirstIf(m.module, pred, visit)
116}