bootstrap.mk 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # Copyright (c) 2014, Loïc Hoguin <essen@ninenines.eu>
  2. # This file is part of erlang.mk and subject to the terms of the ISC License.
  3. .PHONY: bootstrap bootstrap-lib bootstrap-rel new list-templates
  4. # Core targets.
  5. help::
  6. @printf "%s\n" "" \
  7. "Bootstrap targets:" \
  8. " bootstrap Generate a skeleton of an OTP application" \
  9. " bootstrap-lib Generate a skeleton of an OTP library" \
  10. " bootstrap-rel Generate the files needed to build a release" \
  11. " new t=TPL n=NAME Generate a module NAME based on the template TPL" \
  12. " list-templates List available templates"
  13. # Bootstrap templates.
  14. define bs_appsrc
  15. {application, $(PROJECT), [
  16. {description, ""},
  17. {vsn, "0.1.0"},
  18. {modules, []},
  19. {registered, []},
  20. {applications, [
  21. kernel,
  22. stdlib
  23. ]},
  24. {mod, {$(PROJECT)_app, []}},
  25. {env, []}
  26. ]}.
  27. endef
  28. define bs_appsrc_lib
  29. {application, $(PROJECT), [
  30. {description, ""},
  31. {vsn, "0.1.0"},
  32. {modules, []},
  33. {registered, []},
  34. {applications, [
  35. kernel,
  36. stdlib
  37. ]}
  38. ]}.
  39. endef
  40. define bs_Makefile
  41. PROJECT = $(PROJECT)
  42. include erlang.mk
  43. endef
  44. define bs_app
  45. -module($(PROJECT)_app).
  46. -behaviour(application).
  47. -export([start/2]).
  48. -export([stop/1]).
  49. start(_Type, _Args) ->
  50. $(PROJECT)_sup:start_link().
  51. stop(_State) ->
  52. ok.
  53. endef
  54. define bs_relx_config
  55. {release, {$(PROJECT)_release, "1"}, [$(PROJECT)]}.
  56. {extended_start_script, true}.
  57. {sys_config, "rel/sys.config"}.
  58. {vm_args, "rel/vm.args"}.
  59. endef
  60. define bs_sys_config
  61. [
  62. ].
  63. endef
  64. define bs_vm_args
  65. -name $(PROJECT)@127.0.0.1
  66. -setcookie $(PROJECT)
  67. -heart
  68. endef
  69. # Normal templates.
  70. define tpl_supervisor
  71. -module($(n)).
  72. -behaviour(supervisor).
  73. -export([start_link/0]).
  74. -export([init/1]).
  75. start_link() ->
  76. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  77. init([]) ->
  78. Procs = [],
  79. {ok, {{one_for_one, 1, 5}, Procs}}.
  80. endef
  81. define tpl_gen_server
  82. -module($(n)).
  83. -behaviour(gen_server).
  84. %% API.
  85. -export([start_link/0]).
  86. %% gen_server.
  87. -export([init/1]).
  88. -export([handle_call/3]).
  89. -export([handle_cast/2]).
  90. -export([handle_info/2]).
  91. -export([terminate/2]).
  92. -export([code_change/3]).
  93. -record(state, {
  94. }).
  95. %% API.
  96. -spec start_link() -> {ok, pid()}.
  97. start_link() ->
  98. gen_server:start_link(?MODULE, [], []).
  99. %% gen_server.
  100. init([]) ->
  101. {ok, #state{}}.
  102. handle_call(_Request, _From, State) ->
  103. {reply, ignored, State}.
  104. handle_cast(_Msg, State) ->
  105. {noreply, State}.
  106. handle_info(_Info, State) ->
  107. {noreply, State}.
  108. terminate(_Reason, _State) ->
  109. ok.
  110. code_change(_OldVsn, State, _Extra) ->
  111. {ok, State}.
  112. endef
  113. define tpl_cowboy_http
  114. -module($(n)).
  115. -behaviour(cowboy_http_handler).
  116. -export([init/3]).
  117. -export([handle/2]).
  118. -export([terminate/3]).
  119. -record(state, {
  120. }).
  121. init(_, Req, _Opts) ->
  122. {ok, Req, #state{}}.
  123. handle(Req, State=#state{}) ->
  124. {ok, Req2} = cowboy_req:reply(200, Req),
  125. {ok, Req2, State}.
  126. terminate(_Reason, _Req, _State) ->
  127. ok.
  128. endef
  129. define tpl_cowboy_loop
  130. -module($(n)).
  131. -behaviour(cowboy_loop_handler).
  132. -export([init/3]).
  133. -export([info/3]).
  134. -export([terminate/3]).
  135. -record(state, {
  136. }).
  137. init(_, Req, _Opts) ->
  138. {loop, Req, #state{}, 5000, hibernate}.
  139. info(_Info, Req, State) ->
  140. {loop, Req, State, hibernate}.
  141. terminate(_Reason, _Req, _State) ->
  142. ok.
  143. endef
  144. define tpl_cowboy_rest
  145. -module($(n)).
  146. -export([init/3]).
  147. -export([content_types_provided/2]).
  148. -export([get_html/2]).
  149. init(_, _Req, _Opts) ->
  150. {upgrade, protocol, cowboy_rest}.
  151. content_types_provided(Req, State) ->
  152. {[{{<<"text">>, <<"html">>, '_'}, get_html}], Req, State}.
  153. get_html(Req, State) ->
  154. {<<"<html><body>This is REST!</body></html>">>, Req, State}.
  155. endef
  156. define tpl_cowboy_ws
  157. -module($(n)).
  158. -behaviour(cowboy_websocket_handler).
  159. -export([init/3]).
  160. -export([websocket_init/3]).
  161. -export([websocket_handle/3]).
  162. -export([websocket_info/3]).
  163. -export([websocket_terminate/3]).
  164. -record(state, {
  165. }).
  166. init(_, _, _) ->
  167. {upgrade, protocol, cowboy_websocket}.
  168. websocket_init(_, Req, _Opts) ->
  169. Req2 = cowboy_req:compact(Req),
  170. {ok, Req2, #state{}}.
  171. websocket_handle({text, Data}, Req, State) ->
  172. {reply, {text, Data}, Req, State};
  173. websocket_handle({binary, Data}, Req, State) ->
  174. {reply, {binary, Data}, Req, State};
  175. websocket_handle(_Frame, Req, State) ->
  176. {ok, Req, State}.
  177. websocket_info(_Info, Req, State) ->
  178. {ok, Req, State}.
  179. websocket_terminate(_Reason, _Req, _State) ->
  180. ok.
  181. endef
  182. define tpl_ranch_protocol
  183. -module($(n)).
  184. -behaviour(ranch_protocol).
  185. -export([start_link/4]).
  186. -export([init/4]).
  187. -type opts() :: [].
  188. -export_type([opts/0]).
  189. -record(state, {
  190. socket :: inet:socket(),
  191. transport :: module()
  192. }).
  193. start_link(Ref, Socket, Transport, Opts) ->
  194. Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
  195. {ok, Pid}.
  196. -spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
  197. init(Ref, Socket, Transport, _Opts) ->
  198. ok = ranch:accept_ack(Ref),
  199. loop(#state{socket=Socket, transport=Transport}).
  200. loop(State) ->
  201. loop(State).
  202. endef
  203. # Plugin-specific targets.
  204. define render_template
  205. @echo '$(subst $(newline),\n,${1})' > $(2)
  206. endef
  207. define newline
  208. endef
  209. bootstrap:
  210. ifneq ($(wildcard src/),)
  211. $(error Error: src/ directory already exists)
  212. endif
  213. $(call render_template,$(bs_Makefile),Makefile)
  214. @mkdir src/
  215. $(call render_template,$(bs_appsrc),src/$(PROJECT).app.src)
  216. $(call render_template,$(bs_app),src/$(PROJECT)_app.erl)
  217. $(eval n := $(PROJECT)_sup)
  218. $(call render_template,$(tpl_supervisor),src/$(PROJECT)_sup.erl)
  219. bootstrap-lib:
  220. ifneq ($(wildcard src/),)
  221. $(error Error: src/ directory already exists)
  222. endif
  223. $(call render_template,$(bs_Makefile),Makefile)
  224. @mkdir src/
  225. $(call render_template,$(bs_appsrc_lib),src/$(PROJECT).app.src)
  226. bootstrap-rel:
  227. ifneq ($(wildcard relx.config),)
  228. $(error Error: relx.config already exists)
  229. endif
  230. ifneq ($(wildcard rel/),)
  231. $(error Error: rel/ directory already exists)
  232. endif
  233. $(call render_template,$(bs_relx_config),relx.config)
  234. @mkdir rel/
  235. $(call render_template,$(bs_sys_config),rel/sys.config)
  236. $(call render_template,$(bs_vm_args),rel/vm.args)
  237. new:
  238. ifeq ($(wildcard src/),)
  239. $(error Error: src/ directory does not exist)
  240. endif
  241. ifndef t
  242. $(error Usage: make new t=TEMPLATE n=NAME)
  243. endif
  244. ifndef tpl_$(t)
  245. $(error Unknown template)
  246. endif
  247. ifndef n
  248. $(error Usage: make new t=TEMPLATE n=NAME)
  249. endif
  250. $(call render_template,$(tpl_$(t)),src/$(n).erl)
  251. list-templates:
  252. @echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))