bootstrap.mk 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. # Copyright (c) 2014-2015, 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. {id, "git"},
  19. {modules, []},
  20. {registered, []},
  21. {applications, [
  22. kernel,
  23. stdlib
  24. ]},
  25. {mod, {$(PROJECT)_app, []}},
  26. {env, []}
  27. ]}.
  28. endef
  29. define bs_appsrc_lib
  30. {application, $(PROJECT), [
  31. {description, ""},
  32. {vsn, "0.1.0"},
  33. {id, "git"},
  34. {modules, []},
  35. {registered, []},
  36. {applications, [
  37. kernel,
  38. stdlib
  39. ]}
  40. ]}.
  41. endef
  42. define bs_Makefile
  43. PROJECT = $(PROJECT)
  44. include erlang.mk
  45. endef
  46. define bs_app
  47. -module($(PROJECT)_app).
  48. -behaviour(application).
  49. -export([start/2]).
  50. -export([stop/1]).
  51. start(_Type, _Args) ->
  52. $(PROJECT)_sup:start_link().
  53. stop(_State) ->
  54. ok.
  55. endef
  56. define bs_relx_config
  57. {release, {$(PROJECT)_release, "1"}, [$(PROJECT)]}.
  58. {extended_start_script, true}.
  59. {sys_config, "rel/sys.config"}.
  60. {vm_args, "rel/vm.args"}.
  61. endef
  62. define bs_sys_config
  63. [
  64. ].
  65. endef
  66. define bs_vm_args
  67. -name $(PROJECT)@127.0.0.1
  68. -setcookie $(PROJECT)
  69. -heart
  70. endef
  71. # Normal templates.
  72. define tpl_supervisor
  73. -module($(n)).
  74. -behaviour(supervisor).
  75. -export([start_link/0]).
  76. -export([init/1]).
  77. start_link() ->
  78. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  79. init([]) ->
  80. Procs = [],
  81. {ok, {{one_for_one, 1, 5}, Procs}}.
  82. endef
  83. define tpl_gen_server
  84. -module($(n)).
  85. -behaviour(gen_server).
  86. %% API.
  87. -export([start_link/0]).
  88. %% gen_server.
  89. -export([init/1]).
  90. -export([handle_call/3]).
  91. -export([handle_cast/2]).
  92. -export([handle_info/2]).
  93. -export([terminate/2]).
  94. -export([code_change/3]).
  95. -record(state, {
  96. }).
  97. %% API.
  98. -spec start_link() -> {ok, pid()}.
  99. start_link() ->
  100. gen_server:start_link(?MODULE, [], []).
  101. %% gen_server.
  102. init([]) ->
  103. {ok, #state{}}.
  104. handle_call(_Request, _From, State) ->
  105. {reply, ignored, State}.
  106. handle_cast(_Msg, State) ->
  107. {noreply, State}.
  108. handle_info(_Info, State) ->
  109. {noreply, State}.
  110. terminate(_Reason, _State) ->
  111. ok.
  112. code_change(_OldVsn, State, _Extra) ->
  113. {ok, State}.
  114. endef
  115. define tpl_cowboy_http
  116. -module($(n)).
  117. -behaviour(cowboy_http_handler).
  118. -export([init/3]).
  119. -export([handle/2]).
  120. -export([terminate/3]).
  121. -record(state, {
  122. }).
  123. init(_, Req, _Opts) ->
  124. {ok, Req, #state{}}.
  125. handle(Req, State=#state{}) ->
  126. {ok, Req2} = cowboy_req:reply(200, Req),
  127. {ok, Req2, State}.
  128. terminate(_Reason, _Req, _State) ->
  129. ok.
  130. endef
  131. define tpl_gen_fsm
  132. -module($(n)).
  133. -behaviour(gen_fsm).
  134. %% API.
  135. -export([start_link/0]).
  136. %% gen_fsm.
  137. -export([init/1]).
  138. -export([state_name/2]).
  139. -export([handle_event/3]).
  140. -export([state_name/3]).
  141. -export([handle_sync_event/4]).
  142. -export([handle_info/3]).
  143. -export([terminate/3]).
  144. -export([code_change/4]).
  145. -record(state, {
  146. }).
  147. %% API.
  148. -spec start_link() -> {ok, pid()}.
  149. start_link() ->
  150. gen_fsm:start_link(?MODULE, [], []).
  151. %% gen_fsm.
  152. init([]) ->
  153. {ok, state_name, #state{}}.
  154. state_name(_Event, StateData) ->
  155. {next_state, state_name, StateData}.
  156. handle_event(_Event, StateName, StateData) ->
  157. {next_state, StateName, StateData}.
  158. state_name(_Event, _From, StateData) ->
  159. {reply, ignored, state_name, StateData}.
  160. handle_sync_event(_Event, _From, StateName, StateData) ->
  161. {reply, ignored, StateName, StateData}.
  162. handle_info(_Info, StateName, StateData) ->
  163. {next_state, StateName, StateData}.
  164. terminate(_Reason, _StateName, _StateData) ->
  165. ok.
  166. code_change(_OldVsn, StateName, StateData, _Extra) ->
  167. {ok, StateName, StateData}.
  168. endef
  169. define tpl_cowboy_loop
  170. -module($(n)).
  171. -behaviour(cowboy_loop_handler).
  172. -export([init/3]).
  173. -export([info/3]).
  174. -export([terminate/3]).
  175. -record(state, {
  176. }).
  177. init(_, Req, _Opts) ->
  178. {loop, Req, #state{}, 5000, hibernate}.
  179. info(_Info, Req, State) ->
  180. {loop, Req, State, hibernate}.
  181. terminate(_Reason, _Req, _State) ->
  182. ok.
  183. endef
  184. define tpl_cowboy_rest
  185. -module($(n)).
  186. -export([init/3]).
  187. -export([content_types_provided/2]).
  188. -export([get_html/2]).
  189. init(_, _Req, _Opts) ->
  190. {upgrade, protocol, cowboy_rest}.
  191. content_types_provided(Req, State) ->
  192. {[{{<<"text">>, <<"html">>, '*'}, get_html}], Req, State}.
  193. get_html(Req, State) ->
  194. {<<"<html><body>This is REST!</body></html>">>, Req, State}.
  195. endef
  196. define tpl_cowboy_ws
  197. -module($(n)).
  198. -behaviour(cowboy_websocket_handler).
  199. -export([init/3]).
  200. -export([websocket_init/3]).
  201. -export([websocket_handle/3]).
  202. -export([websocket_info/3]).
  203. -export([websocket_terminate/3]).
  204. -record(state, {
  205. }).
  206. init(_, _, _) ->
  207. {upgrade, protocol, cowboy_websocket}.
  208. websocket_init(_, Req, _Opts) ->
  209. Req2 = cowboy_req:compact(Req),
  210. {ok, Req2, #state{}}.
  211. websocket_handle({text, Data}, Req, State) ->
  212. {reply, {text, Data}, Req, State};
  213. websocket_handle({binary, Data}, Req, State) ->
  214. {reply, {binary, Data}, Req, State};
  215. websocket_handle(_Frame, Req, State) ->
  216. {ok, Req, State}.
  217. websocket_info(_Info, Req, State) ->
  218. {ok, Req, State}.
  219. websocket_terminate(_Reason, _Req, _State) ->
  220. ok.
  221. endef
  222. define tpl_ranch_protocol
  223. -module($(n)).
  224. -behaviour(ranch_protocol).
  225. -export([start_link/4]).
  226. -export([init/4]).
  227. -type opts() :: [].
  228. -export_type([opts/0]).
  229. -record(state, {
  230. socket :: inet:socket(),
  231. transport :: module()
  232. }).
  233. start_link(Ref, Socket, Transport, Opts) ->
  234. Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
  235. {ok, Pid}.
  236. -spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
  237. init(Ref, Socket, Transport, _Opts) ->
  238. ok = ranch:accept_ack(Ref),
  239. loop(#state{socket=Socket, transport=Transport}).
  240. loop(State) ->
  241. loop(State).
  242. endef
  243. # Plugin-specific targets.
  244. define render_template
  245. @echo "$${$(1)}" > $(2)
  246. endef
  247. $(foreach template,$(filter bs_%,$(.VARIABLES)),$(eval export $(template)))
  248. $(foreach template,$(filter tpl_%,$(.VARIABLES)),$(eval export $(template)))
  249. bootstrap:
  250. ifneq ($(wildcard src/),)
  251. $(error Error: src/ directory already exists)
  252. endif
  253. $(call render_template,bs_Makefile,Makefile)
  254. @mkdir src/
  255. $(call render_template,bs_appsrc,src/$(PROJECT).app.src)
  256. $(call render_template,bs_app,src/$(PROJECT)_app.erl)
  257. $(eval n := $(PROJECT)_sup)
  258. $(call render_template,tpl_supervisor,src/$(PROJECT)_sup.erl)
  259. bootstrap-lib:
  260. ifneq ($(wildcard src/),)
  261. $(error Error: src/ directory already exists)
  262. endif
  263. $(call render_template,bs_Makefile,Makefile)
  264. @mkdir src/
  265. $(call render_template,bs_appsrc_lib,src/$(PROJECT).app.src)
  266. bootstrap-rel:
  267. ifneq ($(wildcard relx.config),)
  268. $(error Error: relx.config already exists)
  269. endif
  270. ifneq ($(wildcard rel/),)
  271. $(error Error: rel/ directory already exists)
  272. endif
  273. $(call render_template,bs_relx_config,relx.config)
  274. @mkdir rel/
  275. $(call render_template,bs_sys_config,rel/sys.config)
  276. $(call render_template,bs_vm_args,rel/vm.args)
  277. new:
  278. ifeq ($(wildcard src/),)
  279. $(error Error: src/ directory does not exist)
  280. endif
  281. ifndef t
  282. $(error Usage: make new t=TEMPLATE n=NAME)
  283. endif
  284. ifndef tpl_$(t)
  285. $(error Unknown template)
  286. endif
  287. ifndef n
  288. $(error Usage: make new t=TEMPLATE n=NAME)
  289. endif
  290. $(call render_template,tpl_$(t),src/$(n).erl)
  291. list-templates:
  292. @echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))