57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"io/fs"
|
||
"log"
|
||
"os"
|
||
"os/exec"
|
||
"path/filepath"
|
||
)
|
||
|
||
var (
|
||
basePath, _ = os.Getwd()
|
||
protoFolder = filepath.Join(basePath, "bj-rtss-message", "protos")
|
||
protocPath = filepath.Join(basePath, "bj-rtss-message", "protoc-23.1", "bin", "win64", "protoc")
|
||
)
|
||
|
||
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 {
|
||
cmd := exec.Command(protocPath, "-I="+protoFolder, "--go_out=./", fileName)
|
||
fmt.Println(cmd.String())
|
||
cmd.Stdout = os.Stdout
|
||
cmd.Stderr = os.Stderr
|
||
if err := cmd.Run(); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|