bootstrap.mk 6.2 KB

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