main.go 3.4 KB

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