zs
2025-05-20 1772504da433bec2f1695d47e5946c1192e876fb
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
package utils
 
import (
    "net/http"
    "os"
    "path/filepath"
    "strings"
    "unicode"
 
    "github.com/gin-gonic/gin"
    cp "github.com/otiai10/copy"
)
 
// 统一处理错误
func HandlerErr(c *gin.Context, err error) error {
    c.JSON(http.StatusBadRequest, gin.H{"message": err})
    return err
}
 
// 首字母大写
func CapitalizeFirstLetter(s string) string {
    if s == "" {
        return s
    }
    runes := []rune(s)
    runes[0] = unicode.ToUpper(runes[0])
    return string(runes)
}
 
// 首字母小写
func DecapitalizeFirstLetter(s string) string {
    if s == "" {
        return s
    }
    runes := []rune(s)
    runes[0] = unicode.ToLower(runes[0])
    return string(runes)
}
 
func isDirectory(path string) bool {
    info, err := os.Stat(path)
    if err != nil {
        return false // 出错(例如路径不存在),默认返回 false
    }
    return info.IsDir() // 如果是目录,返回 true
}
 
// 读取文件后,并替换文件内容,同步
func ReplaceFileContent(sourceFile string, configArr []string, widgetName string) {
    name := configArr[0]
    entityName := configArr[1]
    oldName := configArr[2]
    oldEntityName := configArr[3]
    sFile, _ := filepath.Abs(sourceFile)
    // 插件名
    upName := CapitalizeFirstLetter(name)
    lowName := DecapitalizeFirstLetter(name)
    // 旧插件名
    upOldName := CapitalizeFirstLetter(oldName)
    lowOldName := DecapitalizeFirstLetter(oldName)
    // 新实体名
    upEntityName := CapitalizeFirstLetter(entityName)
    lowEntityName := DecapitalizeFirstLetter(entityName)
    // 旧实体名
    upOldEntityName := CapitalizeFirstLetter(oldEntityName)
    lowOldEntityName := DecapitalizeFirstLetter(oldEntityName)
    // tFile, _ := filepath.Abs(targetFile)
    content, err := os.ReadFile(sFile)
    if err != nil {
        return
    }
 
    newContentStr := strings.ReplaceAll(string(content), "${{widgetName}}", widgetName)
    newStr := strings.ReplaceAll(string(newContentStr), upOldName, upName)
    pluginStr := strings.ReplaceAll(newStr, lowOldName, lowName)
    entityStr := strings.ReplaceAll(pluginStr, upOldEntityName, upEntityName)
    entityLowStr := strings.ReplaceAll(entityStr, lowOldEntityName, lowEntityName)
    // 写入到文件中
    os.WriteFile(sourceFile, []byte(entityLowStr), 0777)
}
 
// 修改文件名称
func RenameFile(dir string, targetName string, oldName string, entity ...string) {
    slashDir := filepath.ToSlash(dir)
    targetDir := strings.ReplaceAll(slashDir, oldName, targetName)
    if len(entity) == 0 {
 
        if strings.Contains(targetDir, "MyPluginName") || strings.Contains(targetDir, "MyEntityName") {
            return
        }
        if !isDirectory(slashDir) {
            cp.Copy(slashDir, targetDir)
        }
    } else {
        targetEntityDir := strings.ReplaceAll(targetDir, entity[1], entity[0])
        if strings.Contains(targetEntityDir, "MyPluginName") || strings.Contains(targetEntityDir, "MyEntityName") {
            return
        }
        if !isDirectory(slashDir) {
            cp.Copy(slashDir, targetEntityDir)
        }
    }
}