Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame^] | 1 | package blueprint |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | ) |
| 7 | |
| 8 | type Module interface { |
| 9 | GenerateBuildActions(ModuleContext) |
| 10 | } |
| 11 | |
| 12 | type 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 | |
| 28 | var _ ModuleContext = (*moduleContext)(nil) |
| 29 | |
| 30 | type 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 | |
| 42 | func (m *moduleContext) ModuleName() string { |
| 43 | return m.info.properties.Name |
| 44 | } |
| 45 | |
| 46 | func (m *moduleContext) ModuleDir() string { |
| 47 | return filepath.Dir(m.info.relBlueprintFile) |
| 48 | } |
| 49 | |
| 50 | func (m *moduleContext) Config() Config { |
| 51 | return m.config |
| 52 | } |
| 53 | |
| 54 | func (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 | |
| 61 | func (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 | |
| 75 | func (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 | |
| 84 | func (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, ¶ms) |
| 88 | if err != nil { |
| 89 | panic(err) |
| 90 | } |
| 91 | |
| 92 | m.actionDefs.rules = append(m.actionDefs.rules, r) |
| 93 | |
| 94 | return r |
| 95 | } |
| 96 | |
| 97 | func (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, ¶ms) |
| 101 | if err != nil { |
| 102 | panic(err) |
| 103 | } |
| 104 | |
| 105 | m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def) |
| 106 | } |
| 107 | |
| 108 | func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) { |
| 109 | m.context.visitDepsDepthFirst(m.module, visit) |
| 110 | } |
| 111 | |
| 112 | func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool, |
| 113 | visit func(Module)) { |
| 114 | |
| 115 | m.context.visitDepsDepthFirstIf(m.module, pred, visit) |
| 116 | } |