make.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package make
  2. import (
  3. "embed"
  4. "fmt"
  5. "github.com/spf13/cobra"
  6. "github.com/runningwater/gohub/pkg/console"
  7. "github.com/runningwater/gohub/pkg/file"
  8. "github.com/runningwater/gohub/pkg/str"
  9. )
  10. // Model 参数解释
  11. // 单个词,用户命令传参,以 User 模型为例:
  12. // - user
  13. // - User
  14. // - users
  15. // - Users
  16. //
  17. // 整理好的数据:
  18. //
  19. // {
  20. // "TableName": "users",
  21. // "StructName": "User",
  22. // "StructNamePlural": "Users"
  23. // "VariableName": "user",
  24. // "VariableNamePlural": "users",
  25. // "PackageName": "user"
  26. // }
  27. //
  28. // -
  29. // 两个词或者以上,用户命令传参,以 TopicComment 模型为例:
  30. // - topic_comment
  31. // - topic_comments
  32. // - TopicComment
  33. // - TopicComments
  34. //
  35. // 整理好的数据:
  36. //
  37. // {
  38. // "TableName": "topic_comments",
  39. // "StructName": "TopicComment",
  40. // "StructNamePlural": "TopicComments"
  41. // "VariableName": "topicComment",
  42. // "VariableNamePlural": "topicComments",
  43. // "PackageName": "topic_comment"
  44. // }
  45. type Model struct {
  46. TableName string
  47. StructName string
  48. StructNamePlural string
  49. VariableName string
  50. VariableNamePlural string
  51. PackageName string
  52. }
  53. // tplsFS 方便我们后面打包的 .tpl 为后缀的文件
  54. //
  55. //go:embed tpls
  56. var tplsFS embed.FS
  57. var CmdMake = &cobra.Command{
  58. Use: "make",
  59. Short: "Generate code and file",
  60. }
  61. func init() {
  62. // 注册 make 子命令
  63. CmdMake.AddCommand(
  64. CmdMakeCMD,
  65. CmdMakeModel,
  66. CmdMakeAPIController,
  67. CmdMakeRequest,
  68. CmdMakeMigration,
  69. CmdMakeFactory,
  70. CmdMakeSeeder,
  71. CmdMakePolicy,
  72. )
  73. }
  74. // makeModelFromString 通过用户传入的字符串,生成模型数据
  75. func makeModelFromString(name string) Model {
  76. model := Model{}
  77. model.StructName = str.Singular(str.Camel(name))
  78. model.StructNamePlural = str.Plural(model.StructName)
  79. model.TableName = str.Snake(model.StructNamePlural)
  80. model.VariableName = str.LowerCamel(model.StructName)
  81. model.PackageName = str.Snake(model.StructName)
  82. model.VariableNamePlural = str.LowerCamel(model.StructNamePlural)
  83. return model
  84. }
  85. // createFileFromTpl 读取 tpl 文件并进行变量替换
  86. // 最后个选项可选参数,允许用户传递变量替换
  87. func createFileFromTpl(filePath, tplName string, model Model, vars ...map[string]string) {
  88. replaces := make(map[string]string)
  89. if len(vars) > 0 {
  90. for _, _var := range vars {
  91. for k, v := range _var {
  92. replaces[k] = v
  93. }
  94. }
  95. }
  96. // 文件已经存在
  97. if file.Exists(filePath) {
  98. console.Exit(filePath + " already exists")
  99. }
  100. // 读取 stub 文件内容
  101. stub, err := tplsFS.ReadFile("tpls/" + tplName + ".tpl")
  102. if err != nil {
  103. console.Exit(err.Error())
  104. }
  105. modelStub := string(stub)
  106. // 替换变量,默认的变量
  107. replaces["{{TableName}}"] = model.TableName
  108. replaces["{{StructName}}"] = model.StructName
  109. replaces["{{StructNamePlural}}"] = model.StructNamePlural
  110. replaces["{{VariableName}}"] = model.VariableName
  111. replaces["{{VariableNamePlural}}"] = model.VariableNamePlural
  112. replaces["{{PackageName}}"] = model.PackageName
  113. // 替换变量
  114. for search, replace := range replaces {
  115. modelStub = str.ReplaceAll(modelStub, search, replace)
  116. }
  117. // 存储到目标文件中
  118. err = file.Put([]byte(modelStub), filePath)
  119. if err != nil {
  120. console.Exit(err.Error())
  121. }
  122. console.Success(fmt.Sprintf("[%s] created.", filePath))
  123. }