bootstrap.mk 10 KB

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