Parcourir la source

refactor(func): 重命令方法

runningwater il y a 7 mois
Parent
commit
ab535c3ca7

+ 1 - 0
README.md

@@ -64,6 +64,7 @@ Web 服务功能会封装到子命令 serve 中,命令行功能会封装到子
 
 #### 🚜 代码重构
 
+- *(func)* 重命令方法
 - *(doc)* Readme updated
 - 修改生成模板文件后缀名
 - Gofmt -l -w -s package

+ 5 - 5
app/cmd/make/make.go

@@ -54,10 +54,10 @@ type Model struct {
 	PackageName        string
 }
 
-// stubsFS 方便我们后面打包的 .stub 为后缀的文件
+// tplsFS 方便我们后面打包的 .stub 为后缀的文件
 //
 //go:embed tpls
-var stubsFS embed.FS
+var tplsFS embed.FS
 
 var CmdMake = &cobra.Command{
 	Use:   "make",
@@ -88,9 +88,9 @@ func makeModelFromString(name string) Model {
 
 }
 
-// createFileFromStub 读取 stub 文件并进行变量替换
+// createFileFromTpl 读取 tpl 文件并进行变量替换
 // 最后个选项可选参数,允许用户传递变量替换
-func createFileFromStub(filePath, tplName string, model Model, vars ...map[string]string) {
+func createFileFromTpl(filePath, tplName string, model Model, vars ...map[string]string) {
 
 	replaces := make(map[string]string)
 	if len(vars) > 0 {
@@ -107,7 +107,7 @@ func createFileFromStub(filePath, tplName string, model Model, vars ...map[strin
 	}
 
 	// 读取 stub 文件内容
-	stub, err := stubsFS.ReadFile("tpls/" + tplName + ".tpl")
+	stub, err := tplsFS.ReadFile("tpls/" + tplName + ".tpl")
 	if err != nil {
 		console.Exit(err.Error())
 	}

+ 1 - 1
app/cmd/make/make_apicontroller.go

@@ -32,5 +32,5 @@ func runMakeAPIController(cmd *cobra.Command, args []string) {
 	// 如:app/http/controllers/api/v1/user_controller.go
 	filePath := fmt.Sprintf("app/http/controllers/api/%s/%s_controller.go", apiVersion, model.TableName)
 	// 4. 基于模板生成文件
-	createFileFromStub(filePath, "apicontroller", model)
+	createFileFromTpl(filePath, "apicontroller", model)
 }

+ 1 - 1
app/cmd/make/make_cmd.go

@@ -23,7 +23,7 @@ func runMakeCMD(cmd *cobra.Command, args []string) {
 	filePath := fmt.Sprintf("app/cmd/%s.go", model.PackageName)
 
 	// 从模板中创建文件
-	createFileFromStub(filePath, "cmd", model)
+	createFileFromTpl(filePath, "cmd", model)
 
 	// 输出成功信息
 	console.Success(fmt.Sprintf("Create %s success", filePath))

+ 3 - 3
app/cmd/make/make_model.go

@@ -23,7 +23,7 @@ func runMakeModel(cmd *cobra.Command, args []string) {
 	os.MkdirAll(dir, os.ModePerm)
 
 	// 替换变量
-	createFileFromStub(dir+model.PackageName+"_model.go", "model/model", model)
-	createFileFromStub(dir+model.PackageName+"_util.go", "model/model_util", model)
-	createFileFromStub(dir+model.PackageName+"_hooks.go", "model/model_hooks", model)
+	createFileFromTpl(dir+model.PackageName+"_model.go", "model/model", model)
+	createFileFromTpl(dir+model.PackageName+"_util.go", "model/model_util", model)
+	createFileFromTpl(dir+model.PackageName+"_hooks.go", "model/model_hooks", model)
 }

+ 1 - 1
app/cmd/make/make_request.go

@@ -17,5 +17,5 @@ func runMakeRequest(cmd *cobra.Command, args []string) {
 
 	model := makeModelFromString(args[0])
 	filePath := fmt.Sprintf("app/requests/%s_request.go", model.PackageName)
-	createFileFromStub(filePath, "request", model)
+	createFileFromTpl(filePath, "request", model)
 }