\section{MAD: Erlang Build and Deploy Tool}

\subsection{History}

We came to conclusion that no matter how perfect your libraries are,
the comfort and ease come mostly from developing tools.
Everything got started when \footahref{https://github.com/proger}{Vladimir~Kirillov} decided to
replace Rusty's sync beam reloader. As you know sync uses
filesystem polling which is neither energy-efficient nor elegant. Also
sync is only able to recompile separate modules while
common use-case in N2O is to recompile DTL templates
and LESS/SCSS stylesheets. That is why we need to recompile
the whole project. That's the story how \footahref{https://github.com/synrc/active}{active} emerged.
Under the hood active is a client subscriber
of \footahref{https://github.com/synrc/fs}{fs} library, native filesystem listener for Linux, Windows and Mac.

De-facto standard in Erlang world is rebar.
We love rebar interface despite its implementation.
First we plugged rebar into active and then decided to drop its support,
it was slow, especially in cold recompilation.
It was designed to be a stand-alone tool, so it has some
glitches while using as embedded library.
Later we switched to Makefile-based build tool \footahref{https://github.com/synrc/otp.mk}{otp.mk}.

The idea to build rebar replacement was up in the air for a long time.
The best minimal approach was picked up by \footahref{https://github.com/s1n4}{Sina~Samavati},
who implemented the first prototype called 'mad'. Initially mad
was able to compile DTL templated, YECC files, escript (like
bundled in gproc), also it had support for caching with side-effects.
In a month I forked mad and took over the development under the same name.

\vspace{1\baselineskip}
\begin{lstlisting}[caption=Example of building N2O sample]
                                   Cold       Hot
    rebar get-deps compile         53.156s    4.714s
    mad deps compile               54.097s    0.899s
\end{lstlisting}
\vspace{1\baselineskip}

\vspace{1\baselineskip}
\begin{lstlisting}[caption=Example of building Cowboy]
                                   Hot
    make (erlang.mk)               2.588s
    mad compile                    2.521s
\end{lstlisting}
\vspace{1\baselineskip}

\subsection{Introduction}

We were trying to make something minimalistic that fits out \footahref{https://github.com/synrc}{Web Stack}.
Besides we wanted to use our knowledge of other build tools like lein, sbt etc.
Also for sure we tried sinan, ebt, Makefile-based scripts.

Synrc mad has a simple interface as follows:

\vspace{1\baselineskip}
\begin{lstlisting}
  BNF:
      invoke := mad params
      params := [] | run params
         run := command [ options ]
     command := app | lib | deps | compile | bundle
                start | stop | repl
\end{lstlisting}
\vspace{1\baselineskip}

It seems to us more natural, you can specify random
commands set with different specifiers (options).

\subsection{Several Types of Packaging}

The key feature of mad is ability to create single-file bundled web sites.
Thus making dream to boot simpler than node.js come true.
This target escript is ready to run on Windows, Linux and Mac.

\subsection{Deployment Options}

mad is also supposed to be a deploy tool with ability to
deploy not only to our resources like Erlang on Xen, Voxoz (LXC/Xen) but
also to Heroku and others.

\subsection{OTP Compliant}

mad supports rebar umbrella project structure.
Specifically two kinds of directory layouts:

\subsection{Fast Apps Ordering}

As you may know you can create OTP releases with
reltool (rebar generate) or systools (relx). mad currently
creates releases with relx but is going to do it independently soon.
Now it can only order applications.

\subsection{Tiny Size}

And the good part:

\vspace{1\baselineskip}
\begin{lstlisting}
                      Sources        Binary
    mad               567 LOC        39 KB
    rebar             7717 LOC       181 KB
\end{lstlisting}
\vspace{1\baselineskip}