make.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package make
  2. import (
  3. "embed"
  4. "fmt"
  5. "github.com/runningwater/gohub/pkg/console"
  6. "github.com/runningwater/gohub/pkg/file"
  7. "github.com/runningwater/gohub/pkg/str"
  8. "github.com/spf13/cobra"
  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 方便我们后面打包的 .stub 为后缀的文件
  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. )
  69. }
  70. // makeModelFromString 通过用户传入的字符串,生成模型数据
  71. func makeModelFromString(name string) Model {
  72. model := Model{}
  73. model.StructName = str.Singular(str.Camel(name))
  74. model.StructNamePlural = str.Plural(model.StructName)
  75. model.TableName = str.Snake(model.StructNamePlural)
  76. model.VariableName = str.LowerCamel(model.StructName)
  77. model.PackageName = str.Snake(model.StructName)
  78. model.VariableNamePlural = str.LowerCamel(model.StructNamePlural)
  79. return model
  80. }
  81. // createFileFromTpl 读取 tpl 文件并进行变量替换
  82. // 最后个选项可选参数,允许用户传递变量替换
  83. func createFileFromTpl(filePath, tplName string, model Model, vars ...map[string]string) {
  84. replaces := make(map[string]string)
  85. if len(vars) > 0 {
  86. for _, _var := range vars {
  87. for k, v := range _var {
  88. replaces[k] = v
  89. }
  90. }
  91. }
  92. // 文件已经存在
  93. if file.Exists(filePath) {
  94. console.Exit(filePath + " already exists")
  95. }
  96. // 读取 stub 文件内容
  97. stub, err := tplsFS.ReadFile("tpls/" + tplName + ".tpl")
  98. if err != nil {
  99. console.Exit(err.Error())
  100. }
  101. modelStub := string(stub)
  102. // 替换变量,默认的变量
  103. replaces["{{TableName}}"] = model.TableName
  104. replaces["{{StructName}}"] = model.StructName
  105. replaces["{{StructNamePlural}}"] = model.StructNamePlural
  106. replaces["{{VariableName}}"] = model.VariableName
  107. replaces["{{VariableNamePlural}}"] = model.VariableNamePlural
  108. replaces["{{PackageName}}"] = model.PackageName
  109. // 替换变量
  110. for search, replace := range replaces {
  111. modelStub = str.ReplaceAll(modelStub, search, replace)
  112. }
  113. // 存储到目标文件中
  114. err = file.Put([]byte(modelStub), filePath)
  115. if err != nil {
  116. console.Exit(err.Error())
  117. }
  118. console.Success(fmt.Sprintf("[%s] created.", filePath))
  119. }