5
5
"github.com/samber/lo"
6
6
log "github.com/sirupsen/logrus"
7
7
"github.com/spf13/cast"
8
- "github.com/wttech/aemc/pkg/base "
8
+ "github.com/wttech/aemc/pkg/cfg "
9
9
"github.com/wttech/aemc/pkg/common/pathx"
10
10
"github.com/wttech/aemc/pkg/common/stringsx"
11
11
"io/fs"
@@ -39,22 +39,18 @@ func init() {
39
39
fileWithNamespacePatternRegex = regexp .MustCompile (FileWithNamespacePattern )
40
40
}
41
41
42
- type Manager struct {
43
- baseOpts * base.Opts
44
-
42
+ type Editor struct {
45
43
FilesDeleted []PathRule
46
44
FilesFlattened []string
47
45
PropertiesSkipped []PathRule
48
46
MixinTypesSkipped []PathRule
49
47
NamespacesSkipped bool
50
48
}
51
49
52
- func NewManager (baseOpts * base.Opts ) * Manager {
53
- cv := baseOpts .Config ().Values ()
54
-
55
- return & Manager {
56
- baseOpts : baseOpts ,
50
+ func NewEditor (config * cfg.Config ) * Editor {
51
+ cv := config .Values ()
57
52
53
+ return & Editor {
58
54
FilesDeleted : determinePathRules (cv .Get ("content.clean.files_deleted" )),
59
55
FilesFlattened : cv .GetStringSlice ("content.clean.files_flattened" ),
60
56
PropertiesSkipped : determinePathRules (cv .Get ("content.clean.properties_skipped" )),
@@ -63,7 +59,7 @@ func NewManager(baseOpts *base.Opts) *Manager {
63
59
}
64
60
}
65
61
66
- func (c Manager ) Clean (path string ) error {
62
+ func (c Editor ) Clean (path string ) error {
67
63
if pathx .IsDir (path ) {
68
64
log .Infof ("cleaning directory '%s'" , path )
69
65
if err := c .cleanDotContents (path ); err != nil {
@@ -107,13 +103,13 @@ func eachFiles(root string, processFileFunc func(string) error) error {
107
103
})
108
104
}
109
105
110
- func (c Manager ) cleanDotContents (root string ) error {
106
+ func (c Editor ) cleanDotContents (root string ) error {
111
107
return eachFiles (root , func (path string ) error {
112
108
return c .cleanDotContentFile (path )
113
109
})
114
110
}
115
111
116
- func (c Manager ) cleanDotContentFile (path string ) error {
112
+ func (c Editor ) cleanDotContentFile (path string ) error {
117
113
if ! strings .HasSuffix (path , XmlFileSuffix ) {
118
114
return nil
119
115
}
@@ -127,7 +123,7 @@ func (c Manager) cleanDotContentFile(path string) error {
127
123
return writeLines (path , outputLines )
128
124
}
129
125
130
- func (c Manager ) filterLines (path string , lines []string ) []string {
126
+ func (c Editor ) filterLines (path string , lines []string ) []string {
131
127
var result []string
132
128
for _ , line := range lines {
133
129
flag , processedLine := c .lineProcess (path , line )
@@ -148,7 +144,7 @@ func (c Manager) filterLines(path string, lines []string) []string {
148
144
return c .cleanNamespaces (path , result )
149
145
}
150
146
151
- func (c Manager ) cleanNamespaces (path string , lines []string ) []string {
147
+ func (c Editor ) cleanNamespaces (path string , lines []string ) []string {
152
148
if ! c .NamespacesSkipped {
153
149
return lines
154
150
}
@@ -183,7 +179,7 @@ func (c Manager) cleanNamespaces(path string, lines []string) []string {
183
179
return result
184
180
}
185
181
186
- func (c Manager ) lineProcess (path string , line string ) (bool , string ) {
182
+ func (c Editor ) lineProcess (path string , line string ) (bool , string ) {
187
183
groups := propPatternRegex .FindStringSubmatch (line )
188
184
if strings .TrimSpace (line ) == "" {
189
185
return true , ""
@@ -197,7 +193,7 @@ func (c Manager) lineProcess(path string, line string) (bool, string) {
197
193
return false , line
198
194
}
199
195
200
- func (c Manager ) normalizeMixins (path string , line string , propValue string , lineSuffix string ) (bool , string ) {
196
+ func (c Editor ) normalizeMixins (path string , line string , propValue string , lineSuffix string ) (bool , string ) {
201
197
normalizedValue := strings .Trim (propValue , "[]" )
202
198
var resultValues []string
203
199
for _ , value := range strings .Split (normalizedValue , "," ) {
@@ -211,13 +207,13 @@ func (c Manager) normalizeMixins(path string, line string, propValue string, lin
211
207
return false , strings .ReplaceAll (line , normalizedValue , strings .Join (resultValues , "," ))
212
208
}
213
209
214
- func (c Manager ) flattenFiles (root string ) error {
210
+ func (c Editor ) flattenFiles (root string ) error {
215
211
return eachFiles (root , func (path string ) error {
216
212
return c .flattenFile (path )
217
213
})
218
214
}
219
215
220
- func (c Manager ) flattenFile (path string ) error {
216
+ func (c Editor ) flattenFile (path string ) error {
221
217
if ! matchString (path , c .FilesFlattened ) {
222
218
return nil
223
219
}
@@ -231,23 +227,23 @@ func (c Manager) flattenFile(path string) error {
231
227
return os .Rename (path , dest )
232
228
}
233
229
234
- func (c Manager ) deleteFiles (root string ) error {
230
+ func (c Editor ) deleteFiles (root string ) error {
235
231
return eachFiles (root , func (path string ) error {
236
232
return c .DeleteFile (path , func () bool {
237
233
return matchAnyRule (path , path , c .FilesDeleted )
238
234
})
239
235
})
240
236
}
241
237
242
- func (c Manager ) DeleteDir (path string ) error {
238
+ func (c Editor ) DeleteDir (path string ) error {
243
239
if ! pathx .Exists (path ) {
244
240
return nil
245
241
}
246
242
log .Infof ("deleting directory '%s'" , path )
247
243
return os .RemoveAll (path )
248
244
}
249
245
250
- func (c Manager ) DeleteFile (path string , allowedFunc func () bool ) error {
246
+ func (c Editor ) DeleteFile (path string , allowedFunc func () bool ) error {
251
247
if ! pathx .Exists (path ) || allowedFunc != nil && ! allowedFunc () {
252
248
return nil
253
249
}
0 commit comments