This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
package_impl.go
91 lines (80 loc) · 1.99 KB
/
package_impl.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
81
82
83
84
85
86
87
88
89
90
91
package urknall
import (
"fmt"
"strings"
"github.com/dynport/urknall/cmd"
"github.com/dynport/urknall/utils"
)
type packageImpl struct {
tasks []*task
taskNames map[string]struct{}
reference interface{} // used for rendering
cacheKeyPrefix string
}
func (pkg *packageImpl) AddCommands(name string, cmds ...cmd.Command) {
if pkg.cacheKeyPrefix != "" {
name = pkg.cacheKeyPrefix + "." + name
}
name = utils.MustRenderTemplate(name, pkg.reference)
t := &task{name: name}
for _, c := range cmds {
if r, ok := c.(cmd.Renderer); ok {
r.Render(pkg.reference)
}
t.Add(c)
}
pkg.addTask(t)
}
func (pkg *packageImpl) AddTemplate(name string, tpl Template) {
if pkg.cacheKeyPrefix != "" {
name = pkg.cacheKeyPrefix + "." + name
}
name = utils.MustRenderTemplate(name, pkg.reference)
e := validateTemplate(tpl)
if e != nil {
panic(e)
}
if pkg.reference != nil {
name = utils.MustRenderTemplate(name, pkg.reference)
}
pkg.validateTaskName(name)
child := &packageImpl{cacheKeyPrefix: name, reference: tpl}
tpl.Render(child)
for _, task := range child.tasks {
pkg.addTask(task)
}
}
func (pkg *packageImpl) AddTask(name string, tsk Task) {
if pkg.cacheKeyPrefix != "" {
name = pkg.cacheKeyPrefix + "." + name
}
name = utils.MustRenderTemplate(name, pkg.reference)
t := &task{name: name}
cmds, e := tsk.Commands()
if e != nil {
panic(e)
}
for _, c := range cmds {
t.Add(c)
}
pkg.addTask(t)
}
func (pkg *packageImpl) addTask(task *task) {
pkg.validateTaskName(task.name)
pkg.taskNames[task.name] = struct{}{}
pkg.tasks = append(pkg.tasks, task)
}
func (pkg *packageImpl) validateTaskName(name string) {
if name == "" {
panic("package names must not be empty!")
}
if strings.Contains(name, " ") {
panic(fmt.Sprintf(`package names must not contain spaces (%q does)`, name))
}
if pkg.taskNames == nil {
pkg.taskNames = map[string]struct{}{}
}
if _, ok := pkg.taskNames[name]; ok {
panic(fmt.Sprintf("package with name %q exists already", name))
}
}