main.go 3.6 KB

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