main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. )
  11. var protocMap = map[string]string{
  12. "windows": filepath.Join(".dev", "protoc", "windows", "protoc.exe"),
  13. "darwin": filepath.Join(".dev", "protoc", "macos", "protoc"),
  14. "linux": filepath.Join(".dev", "protoc", "linux", "protoc"),
  15. }
  16. var (
  17. repo = flag.String("repo", "", "Repo for protobuf generation, such as v2ray.com/core")
  18. )
  19. func main() {
  20. flag.Parse()
  21. protofiles := make(map[string][]string)
  22. protoc := protocMap[runtime.GOOS]
  23. gosrc := filepath.Join(os.Getenv("GOPATH"), "src")
  24. reporoot := filepath.Join(os.Getenv("GOPATH"), "src", *repo)
  25. filepath.Walk(reporoot, func(path string, info os.FileInfo, err error) error {
  26. if err != nil {
  27. fmt.Println(err)
  28. return err
  29. }
  30. if info.IsDir() {
  31. return nil
  32. }
  33. dir := filepath.Dir(path)
  34. filename := filepath.Base(path)
  35. if strings.HasSuffix(filename, ".proto") {
  36. protofiles[dir] = append(protofiles[dir], path)
  37. }
  38. return nil
  39. })
  40. var protoFilesUsingProtocGenGoFast = map[string]bool{"proxy/vless/encoding/addons.proto": true}
  41. for _, files := range protofiles {
  42. for _, absPath := range files {
  43. relPath, _ := filepath.Rel(reporoot, absPath)
  44. args := make([]string, 0)
  45. if protoFilesUsingProtocGenGoFast[relPath] {
  46. args = []string{"--proto_path", reporoot, "--gofast_out", gosrc}
  47. } else {
  48. args = []string{"--proto_path", reporoot, "--go_out", gosrc, "--go-grpc_out", gosrc}
  49. }
  50. args = append(args, absPath)
  51. cmd := exec.Command(protoc, args...)
  52. cmd.Env = append(cmd.Env, os.Environ()...)
  53. output, err := cmd.CombinedOutput()
  54. if len(output) > 0 {
  55. fmt.Println(string(output))
  56. }
  57. if err != nil {
  58. fmt.Println(err)
  59. }
  60. }
  61. }
  62. }