| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package make
- import (
- "embed"
- "fmt"
- "github.com/spf13/cobra"
- "github.com/runningwater/gohub/pkg/console"
- "github.com/runningwater/gohub/pkg/file"
- "github.com/runningwater/gohub/pkg/str"
- )
- // Model 参数解释
- // 单个词,用户命令传参,以 User 模型为例:
- // - user
- // - User
- // - users
- // - Users
- //
- // 整理好的数据:
- //
- // {
- // "TableName": "users",
- // "StructName": "User",
- // "StructNamePlural": "Users"
- // "VariableName": "user",
- // "VariableNamePlural": "users",
- // "PackageName": "user"
- // }
- //
- // -
- // 两个词或者以上,用户命令传参,以 TopicComment 模型为例:
- // - topic_comment
- // - topic_comments
- // - TopicComment
- // - TopicComments
- //
- // 整理好的数据:
- //
- // {
- // "TableName": "topic_comments",
- // "StructName": "TopicComment",
- // "StructNamePlural": "TopicComments"
- // "VariableName": "topicComment",
- // "VariableNamePlural": "topicComments",
- // "PackageName": "topic_comment"
- // }
- type Model struct {
- TableName string
- StructName string
- StructNamePlural string
- VariableName string
- VariableNamePlural string
- PackageName string
- }
- // tplsFS 方便我们后面打包的 .tpl 为后缀的文件
- //
- //go:embed tpls
- var tplsFS embed.FS
- var CmdMake = &cobra.Command{
- Use: "make",
- Short: "Generate code and file",
- }
- func init() {
- // 注册 make 子命令
- CmdMake.AddCommand(
- CmdMakeCMD,
- CmdMakeModel,
- CmdMakeAPIController,
- CmdMakeRequest,
- CmdMakeMigration,
- CmdMakeFactory,
- CmdMakeSeeder,
- CmdMakePolicy,
- )
- }
- // makeModelFromString 通过用户传入的字符串,生成模型数据
- func makeModelFromString(name string) Model {
- model := Model{}
- model.StructName = str.Singular(str.Camel(name))
- model.StructNamePlural = str.Plural(model.StructName)
- model.TableName = str.Snake(model.StructNamePlural)
- model.VariableName = str.LowerCamel(model.StructName)
- model.PackageName = str.Snake(model.StructName)
- model.VariableNamePlural = str.LowerCamel(model.StructNamePlural)
- return model
- }
- // createFileFromTpl 读取 tpl 文件并进行变量替换
- // 最后个选项可选参数,允许用户传递变量替换
- func createFileFromTpl(filePath, tplName string, model Model, vars ...map[string]string) {
- replaces := make(map[string]string)
- if len(vars) > 0 {
- for _, _var := range vars {
- for k, v := range _var {
- replaces[k] = v
- }
- }
- }
- // 文件已经存在
- if file.Exists(filePath) {
- console.Exit(filePath + " already exists")
- }
- // 读取 stub 文件内容
- stub, err := tplsFS.ReadFile("tpls/" + tplName + ".tpl")
- if err != nil {
- console.Exit(err.Error())
- }
- modelStub := string(stub)
- // 替换变量,默认的变量
- replaces["{{TableName}}"] = model.TableName
- replaces["{{StructName}}"] = model.StructName
- replaces["{{StructNamePlural}}"] = model.StructNamePlural
- replaces["{{VariableName}}"] = model.VariableName
- replaces["{{VariableNamePlural}}"] = model.VariableNamePlural
- replaces["{{PackageName}}"] = model.PackageName
- // 替换变量
- for search, replace := range replaces {
- modelStub = str.ReplaceAll(modelStub, search, replace)
- }
- // 存储到目标文件中
- err = file.Put([]byte(modelStub), filePath)
- if err != nil {
- console.Exit(err.Error())
- }
- console.Success(fmt.Sprintf("[%s] created.", filePath))
- }
|