bootstrap.mk 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # Copyright (c) 2014, 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([handle_event/3])." \
  124. "-export([handle_sync_event/4])." \
  125. "-export([handle_info/3])." \
  126. "-export([terminate/3])." \
  127. "-export([code_change/4])." \
  128. "" \
  129. "-record(state, {" \
  130. "})." \
  131. "" \
  132. "%% API." \
  133. "" \
  134. "-spec start_link() -> {ok, pid()}." \
  135. "start_link() ->" \
  136. " gen_fsm:start_link(?MODULE, [], [])." \
  137. "" \
  138. "%% gen_fsm." \
  139. "" \
  140. "init([]) ->" \
  141. " {ok, initial_state, \#state{}}." \
  142. "" \
  143. "handle_event(_Event, StateName, State) ->" \
  144. " {next_state, StateName, State}." \
  145. "" \
  146. "handle_sync_event(_Event, _From, StateName, State) ->" \
  147. " {reply, ignored, StateName, State}." \
  148. "" \
  149. "handle_info(_Info, StateName, State) ->" \
  150. " {next_state, StateName, State}." \
  151. "" \
  152. "terminate(_Reason, _StateName, _State) ->" \
  153. " ok." \
  154. "" \
  155. "code_change(_OldVsn, StateName, State, _Extra) ->" \
  156. " {ok, StateName, State}."
  157. tpl_cowboy_http = "-module($(n))." \
  158. "-behaviour(cowboy_http_handler)." \
  159. "" \
  160. "-export([init/3])." \
  161. "-export([handle/2])." \
  162. "-export([terminate/3])." \
  163. "" \
  164. "-record(state, {" \
  165. "})." \
  166. "" \
  167. "init(_, Req, _Opts) ->" \
  168. " {ok, Req, \#state{}}." \
  169. "" \
  170. "handle(Req, State=\#state{}) ->" \
  171. " {ok, Req2} = cowboy_req:reply(200, Req)," \
  172. " {ok, Req2, State}." \
  173. "" \
  174. "terminate(_Reason, _Req, _State) ->" \
  175. " ok."
  176. tpl_cowboy_loop = "-module($(n))." \
  177. "-behaviour(cowboy_loop_handler)." \
  178. "" \
  179. "-export([init/3])." \
  180. "-export([info/3])." \
  181. "-export([terminate/3])." \
  182. "" \
  183. "-record(state, {" \
  184. "})." \
  185. "" \
  186. "init(_, Req, _Opts) ->" \
  187. " {loop, Req, \#state{}, 5000, hibernate}." \
  188. "" \
  189. "info(_Info, Req, State) ->" \
  190. " {loop, Req, State, hibernate}." \
  191. "" \
  192. "terminate(_Reason, _Req, _State) ->" \
  193. " ok."
  194. tpl_cowboy_rest = "-module($(n))." \
  195. "" \
  196. "-export([init/3])." \
  197. "-export([content_types_provided/2])." \
  198. "-export([get_html/2])." \
  199. "" \
  200. "init(_, _Req, _Opts) ->" \
  201. " {upgrade, protocol, cowboy_rest}." \
  202. "" \
  203. "content_types_provided(Req, State) ->" \
  204. " {[{{<<\"text\">>, <<\"html\">>, '*'}, get_html}], Req, State}." \
  205. "" \
  206. "get_html(Req, State) ->" \
  207. " {<<\"<html><body>This is REST!</body></html>\">>, Req, State}."
  208. tpl_cowboy_ws = "-module($(n))." \
  209. "-behaviour(cowboy_websocket_handler)." \
  210. "" \
  211. "-export([init/3])." \
  212. "-export([websocket_init/3])." \
  213. "-export([websocket_handle/3])." \
  214. "-export([websocket_info/3])." \
  215. "-export([websocket_terminate/3])." \
  216. "" \
  217. "-record(state, {" \
  218. "})." \
  219. "" \
  220. "init(_, _, _) ->" \
  221. " {upgrade, protocol, cowboy_websocket}." \
  222. "" \
  223. "websocket_init(_, Req, _Opts) ->" \
  224. " Req2 = cowboy_req:compact(Req)," \
  225. " {ok, Req2, \#state{}}." \
  226. "" \
  227. "websocket_handle({text, Data}, Req, State) ->" \
  228. " {reply, {text, Data}, Req, State};" \
  229. "websocket_handle({binary, Data}, Req, State) ->" \
  230. " {reply, {binary, Data}, Req, State};" \
  231. "websocket_handle(_Frame, Req, State) ->" \
  232. " {ok, Req, State}." \
  233. "" \
  234. "websocket_info(_Info, Req, State) ->" \
  235. " {ok, Req, State}." \
  236. "" \
  237. "websocket_terminate(_Reason, _Req, _State) ->" \
  238. " ok."
  239. tpl_ranch_protocol = "-module($(n))." \
  240. "-behaviour(ranch_protocol)." \
  241. "" \
  242. "-export([start_link/4])." \
  243. "-export([init/4])." \
  244. "" \
  245. "-type opts() :: []." \
  246. "-export_type([opts/0])." \
  247. "" \
  248. "-record(state, {" \
  249. " socket :: inet:socket()," \
  250. " transport :: module()" \
  251. "})." \
  252. "" \
  253. "start_link(Ref, Socket, Transport, Opts) ->" \
  254. " Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts])," \
  255. " {ok, Pid}." \
  256. "" \
  257. "-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok." \
  258. "init(Ref, Socket, Transport, _Opts) ->" \
  259. " ok = ranch:accept_ack(Ref)," \
  260. " loop(\#state{socket=Socket, transport=Transport})." \
  261. "" \
  262. "loop(State) ->" \
  263. " loop(State)."
  264. # Plugin-specific targets.
  265. bootstrap:
  266. ifneq ($(wildcard src/),)
  267. $(error Error: src/ directory already exists)
  268. endif
  269. @printf "%s\n" $(bs_Makefile) > Makefile
  270. @mkdir src/
  271. @printf "%s\n" $(bs_appsrc) > src/$(PROJECT).app.src
  272. @printf "%s\n" $(bs_app) > src/$(PROJECT)_app.erl
  273. $(eval n := $(PROJECT)_sup)
  274. @printf "%s\n" $(tpl_supervisor) > src/$(PROJECT)_sup.erl
  275. bootstrap-lib:
  276. ifneq ($(wildcard src/),)
  277. $(error Error: src/ directory already exists)
  278. endif
  279. @printf "%s\n" $(bs_Makefile) > Makefile
  280. @mkdir src/
  281. @printf "%s\n" $(bs_appsrc_lib) > src/$(PROJECT).app.src
  282. bootstrap-rel:
  283. ifneq ($(wildcard relx.config),)
  284. $(error Error: relx.config already exists)
  285. endif
  286. ifneq ($(wildcard rel/),)
  287. $(error Error: rel/ directory already exists)
  288. endif
  289. @printf "%s\n" $(bs_relx_config) > relx.config
  290. @mkdir rel/
  291. @printf "%s\n" $(bs_sys_config) > rel/sys.config
  292. @printf "%s\n" $(bs_vm_args) > rel/vm.args
  293. new:
  294. ifeq ($(wildcard src/),)
  295. $(error Error: src/ directory does not exist)
  296. endif
  297. ifndef t
  298. $(error Usage: make new t=TEMPLATE n=NAME)
  299. endif
  300. ifndef tpl_$(t)
  301. $(error Unknown template)
  302. endif
  303. ifndef n
  304. $(error Usage: make new t=TEMPLATE n=NAME)
  305. endif
  306. @printf "%s\n" $(tpl_$(t)) > src/$(n).erl
  307. list-templates:
  308. @echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))