From 9c420a826ce6a387384ff3b16f2097036f1d094d Mon Sep 17 00:00:00 2001 From: thesai <1021828630@qq.com> Date: Tue, 26 Mar 2024 13:12:15 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E8=BD=A6?= =?UTF-8?q?=E5=BA=93=E9=97=A8PSL=E6=A8=A1=E5=9E=8B=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E5=8F=8A=E7=9B=B8=E5=85=B3ecs=E9=80=BB=E8=BE=91=EF=BC=9B=20?= =?UTF-8?q?=E3=80=90=E6=94=B9=E5=8A=A8=E3=80=91=E8=BD=A6=E5=BA=93=E9=97=A8?= =?UTF-8?q?ecs=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 86 --------------------------------------------------------- 1 file changed, 86 deletions(-) delete mode 100644 main.go diff --git a/main.go b/main.go deleted file mode 100644 index 3f78414..0000000 --- a/main.go +++ /dev/null @@ -1,86 +0,0 @@ -package main - -import ( - "bufio" - "fmt" - "io/fs" - "log" - "os" - "os/exec" - "path/filepath" - "strings" -) - -var ( - basePath, _ = os.Getwd() - protoFolder = filepath.Join(basePath, "rts-sim-testing-message", "protos") - protocPath = filepath.Join(basePath, "rts-sim-testing-message", "protoc-23.1", "bin", "win64", "protoc") - modulePrefix = "joylink.club/bj-rtsts-server" -) - -func main() { - //先安装以下插件 - //go install google.golang.org/protobuf/cmd/protoc-gen-go@latest - - protoFiles := getProtoFiles() - // 编译proto文件为Go文件 - if err := compileProto(protoFiles); err != nil { - log.Fatalf("编译proto文件失败:%v", err) - } -} - -// 获取指定文件夹下的所有proto文件的绝对路径 -func getProtoFiles() []string { - var protoFiles []string - err := filepath.WalkDir(protoFolder, func(path string, d fs.DirEntry, err error) error { - if !d.IsDir() { - protoFiles = append(protoFiles, path) - } - return err - }) - if err != nil { - log.Fatal("获取proto文件列表失败:", err) - } - return protoFiles -} - -// 编译proto文件为Go文件 -func compileProto(protoFiles []string) error { - for _, fileName := range protoFiles { - file, err := os.Open(fileName) - if err != nil { - return err - } - scanner := bufio.NewScanner(file) - var outPath string - for scanner.Scan() { - text := scanner.Text() - if !strings.HasPrefix(text, "option go_package") { - continue - } - start := strings.Index(text, modulePrefix) - if start < 0 { - break - } - start += len(modulePrefix) - dir := "." + text[start:len(text)-2] - err := os.MkdirAll(dir, fs.ModeDir) - if err != nil { - panic(fmt.Sprintf("创建目录 %s 失败:%v", dir, err)) - } - outPath = "paths=source_relative:" + dir - break - } - if outPath == "" { - outPath = "./" - } - cmd := exec.Command(protocPath, "-I="+protoFolder, "--go_out="+outPath, fileName) - fmt.Println(cmd.String()) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return err - } - } - return nil -}