bootstrap.mk 11 KB

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