main.go 3.3 KB

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