No F*cking Idea

Common answer to everything

Rebar -> Swiss Army Knife

| Comments

Intro

Rebar is a great command line tool for building your Erlang apps. It was developed by guys from basho http://basho.com/. If you want to build Erlang app or module you can skip a lot of config / boilerplate code but using rebar.

Wait what? How do i get it ?

To obtain rebar all you have to do is clone source using

1
λ git clone git://github.com/basho/rebar.git

after obtaining source go into directory and bootstrap it.

1
2
λ cd rebar
λ ./bootstrap

This will build rebar script if everything is successful. Last thing i suggest is adding this directory to your path.

1
λ export PATH=/path/to/rebar:$PATH

So you will be able to use it like other command lines tools from “global namespace”. Now you should have working rebar installation. Just to test that everything is ok you can run rebar like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
λ rebar
No command to run specified!
Usage: rebar [-h] [-c] [-v <verbose>] [-V] [-f] [-D <defines>] [-j <jobs>] [-C <config>] [-p] [var=value,...] <command,...>

  -h, --help      Show the program options
  -c, --commands  Show available commands
  -v, --verbose       Verbosity level (-v, -vv, -vvv, --verbose 3). Default: 0
  -V, --version       Show version information
  -f, --force     Force
  -D          Define compiler macro
  -j, --jobs      Number of concurrent workers a command may use. Default: 3
  -C, --config        Rebar config file to use
  -p, --profile       Profile this run of rebar
  var=value        rebar global variables (e.g. force=1)
  command        Command to run (e.g. compile)

How do i use it @_@ ?

Two most important things you can generate using rebar are applications and nodes. To generate application you just need to create app directory and run rebar create-app command like this.

1
2
3
4
5
6
7
mkdir furby
λ cd furby
λ rebar create-app appid=furby
==> furby (create-app)
Writing src/furby.app.src
Writing src/furby_app.erl
Writing src/furby_sup.erl

This has created application scaffold with ready to go supervisor. This is ready to go! to compile it just run rebar compile

Me gusta

This is all fine but that don’t eliminate a lot, sweet things are behind the corner :).

eunit

Rebar enables you to use easy eunit testing framework within your code. Like we did it here http://no-fucking-idea.com/blog/2012/03/23/using-eredis-in-erlang/. To do it just run rebar compile eunit .

exmaple_output
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
λ rebar compile eunit
zsh: correct 'eunit' to '.eunit' [nyae]? n
==> eredis (compile)
==> example_db (compile)
==> eredis (eunit)

=ERROR REPORT==== 29-Mar-2012::19:13:21 ===
** Generic server <0.259.0> terminating
** Last message in was {tcp,#Port<0.4549>,
                            <<"*3\r\n$7\r\nmessage\r\n$3\r\nfoo\r\n$2\r\n12\r\n">>}
** When Server state == {state,"127.0.0.1",6379,<<>>,100,#Port<0.4549>,
                               {pstate,undefined,undefined},
                               [<<"foo">>],
                               {#Ref<0.0.0.3990>,<0.176.0>},
                               {[{message,<<"foo">>,<<"11">>,<0.259.0>},
                                 {message,<<"foo">>,<<"10">>,<0.259.0>},
                                 {message,<<"foo">>,<<"9">>,<0.259.0>},
                                 {message,<<"foo">>,<<"8">>,<0.259.0>},
                                 {message,<<"foo">>,<<"7">>,<0.259.0>},
                                 {message,<<"foo">>,<<"6">>,<0.259.0>},
                                 {message,<<"foo">>,<<"5">>,<0.259.0>},
                                 {message,<<"foo">>,<<"4">>,<0.259.0>},
                                 {message,<<"foo">>,<<"3">>,<0.259.0>}],
                                [{message,<<"foo">>,<<"2">>,<0.259.0>}]},
                               10,exit,need_ack}
** Reason for termination ==
** max_queue_size
  All 53 tests passed.
Cover analysis: /private/tmp/example_db/deps/eredis/.eunit/index.html
==> example_db (eunit)
  All 3 tests passed.
Cover analysis: /private/tmp/example_db/.eunit/index.html

coverage

Also if you will fiddle with rebar.config and set some variables like this:

1
2
3
4
5
λ cat rebar.config
%%-*- mode: erlang -*-

{erl_opts, []}.
{cover_enabled, true}.

you can get test coverage generated in .eunit folder. but this is just the beginning. lets look at it.

example_db.COVER.html
1
2
3
4
5
6
7
8
9
10
11
12
example_db/.eunit λ cat example_db.COVER.html
<html>
<head><title>.eunit/example_db.COVER.html</title></head><body bgcolor=white text=black>
<pre>
File generated from /private/tmp/example_db/.eunit/example_db.erl by COVER 2012-03-29 at 19:13:21

****************************************************************************

        |  -module(example_db).
        |  -behaviour(gen_server).
        |
...(and more)

dependencies

Last thing i want to mention is dependencies, i love this feature from rebar. You can add dependencies and rebar will do all the magic for you :). just open your rebar.config and add thme like this:

rebar.config
1
2
3
4
5
6
7
8
9
10
%%-*- mode: erlang -*-

{erl_opts, []}.
{cover_enabled, true}.

{deps,
  [
    {eredis, ".*", {git, "https://github.com/wooga/eredis.git", "HEAD"}}
  ]
}.

Whenever you will type rebar get-deps he will download all dependencies and install them into deps directory. This makes developing applications using things like mochiweb really easy!

Summary

I love this tool, it makes learning and development in Erlang much easier and more rewarding experience. I hope this help you a bit :). Cheers!

Comments