packages.tex 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. \section{MAD: Build and Packaging Tool}
  2. \subsection{History}
  3. We came to conclusion that no matter how perfect your libraries are,
  4. the comfort and ease come mostly from development tools.
  5. Everything got started when \footahref{https://github.com/proger}{Vladimir~Kirillov} decided to
  6. replace Rusty's sync beam reloader. As you know sync uses
  7. filesystem polling which is neither energy-efficient nor elegant. Also
  8. sync is only able to recompile separate modules, while
  9. common use-case in N2O is to recompile DTL templates
  10. and LESS/SCSS stylesheets. That is why we need to recompile
  11. the whole project. That's the story how \footahref{https://github.com/synrc/active}{active} emerged.
  12. Under the hood active is a client subscriber
  13. of \footahref{https://github.com/synrc/fs}{fs} library, native filesystem listener for Linux, Windows and Mac.
  14. \paragraph{}
  15. De-facto standard in Erlang world is rebar.
  16. We love rebar interface despite its implementation.
  17. First we plugged rebar into active and then decided to drop its support,
  18. it was slow, especially in cold recompilation.
  19. Rebar was designed to be a stand-alone tool, so it has some
  20. glitches while using as embedded library.
  21. Later we switched to Makefile-based build tool \footahref{https://github.com/synrc/otp.mk}{otp.mk}.
  22. \paragraph{}
  23. The idea to build rebar replacement was up in the air for a long time.
  24. The best minimal approach was picked up by \footahref{https://github.com/s1n4}{Sina~Samavati},
  25. who implemented the first prototype called 'mad'. Initially mad
  26. was able to compile DTL templates, YECC files, escript (like
  27. bundled in gproc), and it also had support for caching with side-effects.
  28. \vspace{1\baselineskip}
  29. \begin{lstlisting}[caption=Example of building N2O sample]
  30. Cold Hot
  31. rebar get-deps compile 53.156s 4.714s
  32. mad deps compile 54.097s 0.899s
  33. \end{lstlisting}
  34. \vspace{1\baselineskip}
  35. \vspace{1\baselineskip}
  36. \begin{lstlisting}[caption=Example of building Cowboy]
  37. Hot
  38. make (erlang.mk) 2.588s
  39. mad compile 2.521s
  40. \end{lstlisting}
  41. \vspace{1\baselineskip}
  42. \subsection{Introduction}
  43. We were trying to make something minimalistic that fits out \footahref{https://github.com/synrc}{Web Stack}.
  44. Besides we wanted to use our knowledge of other build tools like lein, sbt etc.
  45. Also for sure we tried sinan, ebt, Makefile-based scripts.
  46. Synrc mad has a simple interface as follows:
  47. \vspace{1\baselineskip}
  48. \begin{lstlisting}
  49. BNF:
  50. invoke := mad params
  51. params := [] | run params
  52. run := command [ options ]
  53. command := app | lib | deps | compile | bundle
  54. start | stop | repl
  55. \end{lstlisting}
  56. \vspace{1\baselineskip}
  57. It seems to us more natural, you can specify random
  58. command sets with different specifiers (options).
  59. \subsection{Single-File Bundling}
  60. The key feature of mad is ability to create single-file bundled web sites.
  61. Thus making dream to boot simpler than Node.js come true.
  62. This target escript is ready for run on Windows, Linux and Mac.
  63. \paragraph{}
  64. To make this possible we implemented a zip filesytem inside escript.
  65. mad packages priv directories along with ebin and configs.
  66. You can redefine each file in zip fs inside target
  67. escript by creating the copy with the same path locally near escript.
  68. After launch all files are copied to ETS.
  69. N2O also comes with custom cowboy static handler that is able to
  70. read static files from this cached ETS filesystem.
  71. Also bundles are compatible with active online realoading and recompilation.
  72. \subsection{Templates}
  73. mad also comes with N2O templates. So you can bootstrap an N2O-based site
  74. just having a single copy of mad binary.
  75. \vspace{1\baselineskip}
  76. \begin{lstlisting}
  77. # mad app sample
  78. # cd sample
  79. # mad deps compile plan bundle sample
  80. \end{lstlisting}
  81. \vspace{1\baselineskip}
  82. After that you can just run escript web\_app under Windows, Linux and
  83. Mac and open \footahref{http://localhost:8000}{http://localhost:8000}.
  84. \vspace{1\baselineskip}
  85. \begin{lstlisting}
  86. C:\> escript sample
  87. Applications: [kernel,stdlib,crypto,cowlib,ranch,cowboy,compiler,
  88. syntax_tools,erlydtl,gproc,xmerl,n2o,sample,
  89. fs,active,mad,sh]
  90. Configuration: [{n2o,[{port,8000},{route,routes}]},
  91. {kvs,[{dba,store_mnesia},
  92. {schema,[kvs_user,kvs_acl,kvs_feed,
  93. kvs_subscription]}]}]
  94. Erlang/OTP 17 [erts-6.0] [64-bit] [smp:4:4] [async-threads:10]
  95. Eshell V6.0 (abort with ^G)
  96. 1>
  97. \end{lstlisting}
  98. \vspace{1\baselineskip}
  99. \subsection{Deploy}
  100. mad is also supposed to be a deploy tool with ability to
  101. deploy not only to our resources like Erlang on Xen, Voxoz (LXC/Xen) but
  102. also to Heroku and others.
  103. \subsection{OTP Compliant}
  104. mad supports rebar umbrella project structure.
  105. Specifically two kinds of directory layouts:
  106. \begin{lstlisting}[caption=Solution]
  107. apps
  108. deps
  109. rebar.config
  110. sys.config
  111. \end{lstlisting}
  112. \begin{lstlisting}[caption=OTP Application]
  113. deps
  114. ebin
  115. include
  116. priv
  117. src
  118. rebar.config
  119. \end{lstlisting}
  120. \subsection{Apps Ordering}
  121. As you may know, you can create OTP releases with
  122. reltool (rebar generate) or systools (relx). mad currently
  123. creates releases with relx but is going to do it independently soon.
  124. Now it can only order applications.
  125. \begin{lstlisting}
  126. # mad plan
  127. Ordered: [kernel,stdlib,mnesia,kvs,crypto,cowlib,ranch,
  128. cowboy,compiler,syntax_tools,erlydtl,gproc,
  129. xmerl,n2o,n2o_sample,fs,active,mad,rest,sh]
  130. \end{lstlisting}
  131. And the good part about mad is it's size:
  132. \begin{lstlisting}
  133. Sources Binary
  134. mad 567 LOC 39 KB
  135. rebar 7717 LOC 181 KB
  136. \end{lstlisting}