@@ -21,13 +21,6 @@ jzero template init --branch gateway
2121goctl template init --home .template/go-zero
2222```
2323
24- ## 构建属于自己的模版
25-
26- ``` shell
27- # 将当前项目构建为模版,并保存到 $HOME/.jzero/templates/local 下
28- jzero template build --name template_name
29- ```
30-
3124## 使用自定义模版初始化项目
3225
3326* 指定远程仓库模板
@@ -48,4 +41,88 @@ jzero new project_name --local template_name
4841
4942``` shell
5043jzero new project_name --home path_to_template
44+ ```
45+
46+ ## 实战: 构建属于自己的模版
47+
48+ :::tip 可以将当前任意项目转换成 jzero 模板, 这非常 cool!
49+ :::
50+
51+ ``` bash
52+ # 新增一个 api 项目
53+ jzero new simpleapi
54+ # 进入项目
55+ cd simpleapi
56+ # 新增一个 api
57+ jzero add api helloworld
58+ # 生成代码
59+ jzero gen
60+
61+ # 将当前项目构建为模版, 并保存到 $HOME/.jzero/templates/local/myapi 下
62+ jzero template build --name myapi
63+
64+ # 此时就可以使用你自己构建的模板了, 你会发现生成的项目自动拥有了 helloworld api 了.
65+ jzero new mysimpleapi --local myapi
66+
67+ # 但是你发现该模板仅允许本地使用, 为了达到通用的效果
68+ # 你可以在远程仓库如 github 创建一个 templates 仓库(假设为 https://github.com/jzero-io/templates)
69+ # 然后将 $HOME/.jzero/templates/local/myapi 下的内容放到仓库中, 并上传到 myapi 分支
70+ jzero new project_name --remote https://github.com/jzero-io/templates --branch myapi
71+ ```
72+
73+ 模板结构如下:
74+
75+ ``` bash
76+ $ tree ~ /.jzero/templates/local/myapi
77+ └── app
78+ ├── Dockerfile.tpl
79+ ├── README.md.tpl
80+ ├── cmd
81+ │ ├── root.go.tpl
82+ │ ├── server.go.tpl
83+ │ └── version.go.tpl
84+ ├── desc
85+ │ ├── api
86+ │ │ ├── helloworld.api.tpl
87+ │ │ └── version.api.tpl
88+ │ └── swagger
89+ │ ├── helloworld.swagger.json.tpl
90+ │ ├── swagger.json.tpl
91+ │ └── version.swagger.json.tpl
92+ ├── etc
93+ │ └── etc.yaml.tpl
94+ ├── go.mod.tpl
95+ ├── internal
96+ │ ├── config
97+ │ │ └── config.go.tpl
98+ │ ├── custom
99+ │ │ └── custom.go.tpl
100+ │ ├── handler
101+ │ │ ├── helloworld
102+ │ │ │ └── helloworld_compact.go.tpl
103+ │ │ ├── routes.go.tpl
104+ │ │ └── version
105+ │ │ └── version.go.tpl
106+ │ ├── logic
107+ │ │ ├── helloworld
108+ │ │ │ └── create.go.tpl
109+ │ │ └── version
110+ │ │ └── version.go.tpl
111+ │ ├── middleware
112+ │ │ ├── middleware.go.tpl
113+ │ │ ├── response.go.tpl
114+ │ │ └── validator.go.tpl
115+ │ ├── svc
116+ │ │ ├── config.go.tpl
117+ │ │ ├── middleware.go.tpl
118+ │ │ └── servicecontext.go.tpl
119+ │ └── types
120+ │ ├── helloworld
121+ │ │ └── types.go.tpl
122+ │ ├── types.go.tpl
123+ │ └── version
124+ │ └── types.go.tpl
125+ ├── main.go.tpl
126+ └── plugins
127+ └── plugins.go.tpl
51128```
0 commit comments