forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.go
146 lines (127 loc) · 3.57 KB
/
compile.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package compiler
import (
"github.com/drone/drone/engine/runner"
"github.com/drone/drone/engine/runner/parse"
yaml "github.com/drone/drone/engine/compiler/parse"
)
// Compiler compiles the Yaml file to the intermediate representation.
type Compiler struct {
trans []Transform
}
func New() *Compiler {
return &Compiler{}
}
// Transforms sets the compiler transforms use to transform the intermediate
// representation during compilation.
func (c *Compiler) Transforms(trans []Transform) *Compiler {
c.trans = append(c.trans, trans...)
return c
}
// CompileString compiles the Yaml configuration string and returns
// the intermediate representation for the interpreter.
func (c *Compiler) CompileString(in string) (*runner.Spec, error) {
return c.Compile([]byte(in))
}
// CompileString compiles the Yaml configuration file and returns
// the intermediate representation for the interpreter.
func (c *Compiler) Compile(in []byte) (*runner.Spec, error) {
root, err := yaml.Parse(in)
if err != nil {
return nil, err
}
if err := root.Walk(c.walk); err != nil {
return nil, err
}
config := &runner.Spec{}
tree := parse.NewTree()
// pod section
if root.Pod != nil {
node, ok := root.Pod.(*yaml.ContainerNode)
if ok {
config.Containers = append(config.Containers, &node.Container)
tree.Append(parse.NewRunNode().SetName(node.Container.Name).SetDetach(true))
}
}
// cache section
if root.Cache != nil {
node, ok := root.Cache.(*yaml.ContainerNode)
if ok && !node.Disabled {
config.Containers = append(config.Containers, &node.Container)
tree.Append(parse.NewRunNode().SetName(node.Container.Name))
}
}
// clone section
if root.Clone != nil {
node, ok := root.Clone.(*yaml.ContainerNode)
if ok && !node.Disabled {
config.Containers = append(config.Containers, &node.Container)
tree.Append(parse.NewRunNode().SetName(node.Container.Name))
}
}
// services section
for _, container := range root.Services {
node, ok := container.(*yaml.ContainerNode)
if !ok || node.Disabled {
continue
}
config.Containers = append(config.Containers, &node.Container)
tree.Append(parse.NewRunNode().SetName(node.Container.Name).SetDetach(true))
}
// pipeline section
for i, container := range root.Script {
node, ok := container.(*yaml.ContainerNode)
if !ok || node.Disabled {
continue
}
config.Containers = append(config.Containers, &node.Container)
// step 1: lookahead to see if any status=failure exist
list := parse.NewListNode()
for ii, next := range root.Script {
if i >= ii {
continue
}
node, ok := next.(*yaml.ContainerNode)
if !ok || node.Disabled || !node.OnFailure() {
continue
}
list.Append(
parse.NewRecoverNode().SetBody(
parse.NewRunNode().SetName(
node.Container.Name,
),
),
)
}
// step 2: if yes, collect these and append to "error" node
if len(list.Body) == 0 {
tree.Append(parse.NewRunNode().SetName(node.Container.Name))
} else {
errorNode := parse.NewErrorNode()
errorNode.SetBody(parse.NewRunNode().SetName(node.Container.Name))
errorNode.SetDefer(list)
tree.Append(errorNode)
}
}
config.Nodes = tree
return config, nil
}
func (c *Compiler) walk(node yaml.Node) (err error) {
for _, trans := range c.trans {
switch v := node.(type) {
case *yaml.BuildNode:
err = trans.VisitBuild(v)
case *yaml.ContainerNode:
err = trans.VisitContainer(v)
case *yaml.NetworkNode:
err = trans.VisitNetwork(v)
case *yaml.VolumeNode:
err = trans.VisitVolume(v)
case *yaml.RootNode:
err = trans.VisitRoot(v)
}
if err != nil {
break
}
}
return err
}