在开发过程中rebar自带模板建立项目,或多或少不能满足自己的开发需求。本人又是那种懒人,所以就要想办法偷懒。查看了priv模板 打造适合自己的项目模板。下面我简单的介绍整个模板的打造过程。
准备过程
1.创建对应的template 目录
2.ctrl+h 查看当前用户目录下是否有一个 .rebar 的文件目录 或者 shell 查看
thinkpad@thinkpad:~$ ll -ps | grep .rebar
4 drwxr-xr-x 10 thinkpad thinkpad 4096 6月 24 22:18 rebar/
4 drwxrwxr-x 3 thinkpad thinkpad 4096 6月 24 22:22 .rebar/
文中 . rebar 就是rebar 存放自定义template 文件的地方
3 .在.reabr 文件中有一个template的文件夹,如果没有没有请创建一个
4.进入template文件夹中创建一个 simple_game 文件夹 和一个simple_game.template的文件
到此为止,创建rebar tempalte 的准备工作我们就做完了。
模板文功能
1.创建一个完成的otp项目
2.开发启动,调试脚本
3.代码更新,编译后自动重新加载。方便调试 这也是erlang热更新的一个特性
4.发布项目,打包镜像
制作模板文件
1.准备模板文件 下面是我准备好的模板文件
1 -rw-rw-r-- 1 thinkpad thinkpad 350 7月 3 12:01 app.config
2 -rw-rw-r-- 1 thinkpad thinkpad 1120 12月 5 2013 erl.script
3 -rw-rw-r-- 1 thinkpad thinkpad 371 7月 3 11:43 game_app.erl
4 -rw-rw-r-- 1 thinkpad thinkpad 230 7月 3 11:41 game.app.src
5 -rw-rw-r-- 1 thinkpad thinkpad 1564 7月 3 11:43 game.erl
6 -rw-rw-r-- 1 thinkpad thinkpad 1439 7月 3 11:43 game_server.erl
7 -rw-rw-r-- 1 thinkpad thinkpad 773 7月 3 11:44 game_sup.erl
8 -rw-rw-r-- 1 thinkpad thinkpad 63 12月 5 2013 gitignore
9 -rw-rw-r-- 1 thinkpad thinkpad 271 12月 5 2013 Makefile
10 -rw-rw-r-- 1 thinkpad thinkpad 4819 12月 5 2013 nodetool
11 -rw-rw-r-- 1 thinkpad thinkpad 249 6月 16 11:11 README.md
12 -rw-rw-r-- 1 thinkpad thinkpad 830 7月 3 11:51 rebar.config
13 -rw-rw-r-- 1 thinkpad thinkpad 4829 6月 14 18:57 reloader.erl
14 -rw-rw-r-- 1 thinkpad thinkpad 1185 6月 30 16:33 reltool.config
15 -rwxrwxr-x 1 thinkpad thinkpad 4371 12月 5 2013 runner*
16 -rwxrw-r-- 1 thinkpad thinkpad 142 7月 3 11:55 start-dev.sh*
17 -rw-rw-r-- 1 thinkpad thinkpad 431 12月 5 2013 vm.args
View Code
2. 提供rebar调用的文件模板
1 %% -*- erlang -*-
2
3 %%
4 %% project template
5 %%
6 %% Sets up boilerplate for a distributed erlang application that
7 %% supports hot upgrades.
8 %%
9 %% Example:
10 %%
11 %% rebar create template=project projectid=superfly
12 %%
13 % App Files
14 {variables, [{projectid, "myproj"}]}.
15
16 %readme
17 {template, "simple_game/README.md", "{{projectid}}/README.md"}.
18 % Build Files
19 {template, "simple_game/Makefile", "{{projectid}}/Makefile"}.
20 {template, "simple_game/gitignore", "{{projectid}}/.gitignore"}.
21 {template, "simple_game/rebar.config", "{{projectid}}/rebar.config"}.
22 {template, "simple_game/start-dev.sh", "{{projectid}}/start-dev.sh"}.
23 {chmod, 8#744, "{{projectid}}/start-dev.sh"}.
24
25 % App Files
26 {template, "simple_game/game.app.src", "{{projectid}}/apps/{{projectid}}/src/{{projectid}}.app.src"}.
27 {template, "simple_game/reloader.erl", "{{projectid}}/apps/{{projectid}}/src/reloader.erl"}.
28 {template, "simple_game/game.erl", "{{projectid}}/apps/{{projectid}}/src/{{projectid}}.erl"}.
29 {template, "simple_game/game_app.erl", "{{projectid}}/apps/{{projectid}}/src/{{projectid}}_app.erl"}.
30 {template, "simple_game/game_sup.erl", "{{projectid}}/apps/{{projectid}}/src/{{projectid}}_sup.erl"}.
31 {template, "simple_game/game_server.erl", "{{projectid}}/apps/{{projectid}}/src/{{projectid}}_server.erl"}.
32
33
34 % Release files
35 {template, "simple_game/README.md", "{{projectid}}/deps/README.md"}.
36 {template, "simple_game/vm.args", "{{projectid}}/rel/files/vm.args"}.
37 {template, "simple_game/reltool.config", "{{projectid}}/rel/reltool.config"}.
38 {template, "simple_game/app.config", "{{ projectid}}/rel/files/app.config"}.
39
40 {file, "simple_game/erl.script", "{{projectid}}/rel/files/erl"}.
41 {chmod, 8#744, "{{projectid}}/rel/files/erl"}.
42
43 {file, "simple_game/nodetool", "{{projectid}}/rel/files/nodetool"}.
44 {chmod, 8#744, "{{projectid}}/rel/files/nodetool"}.
45
46 {file, "simple_game/runner", "{{projectid}}/rel/files/{{projectid}}"}.
47 {chmod, 8#744, "{{projectid}}/rel/files/{{projectid}}"}.
View Code
3.模板的主要制作过程是替换对已文件我恩需要替换的参数。
在rebar文件模板中我们可以看到 这一行 {variables, [{projectid, "myproj"}]}. projectid 就是在rebar中我们传入的参数。在准备好的文件中 我们需要替换或者插入我们参数的地方 使用 {{projectid}} rebar 就会给予替换为我们传入的参数。
4. 模板文件说明
app.config 整个系统启动配置参数,可覆盖app.src 参数
erl.script erlang 发布后启动脚本
game_app.erl application
game.app.src app.src
game.erl 调试启动文件 不用每次 application:start(game).
game_server.erl server
game_sup.erl sup
gitignore
Makefile make 模板
nodetool
README.md
rebar.config rebar对应配置文件
reloader.erl 开发中重新加载编译代码文件来自mochiweb
reltool.config 发布打包构建项目配置
runner*
start-dev.sh* 开发启动文件
vm.args erl erlang 启动参数文件
View Code
创建发布项目
1 .创建项目 rebar create template=simple_game projectid=game_demo
thinkpad@thinkpad:~/demo$ rebar create template=simple_game projectid=game_demo
==> demo (create)
Writing game_demo/README.md
Writing game_demo/Makefile
Writing game_demo/.gitignore
Writing game_demo/rebar.config
Writing game_demo/start-dev.sh
Writing game_demo/apps/game_demo/src/game_demo.app.src
Writing game_demo/apps/game_demo/src/reloader.erl
Writing game_demo/apps/game_demo/src/game_demo.erl
Writing game_demo/apps/game_demo/src/game_demo_app.erl
Writing game_demo/apps/game_demo/src/game_demo_sup.erl
Writing game_demo/apps/game_demo/src/game_demo_server.erl
Writing game_demo/deps/README.md
Writing game_demo/rel/files/vm.args
Writing game_demo/rel/reltool.config
Writing game_demo/rel/files/app.config
Writing game_demo/rel/files/erl
Writing game_demo/rel/files/nodetool
Writing game_demo/rel/files/game_demo
View Code
2 . 发布项目 make
thinkpad@thinkpad:~/demo/game_demo$ make
rebar clean
==> game_demo (clean)
rebar compile
==> game_demo (compile)
Compiled src/game_demo.erl
Compiled src/game_demo_sup.erl
Compiled src/game_demo_server.erl
Compiled src/game_demo_app.erl
Compiled src/reloader.erl
==> rel (compile)
==> game_demo (compile)
rebar generate -f
==> rel (generate)
View Code
3 . 调试发布项目 make console
thinkpad@thinkpad:~/demo/game_demo$ make console
rebar compile
==> game_demo (compile)
==> rel (compile)
==> game_demo (compile)
rebar generate -f
==> rel (generate)
WARN: 'generate' command does not apply to directory /home/thinkpad/demo/game_demo
rel/game_demo/bin/game_demo console
Exec: /home/thinkpad/demo/game_demo/rel/game_demo/erts-6.1/bin/erlexec -boot /home/thinkpad/demo/game_demo/rel/game_demo/releases/0.1.0/game_demo -embedded -config /home/thinkpad/demo/game_demo/rel/game_demo/etc/app.config -args_file /home/thinkpad/demo/game_demo/rel/game_demo/etc/vm.args -- console
Root: /home/thinkpad/demo/game_demo/rel/game_demo
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:4:4] [async-threads:5] [hipe] [kernel-poll:true]
Eshell V6.1 (abort with ^G)
(game_demo@127.0.0.1)1>
View Code
4. 开发项目 ./start-dev.sh
thinkpad@thinkpad:~/demo/game_demo$ ./start-dev.sh
==> game_demo (clean)
==> game_demo (compile)
==> rel (compile)
==> game_demo (compile)
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
View Code
制作好的文件在这 猛点下载 放入 .rebar template 下即可