-
Notifications
You must be signed in to change notification settings - Fork 49
/
dumper.go
203 lines (180 loc) · 4.51 KB
/
dumper.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package dumper
import (
"bytes"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/tufanbarisyildirim/gonginx/config"
)
var (
//NoIndentStyle default style
NoIndentStyle = &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 0,
Debug: false,
}
//IndentedStyle default style
IndentedStyle = &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 4,
Debug: false,
}
//NoIndentSortedStyle default style
NoIndentSortedStyle = &Style{
SortDirectives: true,
StartIndent: 0,
Indent: 0,
Debug: false,
}
//NoIndentSortedSpaceStyle default style
NoIndentSortedSpaceStyle = &Style{
SortDirectives: true,
SpaceBeforeBlocks: true,
StartIndent: 0,
Indent: 0,
Debug: false,
}
)
// Style dumping style
type Style struct {
SortDirectives bool
SpaceBeforeBlocks bool
StartIndent int
Indent int
Debug bool
}
// NewStyle create new style
func NewStyle() *Style {
style := &Style{
SortDirectives: false,
StartIndent: 0,
Indent: 4,
Debug: false,
}
return style
}
// Iterate interate the indentation for sub blocks
func (s *Style) Iterate() *Style {
newStyle := &Style{
SortDirectives: s.SortDirectives,
SpaceBeforeBlocks: s.SpaceBeforeBlocks,
StartIndent: s.StartIndent + s.Indent,
Indent: s.Indent,
}
return newStyle
}
// DumpDirective convert a directive to a string
func DumpDirective(d config.IDirective, style *Style) string {
if d == nil {
return ""
}
var buf bytes.Buffer
if style.SpaceBeforeBlocks && d.GetBlock() != nil {
buf.WriteString("\n")
}
if len(d.GetComment()) > 0 {
for _, comment := range d.GetComment() {
buf.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat(" ", style.StartIndent), comment))
}
}
buf.WriteString(fmt.Sprintf("%s%s", strings.Repeat(" ", style.StartIndent), d.GetName()))
if len(d.GetParameters()) > 0 {
buf.WriteString(fmt.Sprintf(" %s", strings.Join(d.GetParameters(), " ")))
}
if d.GetBlock() == nil {
if d.GetName() != "" {
buf.WriteRune(';')
}
buf.WriteString(d.GetInlineComment())
} else {
buf.WriteString(" {\n")
buf.WriteString(DumpBlock(d.GetBlock(), style.Iterate()))
buf.WriteString(fmt.Sprintf("\n%s}", strings.Repeat(" ", style.StartIndent)))
}
return buf.String()
}
// DumpBlock convert a directive to a string
func DumpBlock(b config.IBlock, style *Style) string {
var buf bytes.Buffer
if b.GetCodeBlock() != "" {
luaLines := strings.Split(b.GetCodeBlock(), "\n")
for i, line := range luaLines {
buf.WriteString(fmt.Sprintf("%s%s", strings.Repeat(" ", style.StartIndent), line))
if i != len(luaLines)-1 {
buf.WriteString("\n")
}
}
return buf.String()
}
directives := b.GetDirectives()
if style.SortDirectives {
sort.SliceStable(directives, func(i, j int) bool {
return directives[i].GetName() < directives[j].GetName()
})
}
for i, directive := range directives {
if style.Debug {
buf.WriteString("#")
buf.WriteString(directive.GetName())
buf.WriteString(fmt.Sprintf("%t", directive.GetBlock()))
buf.WriteString("\n")
}
buf.WriteString(DumpDirective(directive, style))
if i != len(directives)-1 {
buf.WriteString("\n")
}
}
return buf.String()
}
// DumpConfig dump whole config
func DumpConfig(c *config.Config, style *Style) string {
return DumpBlock(c.Block, style)
}
// DumpInclude dump(stringify) the included AST
func DumpInclude(i *config.Include, style *Style) map[string]string {
mp := make(map[string]string)
for _, cfg := range i.Configs {
mp[cfg.FilePath] = DumpConfig(cfg, style)
}
return mp
}
// WriteConfig writes config
func WriteConfig(c *config.Config, style *Style, writeInclude bool) error {
if writeInclude {
includes := c.FindDirectives("include")
for _, include := range includes {
i, ok := include.(*config.Include)
if !ok {
panic("bug in FindDirective")
}
// no config parsed
if len(i.Configs) == 0 {
continue
}
mp := DumpInclude(i, style)
for path, config := range mp {
// create parent directories, if not exit
dir, _ := filepath.Split(path)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
err = os.WriteFile(path, []byte(config), 0644)
if err != nil {
return err
}
}
}
}
// create parent directories, if not exit
dir, _ := filepath.Split(c.FilePath)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
return os.WriteFile(c.FilePath, []byte(DumpConfig(c, style)), 0644)
}