u
This commit is contained in:
1 parent
ac0843c041
commit
a917be08d1
65 files changed
+8043
No files matched your search
Generated
+8
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
Generated
+9
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/genpb.iml" filepath="$PROJECT_DIR$/.idea/genpb.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
module genpb
|
||||
|
||||
go 1.25
|
||||
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 处理单个 pb.go 文件,去掉 ,omitempty
|
||||
func handleFile(path string) error {
|
||||
fmt.Println("正在处理文件:", path)
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 去掉 ,omitempty 标签
|
||||
newContent := strings.ReplaceAll(string(content), ",omitempty", "")
|
||||
|
||||
return os.WriteFile(path, []byte(newContent), 0644)
|
||||
}
|
||||
|
||||
// 查找 biz/model 目录及其子目录下最新的 .pb.go 文件,忽略 biz/model/api
|
||||
func findLatestPBGoFile(root string) (string, error) {
|
||||
var latestFile string
|
||||
var latestTime time.Time
|
||||
ignoreDir := filepath.Join(root, "api")
|
||||
|
||||
err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 忽略 biz/model/api 目录
|
||||
if d.IsDir() && strings.HasPrefix(path, ignoreDir) {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
|
||||
if !d.IsDir() && strings.HasSuffix(d.Name(), ".pb.go") {
|
||||
info, err := d.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
modTime := info.ModTime()
|
||||
if modTime.After(latestTime) {
|
||||
latestTime = modTime
|
||||
latestFile = path
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if latestFile == "" {
|
||||
return "", fmt.Errorf("未找到 .pb.go 文件")
|
||||
}
|
||||
|
||||
return latestFile, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("用法: genpb.exe <proto文件路径>")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
protoFile := os.Args[1]
|
||||
|
||||
// 1. 执行 hz update(同步等待执行完毕)
|
||||
cmd := exec.Command("hz", "update", "-I", "idl", "-idl", protoFile)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
fmt.Println("正在执行 hz update ...")
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Println("hz update 执行失败:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("hz update 执行完成.")
|
||||
|
||||
// 2. 查找最新 pb.go 文件并处理
|
||||
modelDir := "biz/model"
|
||||
latestFile, err := findLatestPBGoFile(modelDir)
|
||||
if err != nil {
|
||||
fmt.Println("查找最新 .pb.go 文件失败:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := handleFile(latestFile); err != nil {
|
||||
fmt.Println("处理 pb.go 文件失败:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("最新 .pb.go 文件处理完成:", latestFile)
|
||||
}
|
||||
Reference in new issue
Block a user