1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-09 01:10:44 +00:00
v2fly/infra/vprotogen/main.go

74 lines
1.7 KiB
Go
Raw Normal View History

2019-02-10 18:04:11 +00:00
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var protocMap = map[string]string{
2020-08-26 09:37:51 +00:00
"windows": filepath.Join(".dev", "protoc", "windows", "protoc.exe"),
"darwin": filepath.Join(".dev", "protoc", "macos", "protoc"),
"linux": filepath.Join(".dev", "protoc", "linux", "protoc"),
2019-02-10 18:04:11 +00:00
}
var (
repo = flag.String("repo", "", "Repo for protobuf generation, such as v2ray.com/core")
)
func main() {
flag.Parse()
protofiles := make(map[string][]string)
protoc := protocMap[runtime.GOOS]
gosrc := filepath.Join(os.Getenv("GOPATH"), "src")
reporoot := filepath.Join(os.Getenv("GOPATH"), "src", *repo)
filepath.Walk(reporoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
if info.IsDir() {
return nil
}
dir := filepath.Dir(path)
filename := filepath.Base(path)
if strings.HasSuffix(filename, ".proto") {
protofiles[dir] = append(protofiles[dir], path)
}
return nil
})
var protoFilesUsingProtocGenGoFast = map[string]bool{"proxy/vless/encoding/addons.proto": true}
2019-02-10 18:04:11 +00:00
for _, files := range protofiles {
for _, absPath := range files {
relPath, _ := filepath.Rel(reporoot, absPath)
args := make([]string, 0)
if protoFilesUsingProtocGenGoFast[relPath] {
args = []string{"--proto_path", reporoot, "--gofast_out", gosrc}
} else {
args = []string{"--proto_path", reporoot, "--go_out", gosrc, "--go-grpc_out", gosrc}
}
args = append(args, absPath)
cmd := exec.Command(protoc, args...)
cmd.Env = append(cmd.Env, os.Environ()...)
output, err := cmd.CombinedOutput()
if len(output) > 0 {
fmt.Println(string(output))
}
if err != nil {
fmt.Println(err)
}
2019-02-10 18:04:11 +00:00
}
}
}