bootstrap.mk 6.9 KB

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