-
Notifications
You must be signed in to change notification settings - Fork 787
/
jenkinsfile_writer.go
128 lines (114 loc) · 2.94 KB
/
jenkinsfile_writer.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
package util
import (
"bytes"
"strings"
)
var (
contextFunctions = map[string]bool{
"container": true,
"dir": true,
}
)
// Statement represents a statement in a Jenkinsfile
type Statement struct {
Function string
Arguments []string
Statement string
Children []*Statement
}
// Text returns the text line of the current function or statement
func (s *Statement) Text() string {
if s.Function != "" {
text := s.Function
expressions := []string{}
for _, arg := range s.Arguments {
expressions = append(expressions, asArgumentExpression(arg))
}
return text + "(" + strings.Join(expressions, ", ") + ")"
}
return s.Statement
}
// ContextEquals returns true if this statement is a context statement and it equals
// the same context as that statement
func (s *Statement) ContextEquals(that *Statement) bool {
if s.Function == that.Function && contextFunctions[s.Function] {
return StringArraysEqual(s.Arguments, that.Arguments)
}
return false
}
func asArgumentExpression(arg string) string {
return "'" + arg + "'"
}
// Writer implements the struct for Jenkinsfilewriter
type Writer struct {
InitialIndent string
IndentText string
Buffer bytes.Buffer
IndentCount int
}
// WriteJenkinsfileStatements writes the given Jenkinsfile statements as a string
func WriteJenkinsfileStatements(indentCount int, statements []*Statement) string {
writer := NewWriter(indentCount)
writer.Write(statements)
return writer.String()
}
// NewWriter creates a Jenkinsfile writer
func NewWriter(indentCount int) *Writer {
return &Writer{
IndentText: " ",
IndentCount: indentCount,
}
}
func (w *Writer) Write(inputStatements []*Statement) {
statements := w.combineSimilarContexts(inputStatements)
w.writeStatement(nil, statements)
}
func (w *Writer) writeStatement(parent *Statement, statements []*Statement) {
for _, s := range statements {
text := s.Text()
hasChildren := len(s.Children) > 0
if hasChildren {
text = text + " {"
}
w.println(text)
if hasChildren {
w.IndentCount++
w.writeStatement(s, s.Children)
w.IndentCount--
}
if hasChildren {
w.println("}")
}
}
}
func (w *Writer) println(text string) {
if text != "" {
for i := 0; i < w.IndentCount; i++ {
w.Buffer.WriteString(w.IndentText)
}
w.Buffer.WriteString(text)
}
w.Buffer.WriteString("\n")
}
// String returns the string value of this writer
func (w *Writer) String() string {
return w.Buffer.String()
}
func (w *Writer) combineSimilarContexts(statements []*Statement) []*Statement {
answer := append([]*Statement{}, statements...)
for i := 0; i < len(answer)-1; {
s1 := answer[i]
s2 := answer[i+1]
// lets combine the children to the first node if the contexts are equal
if s1.ContextEquals(s2) {
s1.Children = append(s1.Children, s2.Children...)
answer = append(answer[0:i+1], answer[i+2:]...)
} else {
i++
}
}
for _, s := range answer {
s.Children = w.combineSimilarContexts(s.Children)
}
return answer
}