main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "v2ray.com/core"
  10. "v2ray.com/core/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. EXE := ""
  20. if runtime.GOOS == "windows" {
  21. EXE = ".exe"
  22. }
  23. protoc := "protoc" + EXE
  24. if path, err := exec.LookPath(protoc); err != nil {
  25. fmt.Println("Make sure that you have `" + protoc + "` in your system or current path, please visit https://github.com/protocolbuffers/protobuf/releases")
  26. os.Exit(1)
  27. } else {
  28. protoc = path
  29. }
  30. protoFilesMap := make(map[string][]string)
  31. walkErr := filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
  32. if err != nil {
  33. fmt.Println(err)
  34. return err
  35. }
  36. if info.IsDir() {
  37. return nil
  38. }
  39. dir := filepath.Dir(path)
  40. filename := filepath.Base(path)
  41. if strings.HasSuffix(filename, ".proto") {
  42. protoFilesMap[dir] = append(protoFilesMap[dir], path)
  43. }
  44. return nil
  45. })
  46. if walkErr != nil {
  47. fmt.Println(walkErr)
  48. os.Exit(1)
  49. }
  50. for _, files := range protoFilesMap {
  51. for _, relProtoFile := range files {
  52. var args []string
  53. if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] {
  54. args = []string{"--gofast_out", pwd, "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE}
  55. } else {
  56. args = []string{"--go_out", pwd, "--go-grpc_out", pwd, "--plugin", "protoc-gen-go=" + GOBIN + "/protoc-gen-go" + EXE, "--plugin", "protoc-gen-go-grpc=" + GOBIN + "/protoc-gen-go-grpc" + EXE}
  57. }
  58. args = append(args, relProtoFile)
  59. cmd := exec.Command(protoc, args...)
  60. cmd.Env = append(cmd.Env, os.Environ()...)
  61. cmd.Env = append(cmd.Env, "GOBIN="+GOBIN)
  62. output, cmdErr := cmd.CombinedOutput()
  63. if len(output) > 0 {
  64. fmt.Println(string(output))
  65. }
  66. if cmdErr != nil {
  67. fmt.Println(cmdErr)
  68. os.Exit(1)
  69. }
  70. }
  71. }
  72. moduleName, gmnErr := common.GetModuleName(pwd)
  73. if gmnErr != nil {
  74. fmt.Println(gmnErr)
  75. os.Exit(1)
  76. }
  77. modulePath := filepath.Join(strings.Split(moduleName, "/")...)
  78. pbGoFilesMap := make(map[string][]string)
  79. walkErr2 := filepath.Walk(modulePath, func(path string, info os.FileInfo, err error) error {
  80. if err != nil {
  81. fmt.Println(err)
  82. return err
  83. }
  84. if info.IsDir() {
  85. return nil
  86. }
  87. dir := filepath.Dir(path)
  88. filename := filepath.Base(path)
  89. if strings.HasSuffix(filename, ".pb.go") {
  90. pbGoFilesMap[dir] = append(pbGoFilesMap[dir], path)
  91. }
  92. return nil
  93. })
  94. if walkErr2 != nil {
  95. fmt.Println(walkErr2)
  96. os.Exit(1)
  97. }
  98. var err error
  99. for _, srcPbGoFiles := range pbGoFilesMap {
  100. for _, srcPbGoFile := range srcPbGoFiles {
  101. var dstPbGoFile string
  102. dstPbGoFile, err = filepath.Rel(modulePath, srcPbGoFile)
  103. if err != nil {
  104. fmt.Println(err)
  105. continue
  106. }
  107. err = os.Link(srcPbGoFile, dstPbGoFile)
  108. if err != nil {
  109. if os.IsNotExist(err) {
  110. fmt.Printf("'%s' does not exist\n", srcPbGoFile)
  111. continue
  112. }
  113. if os.IsPermission(err) {
  114. fmt.Println(err)
  115. continue
  116. }
  117. if os.IsExist(err) {
  118. err = os.Remove(dstPbGoFile)
  119. if err != nil {
  120. fmt.Printf("Failed to delete file '%s'\n", dstPbGoFile)
  121. continue
  122. }
  123. err = os.Rename(srcPbGoFile, dstPbGoFile)
  124. if err != nil {
  125. fmt.Printf("Can not move '%s' to '%s'\n", srcPbGoFile, dstPbGoFile)
  126. }
  127. continue
  128. }
  129. }
  130. err = os.Rename(srcPbGoFile, dstPbGoFile)
  131. if err != nil {
  132. fmt.Printf("Can not move '%s' to '%s'\n", srcPbGoFile, dstPbGoFile)
  133. }
  134. continue
  135. }
  136. }
  137. if err == nil {
  138. err = os.RemoveAll(strings.Split(modulePath, "/")[0])
  139. if err != nil {
  140. fmt.Println(err)
  141. }
  142. }
  143. }