Xray-core/infra/vprotogen/main.go

102 lines
2.5 KiB
Go
Raw Permalink Normal View History

2020-11-25 11:01:53 +00:00
package main
import (
2020-12-10 03:52:05 +00:00
"flag"
2020-11-25 11:01:53 +00:00
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/core"
2020-11-25 11:01:53 +00:00
)
2020-12-10 03:52:05 +00:00
var directory = flag.String("pwd", "", "Working directory of Xray vprotogen.")
2020-11-25 11:01:53 +00:00
func main() {
2020-12-10 03:52:05 +00:00
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of vprotogen:\n")
flag.PrintDefaults()
}
flag.Parse()
if !filepath.IsAbs(*directory) {
pwd, wdErr := os.Getwd()
if wdErr != nil {
fmt.Println("Can not get current working directory.")
os.Exit(1)
}
*directory = filepath.Join(pwd, *directory)
2020-11-25 11:01:53 +00:00
}
2020-12-10 03:52:05 +00:00
pwd := *directory
2020-11-25 11:01:53 +00:00
GOBIN := common.GetGOBIN()
binPath := os.Getenv("PATH")
pathSlice := []string{binPath, GOBIN, pwd}
binPath = strings.Join(pathSlice, string(os.PathListSeparator))
os.Setenv("PATH", binPath)
EXE := ""
if runtime.GOOS == "windows" {
EXE = ".exe"
}
protoc := "protoc" + EXE
if path, err := exec.LookPath(protoc); err != nil {
fmt.Println("Make sure that you have `" + protoc + "` in your system path or current path. To download it, please visit https://github.com/protocolbuffers/protobuf/releases")
os.Exit(1)
} else {
protoc = path
}
protoFilesMap := make(map[string][]string)
2020-12-10 03:52:05 +00:00
walkErr := filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error {
2020-11-25 11:01:53 +00:00
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") {
2020-12-10 03:52:05 +00:00
path = path[len(pwd)+1:]
2020-11-25 11:01:53 +00:00
protoFilesMap[dir] = append(protoFilesMap[dir], path)
}
return nil
})
if walkErr != nil {
fmt.Println(walkErr)
os.Exit(1)
}
for _, files := range protoFilesMap {
for _, relProtoFile := range files {
var args []string
if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] {
args = []string{"--gofast_out", pwd, "--gofast_opt", "paths=source_relative", "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE}
} else {
args = []string{"--go_out", pwd, "--go_opt", "paths=source_relative", "--go-grpc_out", pwd, "--go-grpc_opt", "paths=source_relative", "--plugin", "protoc-gen-go=" + GOBIN + "/protoc-gen-go" + EXE, "--plugin", "protoc-gen-go-grpc=" + GOBIN + "/protoc-gen-go-grpc" + EXE}
}
args = append(args, relProtoFile)
cmd := exec.Command(protoc, args...)
cmd.Env = append(cmd.Env, os.Environ()...)
2020-12-10 03:52:05 +00:00
cmd.Dir = pwd
2020-11-25 11:01:53 +00:00
output, cmdErr := cmd.CombinedOutput()
if len(output) > 0 {
fmt.Println(string(output))
}
if cmdErr != nil {
fmt.Println(cmdErr)
os.Exit(1)
}
}
}
}