2023-07-28 15:23:15 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-11 10:24:56 +08:00
|
|
|
|
"bufio"
|
2023-07-28 15:23:15 +08:00
|
|
|
|
"fmt"
|
2023-08-28 15:30:46 +08:00
|
|
|
|
"io/fs"
|
2023-07-28 15:23:15 +08:00
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
2024-01-11 10:24:56 +08:00
|
|
|
|
"strings"
|
2023-07-28 15:23:15 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2024-01-11 10:24:56 +08:00
|
|
|
|
basePath, _ = os.Getwd()
|
|
|
|
|
protoFolder = filepath.Join(basePath, "bj-rtss-message", "protos")
|
|
|
|
|
protocPath = filepath.Join(basePath, "bj-rtss-message", "protoc-23.1", "bin", "win64", "protoc")
|
|
|
|
|
modulePrefix = "joylink.club/bj-rtsts-server"
|
2023-07-28 15:23:15 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 15:30:46 +08:00
|
|
|
|
// 获取指定文件夹下的所有proto文件的绝对路径
|
2023-07-28 15:23:15 +08:00
|
|
|
|
func getProtoFiles() []string {
|
|
|
|
|
var protoFiles []string
|
2023-08-28 15:30:46 +08:00
|
|
|
|
err := filepath.WalkDir(protoFolder, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
|
if !d.IsDir() {
|
|
|
|
|
protoFiles = append(protoFiles, path)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
})
|
2023-07-28 15:23:15 +08:00
|
|
|
|
if err != nil {
|
2023-08-28 15:30:46 +08:00
|
|
|
|
log.Fatal("获取proto文件列表失败:", err)
|
2023-07-28 15:23:15 +08:00
|
|
|
|
}
|
|
|
|
|
return protoFiles
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 编译proto文件为Go文件
|
|
|
|
|
func compileProto(protoFiles []string) error {
|
2023-08-28 15:30:46 +08:00
|
|
|
|
for _, fileName := range protoFiles {
|
2024-01-11 10:24:56 +08:00
|
|
|
|
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)
|
2023-07-28 15:23:15 +08:00
|
|
|
|
fmt.Println(cmd.String())
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|