bootstrap.mk 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. $(verbose) 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-app n=NAME Create a new local OTP application NAME" \
  12. " new-lib n=NAME Create a new local OTP library NAME" \
  13. " new t=TPL n=NAME Generate a module NAME based on the template TPL" \
  14. " new t=T n=N in=APP Generate a module NAME based on the template TPL in APP" \
  15. " list-templates List available templates"
  16. # Bootstrap templates.
  17. define bs_appsrc
  18. {application, $p, [
  19. {description, ""},
  20. {vsn, "0.1.0"},
  21. {id, "git"},
  22. {modules, []},
  23. {registered, []},
  24. {applications, [
  25. kernel,
  26. stdlib
  27. ]},
  28. {mod, {$p_app, []}},
  29. {env, []}
  30. ]}.
  31. endef
  32. define bs_appsrc_lib
  33. {application, $p, [
  34. {description, ""},
  35. {vsn, "0.1.0"},
  36. {id, "git"},
  37. {modules, []},
  38. {registered, []},
  39. {applications, [
  40. kernel,
  41. stdlib
  42. ]}
  43. ]}.
  44. endef
  45. ifdef SP
  46. define bs_Makefile
  47. PROJECT = $p
  48. PROJECT_DESCRIPTION = New project
  49. PROJECT_VERSION = 0.0.1
  50. # Whitespace to be used when creating files from templates.
  51. SP = $(SP)
  52. include erlang.mk
  53. endef
  54. else
  55. define bs_Makefile
  56. PROJECT = $p
  57. include erlang.mk
  58. endef
  59. endif
  60. define bs_apps_Makefile
  61. PROJECT = $p
  62. include $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(APPS_DIR)/app)/erlang.mk
  63. endef
  64. define bs_app
  65. -module($p_app).
  66. -behaviour(application).
  67. -export([start/2]).
  68. -export([stop/1]).
  69. start(_Type, _Args) ->
  70. $p_sup:start_link().
  71. stop(_State) ->
  72. ok.
  73. endef
  74. define bs_relx_config
  75. {release, {$p_release, "1"}, [$p]}.
  76. {extended_start_script, true}.
  77. {sys_config, "rel/sys.config"}.
  78. {vm_args, "rel/vm.args"}.
  79. endef
  80. define bs_sys_config
  81. [
  82. ].
  83. endef
  84. define bs_vm_args
  85. -name $p@127.0.0.1
  86. -setcookie $p
  87. -heart
  88. endef
  89. # Normal templates.
  90. define tpl_supervisor
  91. -module($(n)).
  92. -behaviour(supervisor).
  93. -export([start_link/0]).
  94. -export([init/1]).
  95. start_link() ->
  96. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  97. init([]) ->
  98. Procs = [],
  99. {ok, {{one_for_one, 1, 5}, Procs}}.
  100. endef
  101. define tpl_gen_server
  102. -module($(n)).
  103. -behaviour(gen_server).
  104. %% API.
  105. -export([start_link/0]).
  106. %% gen_server.
  107. -export([init/1]).
  108. -export([handle_call/3]).
  109. -export([handle_cast/2]).
  110. -export([handle_info/2]).
  111. -export([terminate/2]).
  112. -export([code_change/3]).
  113. -record(state, {
  114. }).
  115. %% API.
  116. -spec start_link() -> {ok, pid()}.
  117. start_link() ->
  118. gen_server:start_link(?MODULE, [], []).
  119. %% gen_server.
  120. init([]) ->
  121. {ok, #state{}}.
  122. handle_call(_Request, _From, State) ->
  123. {reply, ignored, State}.
  124. handle_cast(_Msg, State) ->
  125. {noreply, State}.
  126. handle_info(_Info, State) ->
  127. {noreply, State}.
  128. terminate(_Reason, _State) ->
  129. ok.
  130. code_change(_OldVsn, State, _Extra) ->
  131. {ok, State}.
  132. endef
  133. define tpl_cowboy_http
  134. -module($(n)).
  135. -behaviour(cowboy_http_handler).
  136. -export([init/3]).
  137. -export([handle/2]).
  138. -export([terminate/3]).
  139. -record(state, {
  140. }).
  141. init(_, Req, _Opts) ->
  142. {ok, Req, #state{}}.
  143. handle(Req, State=#state{}) ->
  144. {ok, Req2} = cowboy_req:reply(200, Req),
  145. {ok, Req2, State}.
  146. terminate(_Reason, _Req, _State) ->
  147. ok.
  148. endef
  149. define tpl_gen_fsm
  150. -module($(n)).
  151. -behaviour(gen_fsm).
  152. %% API.
  153. -export([start_link/0]).
  154. %% gen_fsm.
  155. -export([init/1]).
  156. -export([state_name/2]).
  157. -export([handle_event/3]).
  158. -export([state_name/3]).
  159. -export([handle_sync_event/4]).
  160. -export([handle_info/3]).
  161. -export([terminate/3]).
  162. -export([code_change/4]).
  163. -record(state, {
  164. }).
  165. %% API.
  166. -spec start_link() -> {ok, pid()}.
  167. start_link() ->
  168. gen_fsm:start_link(?MODULE, [], []).
  169. %% gen_fsm.
  170. init([]) ->
  171. {ok, state_name, #state{}}.
  172. state_name(_Event, StateData) ->
  173. {next_state, state_name, StateData}.
  174. handle_event(_Event, StateName, StateData) ->
  175. {next_state, StateName, StateData}.
  176. state_name(_Event, _From, StateData) ->
  177. {reply, ignored, state_name, StateData}.
  178. handle_sync_event(_Event, _From, StateName, StateData) ->
  179. {reply, ignored, StateName, StateData}.
  180. handle_info(_Info, StateName, StateData) ->
  181. {next_state, StateName, StateData}.
  182. terminate(_Reason, _StateName, _StateData) ->
  183. ok.
  184. code_change(_OldVsn, StateName, StateData, _Extra) ->
  185. {ok, StateName, StateData}.
  186. endef
  187. define tpl_cowboy_loop
  188. -module($(n)).
  189. -behaviour(cowboy_loop_handler).
  190. -export([init/3]).
  191. -export([info/3]).
  192. -export([terminate/3]).
  193. -record(state, {
  194. }).
  195. init(_, Req, _Opts) ->
  196. {loop, Req, #state{}, 5000, hibernate}.
  197. info(_Info, Req, State) ->
  198. {loop, Req, State, hibernate}.
  199. terminate(_Reason, _Req, _State) ->
  200. ok.
  201. endef
  202. define tpl_cowboy_rest
  203. -module($(n)).
  204. -export([init/3]).
  205. -export([content_types_provided/2]).
  206. -export([get_html/2]).
  207. init(_, _Req, _Opts) ->
  208. {upgrade, protocol, cowboy_rest}.
  209. content_types_provided(Req, State) ->
  210. {[{{<<"text">>, <<"html">>, '*'}, get_html}], Req, State}.
  211. get_html(Req, State) ->
  212. {<<"<html><body>This is REST!</body></html>">>, Req, State}.
  213. endef
  214. define tpl_cowboy_ws
  215. -module($(n)).
  216. -behaviour(cowboy_websocket_handler).
  217. -export([init/3]).
  218. -export([websocket_init/3]).
  219. -export([websocket_handle/3]).
  220. -export([websocket_info/3]).
  221. -export([websocket_terminate/3]).
  222. -record(state, {
  223. }).
  224. init(_, _, _) ->
  225. {upgrade, protocol, cowboy_websocket}.
  226. websocket_init(_, Req, _Opts) ->
  227. Req2 = cowboy_req:compact(Req),
  228. {ok, Req2, #state{}}.
  229. websocket_handle({text, Data}, Req, State) ->
  230. {reply, {text, Data}, Req, State};
  231. websocket_handle({binary, Data}, Req, State) ->
  232. {reply, {binary, Data}, Req, State};
  233. websocket_handle(_Frame, Req, State) ->
  234. {ok, Req, State}.
  235. websocket_info(_Info, Req, State) ->
  236. {ok, Req, State}.
  237. websocket_terminate(_Reason, _Req, _State) ->
  238. ok.
  239. endef
  240. define tpl_ranch_protocol
  241. -module($(n)).
  242. -behaviour(ranch_protocol).
  243. -export([start_link/4]).
  244. -export([init/4]).
  245. -type opts() :: [].
  246. -export_type([opts/0]).
  247. -record(state, {
  248. socket :: inet:socket(),
  249. transport :: module()
  250. }).
  251. start_link(Ref, Socket, Transport, Opts) ->
  252. Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
  253. {ok, Pid}.
  254. -spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
  255. init(Ref, Socket, Transport, _Opts) ->
  256. ok = ranch:accept_ack(Ref),
  257. loop(#state{socket=Socket, transport=Transport}).
  258. loop(State) ->
  259. loop(State).
  260. endef
  261. # Plugin-specific targets.
  262. define render_template
  263. $(verbose) echo "$${_$(1)}" > $(2)
  264. endef
  265. ifndef WS
  266. ifdef SP
  267. WS = $(subst a,,a $(wordlist 1,$(SP),a a a a a a a a a a a a a a a a a a a a))
  268. else
  269. WS = $(tab)
  270. endif
  271. endif
  272. $(foreach template,$(filter bs_% tpl_%,$(.VARIABLES)), \
  273. $(eval _$(template) = $$(subst $$(tab),$$(WS),$$($(template)))) \
  274. $(eval export _$(template)))
  275. bootstrap:
  276. ifneq ($(wildcard src/),)
  277. $(error Error: src/ directory already exists)
  278. endif
  279. $(eval p := $(PROJECT))
  280. $(eval n := $(PROJECT)_sup)
  281. $(call render_template,bs_Makefile,Makefile)
  282. $(verbose) mkdir src/
  283. ifdef LEGACY
  284. $(call render_template,bs_appsrc,src/$(PROJECT).app.src)
  285. endif
  286. $(call render_template,bs_app,src/$(PROJECT)_app.erl)
  287. $(call render_template,tpl_supervisor,src/$(PROJECT)_sup.erl)
  288. bootstrap-lib:
  289. ifneq ($(wildcard src/),)
  290. $(error Error: src/ directory already exists)
  291. endif
  292. $(eval p := $(PROJECT))
  293. $(call render_template,bs_Makefile,Makefile)
  294. $(verbose) mkdir src/
  295. ifdef LEGACY
  296. $(call render_template,bs_appsrc_lib,src/$(PROJECT).app.src)
  297. endif
  298. bootstrap-rel:
  299. ifneq ($(wildcard relx.config),)
  300. $(error Error: relx.config already exists)
  301. endif
  302. ifneq ($(wildcard rel/),)
  303. $(error Error: rel/ directory already exists)
  304. endif
  305. $(eval p := $(PROJECT))
  306. $(call render_template,bs_relx_config,relx.config)
  307. $(verbose) mkdir rel/
  308. $(call render_template,bs_sys_config,rel/sys.config)
  309. $(call render_template,bs_vm_args,rel/vm.args)
  310. new-app:
  311. ifndef in
  312. $(error Usage: $(MAKE) new-app in=APP)
  313. endif
  314. ifneq ($(wildcard $(APPS_DIR)/$in),)
  315. $(error Error: Application $in already exists)
  316. endif
  317. $(eval p := $(in))
  318. $(eval n := $(in)_sup)
  319. $(verbose) mkdir -p $(APPS_DIR)/$p/src/
  320. $(call render_template,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile)
  321. ifdef LEGACY
  322. $(call render_template,bs_appsrc,$(APPS_DIR)/$p/src/$p.app.src)
  323. endif
  324. $(call render_template,bs_app,$(APPS_DIR)/$p/src/$p_app.erl)
  325. $(call render_template,tpl_supervisor,$(APPS_DIR)/$p/src/$p_sup.erl)
  326. new-lib:
  327. ifndef in
  328. $(error Usage: $(MAKE) new-lib in=APP)
  329. endif
  330. ifneq ($(wildcard $(APPS_DIR)/$in),)
  331. $(error Error: Application $in already exists)
  332. endif
  333. $(eval p := $(in))
  334. $(verbose) mkdir -p $(APPS_DIR)/$p/src/
  335. $(call render_template,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile)
  336. ifdef LEGACY
  337. $(call render_template,bs_appsrc_lib,$(APPS_DIR)/$p/src/$p.app.src)
  338. endif
  339. new:
  340. ifeq ($(wildcard src/)$(in),)
  341. $(error Error: src/ directory does not exist)
  342. endif
  343. ifndef t
  344. $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP])
  345. endif
  346. ifndef tpl_$(t)
  347. $(error Unknown template)
  348. endif
  349. ifndef n
  350. $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP])
  351. endif
  352. ifdef in
  353. $(verbose) $(MAKE) -C $(APPS_DIR)/$(in)/ new t=$t n=$n in=
  354. else
  355. $(call render_template,tpl_$(t),src/$(n).erl)
  356. endif
  357. list-templates:
  358. $(verbose) echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))