bootstrap.mk 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. # 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.0.1
  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.0.1
  60. endef
  61. endif
  62. define bs_apps_Makefile
  63. PROJECT = $p
  64. PROJECT_DESCRIPTION = New project
  65. PROJECT_VERSION = 0.0.1
  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]}.
  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_cowboy_http
  138. -module($(n)).
  139. -behaviour(cowboy_http_handler).
  140. -export([init/3]).
  141. -export([handle/2]).
  142. -export([terminate/3]).
  143. -record(state, {
  144. }).
  145. init(_, Req, _Opts) ->
  146. {ok, Req, #state{}}.
  147. handle(Req, State=#state{}) ->
  148. {ok, Req2} = cowboy_req:reply(200, Req),
  149. {ok, Req2, State}.
  150. terminate(_Reason, _Req, _State) ->
  151. ok.
  152. endef
  153. define tpl_gen_fsm
  154. -module($(n)).
  155. -behaviour(gen_fsm).
  156. %% API.
  157. -export([start_link/0]).
  158. %% gen_fsm.
  159. -export([init/1]).
  160. -export([state_name/2]).
  161. -export([handle_event/3]).
  162. -export([state_name/3]).
  163. -export([handle_sync_event/4]).
  164. -export([handle_info/3]).
  165. -export([terminate/3]).
  166. -export([code_change/4]).
  167. -record(state, {
  168. }).
  169. %% API.
  170. -spec start_link() -> {ok, pid()}.
  171. start_link() ->
  172. gen_fsm:start_link(?MODULE, [], []).
  173. %% gen_fsm.
  174. init([]) ->
  175. {ok, state_name, #state{}}.
  176. state_name(_Event, StateData) ->
  177. {next_state, state_name, StateData}.
  178. handle_event(_Event, StateName, StateData) ->
  179. {next_state, StateName, StateData}.
  180. state_name(_Event, _From, StateData) ->
  181. {reply, ignored, state_name, StateData}.
  182. handle_sync_event(_Event, _From, StateName, StateData) ->
  183. {reply, ignored, StateName, StateData}.
  184. handle_info(_Info, StateName, StateData) ->
  185. {next_state, StateName, StateData}.
  186. terminate(_Reason, _StateName, _StateData) ->
  187. ok.
  188. code_change(_OldVsn, StateName, StateData, _Extra) ->
  189. {ok, StateName, StateData}.
  190. endef
  191. define tpl_cowboy_loop
  192. -module($(n)).
  193. -behaviour(cowboy_loop_handler).
  194. -export([init/3]).
  195. -export([info/3]).
  196. -export([terminate/3]).
  197. -record(state, {
  198. }).
  199. init(_, Req, _Opts) ->
  200. {loop, Req, #state{}, 5000, hibernate}.
  201. info(_Info, Req, State) ->
  202. {loop, Req, State, hibernate}.
  203. terminate(_Reason, _Req, _State) ->
  204. ok.
  205. endef
  206. define tpl_cowboy_rest
  207. -module($(n)).
  208. -export([init/3]).
  209. -export([content_types_provided/2]).
  210. -export([get_html/2]).
  211. init(_, _Req, _Opts) ->
  212. {upgrade, protocol, cowboy_rest}.
  213. content_types_provided(Req, State) ->
  214. {[{{<<"text">>, <<"html">>, '*'}, get_html}], Req, State}.
  215. get_html(Req, State) ->
  216. {<<"<html><body>This is REST!</body></html>">>, Req, State}.
  217. endef
  218. define tpl_cowboy_ws
  219. -module($(n)).
  220. -behaviour(cowboy_websocket_handler).
  221. -export([init/3]).
  222. -export([websocket_init/3]).
  223. -export([websocket_handle/3]).
  224. -export([websocket_info/3]).
  225. -export([websocket_terminate/3]).
  226. -record(state, {
  227. }).
  228. init(_, _, _) ->
  229. {upgrade, protocol, cowboy_websocket}.
  230. websocket_init(_, Req, _Opts) ->
  231. Req2 = cowboy_req:compact(Req),
  232. {ok, Req2, #state{}}.
  233. websocket_handle({text, Data}, Req, State) ->
  234. {reply, {text, Data}, Req, State};
  235. websocket_handle({binary, Data}, Req, State) ->
  236. {reply, {binary, Data}, Req, State};
  237. websocket_handle(_Frame, Req, State) ->
  238. {ok, Req, State}.
  239. websocket_info(_Info, Req, State) ->
  240. {ok, Req, State}.
  241. websocket_terminate(_Reason, _Req, _State) ->
  242. ok.
  243. endef
  244. define tpl_ranch_protocol
  245. -module($(n)).
  246. -behaviour(ranch_protocol).
  247. -export([start_link/4]).
  248. -export([init/4]).
  249. -type opts() :: [].
  250. -export_type([opts/0]).
  251. -record(state, {
  252. socket :: inet:socket(),
  253. transport :: module()
  254. }).
  255. start_link(Ref, Socket, Transport, Opts) ->
  256. Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
  257. {ok, Pid}.
  258. -spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
  259. init(Ref, Socket, Transport, _Opts) ->
  260. ok = ranch:accept_ack(Ref),
  261. loop(#state{socket=Socket, transport=Transport}).
  262. loop(State) ->
  263. loop(State).
  264. endef
  265. # Plugin-specific targets.
  266. define render_template
  267. $(verbose) printf -- '$(subst $(newline),\n,$(subst %,%%,$(subst ','\'',$(subst $(tab),$(WS),$(call $(1))))))\n' > $(2)
  268. endef
  269. ifndef WS
  270. ifdef SP
  271. 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))
  272. else
  273. WS = $(tab)
  274. endif
  275. endif
  276. bootstrap:
  277. ifneq ($(wildcard src/),)
  278. $(error Error: src/ directory already exists)
  279. endif
  280. $(eval p := $(PROJECT))
  281. $(eval n := $(PROJECT)_sup)
  282. $(call render_template,bs_Makefile,Makefile)
  283. $(verbose) echo "include erlang.mk" >> Makefile
  284. $(verbose) mkdir src/
  285. ifdef LEGACY
  286. $(call render_template,bs_appsrc,src/$(PROJECT).app.src)
  287. endif
  288. $(call render_template,bs_app,src/$(PROJECT)_app.erl)
  289. $(call render_template,tpl_supervisor,src/$(PROJECT)_sup.erl)
  290. bootstrap-lib:
  291. ifneq ($(wildcard src/),)
  292. $(error Error: src/ directory already exists)
  293. endif
  294. $(eval p := $(PROJECT))
  295. $(call render_template,bs_Makefile,Makefile)
  296. $(verbose) echo "include erlang.mk" >> Makefile
  297. $(verbose) mkdir src/
  298. ifdef LEGACY
  299. $(call render_template,bs_appsrc_lib,src/$(PROJECT).app.src)
  300. endif
  301. bootstrap-rel:
  302. ifneq ($(wildcard relx.config),)
  303. $(error Error: relx.config already exists)
  304. endif
  305. ifneq ($(wildcard rel/),)
  306. $(error Error: rel/ directory already exists)
  307. endif
  308. $(eval p := $(PROJECT))
  309. $(call render_template,bs_relx_config,relx.config)
  310. $(verbose) mkdir rel/
  311. $(call render_template,bs_sys_config,rel/sys.config)
  312. $(call render_template,bs_vm_args,rel/vm.args)
  313. new-app:
  314. ifndef in
  315. $(error Usage: $(MAKE) new-app in=APP)
  316. endif
  317. ifneq ($(wildcard $(APPS_DIR)/$in),)
  318. $(error Error: Application $in already exists)
  319. endif
  320. $(eval p := $(in))
  321. $(eval n := $(in)_sup)
  322. $(verbose) mkdir -p $(APPS_DIR)/$p/src/
  323. $(call render_template,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile)
  324. ifdef LEGACY
  325. $(call render_template,bs_appsrc,$(APPS_DIR)/$p/src/$p.app.src)
  326. endif
  327. $(call render_template,bs_app,$(APPS_DIR)/$p/src/$p_app.erl)
  328. $(call render_template,tpl_supervisor,$(APPS_DIR)/$p/src/$p_sup.erl)
  329. new-lib:
  330. ifndef in
  331. $(error Usage: $(MAKE) new-lib in=APP)
  332. endif
  333. ifneq ($(wildcard $(APPS_DIR)/$in),)
  334. $(error Error: Application $in already exists)
  335. endif
  336. $(eval p := $(in))
  337. $(verbose) mkdir -p $(APPS_DIR)/$p/src/
  338. $(call render_template,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile)
  339. ifdef LEGACY
  340. $(call render_template,bs_appsrc_lib,$(APPS_DIR)/$p/src/$p.app.src)
  341. endif
  342. new:
  343. ifeq ($(wildcard src/)$(in),)
  344. $(error Error: src/ directory does not exist)
  345. endif
  346. ifndef t
  347. $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP])
  348. endif
  349. ifndef tpl_$(t)
  350. $(error Unknown template)
  351. endif
  352. ifndef n
  353. $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP])
  354. endif
  355. ifdef in
  356. $(verbose) $(MAKE) -C $(APPS_DIR)/$(in)/ new t=$t n=$n in=
  357. else
  358. $(call render_template,tpl_$(t),src/$(n).erl)
  359. endif
  360. list-templates:
  361. $(verbose) echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))