First question when i need to add new blog to my code I ask is, do i remember all the boilerplate and how many times i will make a mistake this time. No more :). Rebar has a nice thing built in it is templating language that lets us build our own templates.
Custom template ?
I always end up looking at old projects and copying parts like gen_server
and reusing them. I always knew rebar has option to write them but never had time to look at it. Today lol, i wanted to do some cleaning at home so every thing seems to be a good excuse to not do any cleaning :D.
What i need to know
Basic template is made from one or many .erl
files written with mustache
style { { } }
code and .template
file describing what to do with files.
Let’s build gen_server template
So my first file will be gen_server.erl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
This is the template, i know a bit long i tried to cut out all comments, euint etc and narrow it to minimum. I posted it to show how much you can save :). Now you will need the transformation file
. All this things in { { something } } will be replaced by things we will type in command line or defaults from template file.
1 2 3 4 5 6 7 |
|
For me it looks like this, we have few default definitions and at the bottom. template! This is important part it says which file he has to copy where and what will be the name of new file. Now everything should be clear!
Injet it into rebar!
All you need now to do is symlink your template folders to ~/.rebar/templates
and you can use them!
(you can symlink your folder or just create one there :) )
Use!
1 2 3 |
|
Viola!
important
When i was looking at this post i saw that { { is converted in a wrong way by octopress so i added spaces between them! check repo for correct code!
My own templates
Today i started adding my own templates initially i have only gen_server
and webmachine_resource
but i will add more :). It is fun it is like building your own anti-boiler plate framework.
My templates repo: https://github.com/JakubOboza/rebar-templates
Hope this helps! Cheers!