main.go 2.2 KB

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