No F*cking Idea

Common answer to everything

Rebar Templates

| Comments

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

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
%%% @author  { {author_name } } <{ {author_email} }>
%%% @copyright  { {copyright_year } }  { {author_name} }.
%%% @doc  { {description } }

-module( { {name } }).
-behaviour(gen_server).

-author(' { {author_name } } <{ {author_email } }>').

-export([start_link/1]).
-export([init/1, handle_call/3, handle_cast/2, terminate/2, handle_info/2, code_change/3, stop/1]).

% public api

start_link(_Args) ->
  gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

% state should be change with State that you will pass
init([]) ->
  {ok, state}.

stop(_Pid) ->
  stop().

stop() ->
  gen_server:cast(?MODULE, stop).

handle_call({method_name_and_params}, _From, State) ->
  Response = ok,
  {reply, Response, State};

handle_call(_Message, _From, State) ->
  {reply, error, State}.

handle_cast(_Message, State) -> {noreply, State}.
handle_info(_Message, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVersion, State, _Extra) -> {ok, State}.

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.

gen_server.template
1
2
3
4
5
6
7
{variables, [
  {name, "template"},
  {copyright_year, "2012"},
  {author_name, "jakub Oboza"},
  {author_email, "jakub.oboza@gmail.com"}
]}.
{template, "gen_server.erl", "src/{ {name} }.erl"}.

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
λ rebar create template=gen_server name=example
==> tmp (create)
Writing src/example.erl

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!

Comments