-
Notifications
You must be signed in to change notification settings - Fork 3
/
replacer.go
416 lines (360 loc) · 10 KB
/
replacer.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package replacer
import (
"bytes"
"embed"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/zhufuyi/pkg/gofile"
)
var _ Replacer = (*replacerInfo)(nil)
// Replacer 接口
type Replacer interface {
SetReplacementFields(fields []Field)
SetSubDirsAndFiles(subDirs []string, subFiles ...string)
SetIgnoreSubDirs(dirs ...string)
SetIgnoreSubFiles(filenames ...string)
SetOutputDir(absDir string, name ...string) error
GetOutputDir() string
GetSourcePath() string
SaveFiles() error
ReadFile(filename string) ([]byte, error)
}
// replacerInfo replacer信息
type replacerInfo struct {
path string // 模板目录或文件
fs embed.FS // 模板目录对应二进制对象
isActual bool // fs字段是否来源实际路径,如果为true,使用io操作文件,如果为false使用fs操作文件
files []string // 模板文件列表
ignoreFiles []string // 忽略替换的文件列表
ignoreDirs []string // 忽略处理的子目录
replacementFields []Field // 从模板文件转为新文件需要替换的字符
outPath string // 输出替换后文件存放目录路径
}
// New 根据指定路径创建replacer
func New(path string) (Replacer, error) {
files, err := gofile.ListFiles(path)
if err != nil {
return nil, err
}
path, _ = filepath.Abs(path)
return &replacerInfo{
path: path,
isActual: true,
files: files,
replacementFields: []Field{},
}, nil
}
// NewFS 根据嵌入的路径创建replacer
func NewFS(path string, fs embed.FS) (Replacer, error) {
files, err := listFiles(path, fs)
if err != nil {
return nil, err
}
return &replacerInfo{
path: path,
fs: fs,
isActual: false,
files: files,
replacementFields: []Field{},
}, nil
}
// Field 替换字段信息
type Field struct {
Old string // 模板字段
New string // 新字段
IsCaseSensitive bool // 第一个字母是否区分大小写
}
// SetReplacementFields 设置替换字段,注:old字符尽量不要存在包含关系,如果存在,在设置Field时注意先后顺序
func (r *replacerInfo) SetReplacementFields(fields []Field) {
var newFields []Field
for _, field := range fields {
if field.IsCaseSensitive && isFirstAlphabet(field.Old) { // 拆分首字母大小写两个字段
newFields = append(newFields,
Field{ // 把第一个字母转为大写
Old: strings.ToUpper(field.Old[:1]) + field.Old[1:],
New: strings.ToUpper(field.New[:1]) + field.New[1:],
},
Field{ // 把第一个字母转为小写
Old: strings.ToLower(field.Old[:1]) + field.Old[1:],
New: strings.ToLower(field.New[:1]) + field.New[1:],
},
)
} else {
newFields = append(newFields, field)
}
}
r.replacementFields = newFields
}
// SetSubDirsAndFiles 设置处理指定子目录,其他目录下文件忽略处理
func (r *replacerInfo) SetSubDirsAndFiles(subDirs []string, subFiles ...string) {
if len(subDirs) == 0 {
return
}
subDirs = r.covertPathsDelimiter(subDirs...)
subFiles = r.covertPathsDelimiter(subFiles...)
var files []string
isExistFile := make(map[string]struct{})
for _, file := range r.files {
for _, dir := range subDirs {
if isSubPath(file, dir) {
// 避免重复文件
if _, ok := isExistFile[file]; ok {
continue
} else {
isExistFile[file] = struct{}{}
}
files = append(files, file)
}
}
for _, sf := range subFiles {
if isMatchFile(file, sf) {
// 避免重复文件
if _, ok := isExistFile[file]; ok {
continue
} else {
isExistFile[file] = struct{}{}
}
files = append(files, file)
}
}
}
if len(files) == 0 {
return
}
r.files = files
}
// SetIgnoreFiles 设置忽略处理的文件
func (r *replacerInfo) SetIgnoreSubFiles(filenames ...string) {
r.ignoreFiles = append(r.ignoreFiles, filenames...)
}
// SetIgnoreSubDirs 设置忽略处理的子目录
func (r *replacerInfo) SetIgnoreSubDirs(dirs ...string) {
dirs = r.covertPathsDelimiter(dirs...)
r.ignoreDirs = append(r.ignoreDirs, dirs...)
}
// SetOutputDir 设置输出目录,优先使用absPath,如果absPath为空,自动在当前目录根据参数name名称生成输出目录
func (r *replacerInfo) SetOutputDir(absPath string, name ...string) error {
// 输出到指定目录
if absPath != "" {
abs, err := filepath.Abs(absPath)
if err != nil {
return err
}
r.outPath = abs
return nil
}
// 输出到当前目录
subPath := strings.Join(name, "_")
pwd, err := os.Getwd()
if err != nil {
return err
}
r.outPath = pwd + gofile.GetPathDelimiter() + subPath + "_" + time.Now().Format("150405")
return nil
}
// GetOutputDir 获取输出目录
func (r *replacerInfo) GetOutputDir() string {
return r.outPath
}
// GetSourcePath 获取源路径
func (r *replacerInfo) GetSourcePath() string {
return r.path
}
// ReadFile 读取文件内容
func (r *replacerInfo) ReadFile(filename string) ([]byte, error) {
filename = r.covertPathDelimiter(filename)
foundFile := []string{}
for _, file := range r.files {
if strings.Contains(file, filename) && gofile.GetFilename(file) == gofile.GetFilename(filename) {
foundFile = append(foundFile, file)
}
}
if len(foundFile) != 1 {
return nil, fmt.Errorf("total %d file named '%s', files=%+v", len(foundFile), filename, foundFile)
}
if r.isActual {
return os.ReadFile(foundFile[0])
}
return r.fs.ReadFile(foundFile[0])
}
// SaveFiles 导出文件
func (r *replacerInfo) SaveFiles() error {
if r.outPath == "" {
r.outPath = gofile.GetRunPath() + gofile.GetPathDelimiter() + "generate_" + time.Now().Format("150405")
}
var existFiles []string
var writeData = make(map[string][]byte)
for _, file := range r.files {
if r.isInIgnoreDir(file) || r.isIgnoreFile(file) {
continue
}
// 从二进制读取模板文件内容使用embed.FS,如果要从指定目录读取使用os.ReadFile
var data []byte
var err error
if r.isActual {
data, err = os.ReadFile(file)
} else {
data, err = r.fs.ReadFile(file)
}
if err != nil {
return err
}
// 替换文本内容
for _, field := range r.replacementFields {
data = bytes.ReplaceAll(data, []byte(field.Old), []byte(field.New))
}
// 获取新文件路径
newFilePath := r.getNewFilePath(file)
dir, filename := filepath.Split(newFilePath)
// 替换文件名和文件夹名
for _, field := range r.replacementFields {
if strings.Contains(dir, field.Old) {
dir = strings.ReplaceAll(dir, field.Old, field.New)
}
if strings.Contains(filename, field.Old) {
filename = strings.ReplaceAll(filename, field.Old, field.New)
}
if newFilePath != dir+filename {
newFilePath = dir + filename
}
}
if gofile.IsExists(newFilePath) {
existFiles = append(existFiles, newFilePath)
}
writeData[newFilePath] = data
}
if len(existFiles) > 0 {
//nolint
return fmt.Errorf("existing files detected\n %s\nCode generation has been cancelled\n",
strings.Join(existFiles, "\n "))
}
for file, data := range writeData {
// 保存文件
err := saveToNewFile(file, data)
if err != nil {
return err
}
}
return nil
}
func (r *replacerInfo) isIgnoreFile(file string) bool {
isIgnore := false
//_, filename := filepath.Split(file)
for _, v := range r.ignoreFiles {
if isMatchFile(file, v) {
isIgnore = true
break
}
//if filename == v {
// isIgnore = true
// break
//}
}
return isIgnore
}
func (r *replacerInfo) isInIgnoreDir(file string) bool {
isIgnore := false
dir, _ := filepath.Split(file)
for _, v := range r.ignoreDirs {
if strings.Contains(dir, v) {
isIgnore = true
break
}
}
return isIgnore
}
func (r *replacerInfo) getNewFilePath(file string) string {
var newFilePath string
if r.isActual {
newFilePath = r.outPath + strings.Replace(file, r.path, "", 1)
} else {
newFilePath = r.outPath + strings.Replace(file, r.path, "", 1)
}
if gofile.IsWindows() {
newFilePath = strings.ReplaceAll(newFilePath, "/", "\\")
}
return newFilePath
}
// 如果是windows,转换路径分割符
func (r *replacerInfo) covertPathDelimiter(filePath string) string {
if r.isActual && gofile.IsWindows() {
filePath = strings.ReplaceAll(filePath, "/", "\\")
}
return filePath
}
// 如果是windows,批量转换路径分割符
func (r *replacerInfo) covertPathsDelimiter(filePaths ...string) []string {
if r.isActual && gofile.IsWindows() {
filePathsTmp := []string{}
for _, dir := range filePaths {
filePathsTmp = append(filePathsTmp, strings.ReplaceAll(dir, "/", "\\"))
}
return filePathsTmp
}
return filePaths
}
func saveToNewFile(filePath string, data []byte) error {
// 创建目录
dir, _ := filepath.Split(filePath)
err := os.MkdirAll(dir, 0666)
if err != nil {
return err
}
// 保存文件
err = os.WriteFile(filePath, data, 0666)
if err != nil {
return err
}
return nil
}
// 遍历嵌入的目录下所有文件,返回文件的绝对路径
func listFiles(path string, fs embed.FS) ([]string, error) {
files := []string{}
err := walkDir(path, &files, fs)
return files, err
}
// 通过迭代方式遍历嵌入的目录
func walkDir(dirPath string, allFiles *[]string, fs embed.FS) error {
files, err := fs.ReadDir(dirPath) // 读取目录下文件
if err != nil {
return err
}
for _, file := range files {
deepFile := dirPath + "/" + file.Name()
if file.IsDir() {
_ = walkDir(deepFile, allFiles, fs)
continue
}
*allFiles = append(*allFiles, deepFile)
}
return nil
}
// 判断字符串第一个字符是字母
func isFirstAlphabet(str string) bool {
if len(str) == 0 {
return false
}
if (str[0] >= 'A' && str[0] <= 'Z') || (str[0] >= 'a' && str[0] <= 'z') {
return true
}
return false
}
func isSubPath(filePath string, subPath string) bool {
dir, _ := filepath.Split(filePath)
return strings.Contains(dir, subPath)
}
func isMatchFile(filePath string, sf string) bool {
dir1, file1 := filepath.Split(filePath)
dir2, file2 := filepath.Split(sf)
if file1 != file2 {
return false
}
l1, l2 := len(dir1), len(dir2)
if l1 >= l2 && dir1[l1-l2:] == dir2 {
return true
}
return false
}