1
0

main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "github.com/xtls/xray-core/common"
  11. "github.com/xtls/xray-core/core"
  12. )
  13. var directory = flag.String("pwd", "", "Working directory of Xray vprotogen.")
  14. func main() {
  15. flag.Usage = func() {
  16. fmt.Fprintf(flag.CommandLine.Output(), "Usage of vprotogen:\n")
  17. flag.PrintDefaults()
  18. }
  19. flag.Parse()
  20. if !filepath.IsAbs(*directory) {
  21. pwd, wdErr := os.Getwd()
  22. if wdErr != nil {
  23. fmt.Println("Can not get current working directory.")
  24. os.Exit(1)
  25. }
  26. *directory = filepath.Join(pwd, *directory)
  27. }
  28. pwd := *directory
  29. GOBIN := common.GetGOBIN()
  30. binPath := os.Getenv("PATH")
  31. pathSlice := []string{binPath, GOBIN, pwd}
  32. binPath = strings.Join(pathSlice, string(os.PathListSeparator))
  33. os.Setenv("PATH", binPath)
  34. EXE := ""
  35. if runtime.GOOS == "windows" {
  36. EXE = ".exe"
  37. }
  38. protoc := "protoc" + EXE
  39. if path, err := exec.LookPath(protoc); err != nil {
  40. 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")
  41. os.Exit(1)
  42. } else {
  43. protoc = path
  44. }
  45. protoFilesMap := make(map[string][]string)
  46. walkErr := filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error {
  47. if err != nil {
  48. fmt.Println(err)
  49. return err
  50. }
  51. if info.IsDir() {
  52. return nil
  53. }
  54. dir := filepath.Dir(path)
  55. filename := filepath.Base(path)
  56. if strings.HasSuffix(filename, ".proto") {
  57. path = path[len(pwd)+1:]
  58. protoFilesMap[dir] = append(protoFilesMap[dir], path)
  59. }
  60. return nil
  61. })
  62. if walkErr != nil {
  63. fmt.Println(walkErr)
  64. os.Exit(1)
  65. }
  66. for _, files := range protoFilesMap {
  67. for _, relProtoFile := range files {
  68. var args []string
  69. if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] {
  70. args = []string{"--gofast_out", pwd, "--gofast_opt", "paths=source_relative", "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE}
  71. } else {
  72. 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}
  73. }
  74. args = append(args, relProtoFile)
  75. cmd := exec.Command(protoc, args...)
  76. cmd.Env = append(cmd.Env, os.Environ()...)
  77. cmd.Dir = pwd
  78. output, cmdErr := cmd.CombinedOutput()
  79. if len(output) > 0 {
  80. fmt.Println(string(output))
  81. }
  82. if cmdErr != nil {
  83. fmt.Println(cmdErr)
  84. os.Exit(1)
  85. }
  86. }
  87. }
  88. }