project.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package project
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "regexp"
  8. "strings"
  9. "github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
  10. )
  11. const (
  12. constGo = "go"
  13. constProtoC = "protoc"
  14. constGoMod = "go env GOMOD"
  15. constGoPath = "go env GOPATH"
  16. constProtoCGenGo = "protoc-gen-go"
  17. )
  18. type (
  19. Project struct {
  20. Path string // Project path name
  21. Name string // Project name
  22. Package string // The service related package
  23. GoMod GoMod
  24. }
  25. GoMod struct {
  26. Module string // The gomod module name
  27. Path string // The gomod related path
  28. }
  29. )
  30. func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
  31. _, err := exec.LookPath(constGo)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if checkGrpcEnv {
  36. _, err = exec.LookPath(constProtoC)
  37. if err != nil {
  38. return nil, err
  39. }
  40. _, err = exec.LookPath(constProtoCGenGo)
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. var (
  46. goMod, module string
  47. goPath string
  48. name, path string
  49. pkg string
  50. )
  51. ret, err := execx.Run(constGoMod, projectDir)
  52. if err != nil {
  53. return nil, err
  54. }
  55. goMod = strings.TrimSpace(ret)
  56. if goMod == os.DevNull {
  57. goMod = ""
  58. }
  59. ret, err = execx.Run(constGoPath, "")
  60. if err != nil {
  61. return nil, err
  62. }
  63. goPath = strings.TrimSpace(ret)
  64. src := filepath.Join(goPath, "src")
  65. if len(goMod) > 0 {
  66. path = filepath.Dir(goMod)
  67. name = filepath.Base(path)
  68. data, err := ioutil.ReadFile(goMod)
  69. if err != nil {
  70. return nil, err
  71. }
  72. module, err = matchModule(data)
  73. if err != nil {
  74. return nil, err
  75. }
  76. } else {
  77. pwd, err := execx.Run("pwd", projectDir)
  78. if err != nil {
  79. return nil, err
  80. }
  81. pwd = filepath.Clean(strings.TrimSpace(pwd))
  82. if !strings.HasPrefix(pwd, src) {
  83. absPath, err := filepath.Abs(projectDir)
  84. if err != nil {
  85. return nil, err
  86. }
  87. name = filepath.Clean(filepath.Base(absPath))
  88. path = projectDir
  89. pkg = name
  90. } else {
  91. r := strings.TrimPrefix(pwd, src+string(filepath.Separator))
  92. name = filepath.Dir(r)
  93. if name == "." {
  94. name = r
  95. }
  96. path = filepath.Join(src, name)
  97. pkg = r
  98. }
  99. module = name
  100. }
  101. return &Project{
  102. Name: name,
  103. Path: path,
  104. Package: pkg,
  105. GoMod: GoMod{
  106. Module: module,
  107. Path: goMod,
  108. },
  109. }, nil
  110. }
  111. func matchModule(data []byte) (string, error) {
  112. text := string(data)
  113. re := regexp.MustCompile(`(?m)^\s*module\s+[a-z0-9/\-.]+$`)
  114. matches := re.FindAllString(text, -1)
  115. if len(matches) == 1 {
  116. target := matches[0]
  117. index := strings.Index(target, "module")
  118. return strings.TrimSpace(target[index+6:]), nil
  119. }
  120. return "", nil
  121. }