-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsections.go
158 lines (140 loc) · 4.29 KB
/
sections.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
package file
import (
"cmp"
"slices"
"strings"
"unicode/utf8"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/lib/filesystem"
)
const (
InjectPrefix = prefix + "INJECT"
ModulePrefix = prefix + "HAS_MODULE"
sectionPrefix = prefix + "SECTION"
startPattern = "_START("
endPattern = "_END("
closePattern = ")$"
IgnorePattern = prefix + "IGNORE" + suffix
GenerateOncePattern = prefix + "GENERATE_ONCE" + suffix
)
func Inject(fl *File, content map[string]string) error {
injectIndexes, err := sectionIndexes(fl.Content, InjectPrefix)
if err != nil {
return errors.Wrap(err, "can't read inject indexes")
}
// if len(injectIndexes) < len(content) {
// return errors.Errorf("file contains [%d] injection points, but [%d] are required", len(injectIndexes), len(content))
// }
for _, idx := range injectIndexes {
c, ok := content[idx.Key]
if !ok {
return errors.Errorf("no inject index for key [%s]", idx.Key)
}
fl.Content = fl.Content[0:idx.Start] + c + fl.Content[idx.End:]
}
return nil
}
func ReplaceSections(fl *File, tgt filesystem.FileLoader) error {
f := fl.FullPath()
if strings.Contains(fl.Content, sectionPrefix) && tgt.Exists(f) {
tgtBytes, _ := tgt.ReadFile(f)
if utf8.Valid(tgtBytes) {
newContent, err := copySections(string(tgtBytes), fl.Content)
if err != nil {
return errors.Wrapf(err, "error reading sections from [%s]", f)
}
fl.Content = newContent
}
}
return nil
}
func copySections(src string, tgt string) (string, error) {
srcSections, err := sectionIndexes(src, sectionPrefix)
if err != nil {
return "", errors.Wrap(err, "unable to read sections from source content")
}
tgtSections, err := sectionIndexes(tgt, sectionPrefix)
if err != nil {
return "", errors.Wrap(err, "unable to read sections from target content")
}
ret := tgt
for _, sec := range srcSections {
tgtSec := tgtSections.Get(sec.Key)
if tgtSec == nil {
return "", errors.Errorf("no section [%s] found in target", sec.Key)
}
if sec.End == 0 {
return "", errors.Errorf("no section end for [%s] found in target", sec.Key)
}
content := src[sec.Start:sec.End]
ret = ret[0:tgtSec.Start] + content + ret[tgtSec.End:]
}
return ret, nil
}
type section struct {
Key string
Start int
End int
}
type sections []*section
func (s sections) Get(key string) *section {
return lo.FindOrElse(s, nil, func(x *section) bool {
return x.Key == key
})
}
func (s sections) Sort() {
slices.SortFunc(s, func(l *section, r *section) int {
return -cmp.Compare(l.Start, r.Start)
})
}
func sectionIndexes(s string, prefix string) (sections, error) {
var ret sections
for idx, c := range s {
if c == '$' && len(s) > idx+len(prefix) {
candidate := s[idx : idx+len(prefix)]
if candidate == prefix {
nextDollar := strings.Index(s[idx+len(prefix):], closePattern)
if nextDollar == -1 {
return nil, errors.New("found section, but no closing delimiter")
}
if nextDollar > 256 {
return nil, errors.Errorf("found section, but no closing delimiter within [%d] bytes", nextDollar)
}
endIdx := idx + len(prefix) + nextDollar + len(closePattern)
text := s[idx+len(prefix) : endIdx]
var err error
ret, err = parseText(ret, endIdx, text, prefix)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse text [%s]", text)
}
}
}
}
ret.Sort()
return ret, nil
}
func parseText(ret sections, idx int, text string, prefix string) (sections, error) {
switch {
case strings.HasPrefix(text, startPattern):
currSection := text[len(startPattern) : len(text)-len(closePattern)]
if ret.Get(currSection) != nil {
return nil, errors.Errorf("multiple sections found with key [%s]", currSection)
}
ret = append(ret, §ion{Key: currSection, Start: idx})
return ret, nil
case strings.HasPrefix(text, endPattern):
if len(ret) == 0 {
return nil, errors.New("encountered end section pattern before start")
}
curr := ret[len(ret)-1]
sec := text[len(endPattern) : len(text)-len(closePattern)]
if curr.Key != sec {
return nil, errors.Errorf("encountered nested section patterns (%s within %s)", sec, curr.Key)
}
curr.End = idx - len(prefix) - len(text)
return ret, nil
default:
return nil, errors.Errorf("invalid section pattern [%s]", text)
}
}