bootstrap.mk 8.1 KB

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