bootstrap.mk 11 KB

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