main.go 3.6 KB

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