Browse Source

Add gen_statem template

[skip ci]
juhlig 7 years ago
parent
commit
22b77abb90
3 changed files with 49 additions and 2 deletions
  1. 1 1
      doc/src/guide/getting_started.asciidoc
  2. 45 0
      plugins/bootstrap.mk
  3. 3 1
      test/plugin_bootstrap.mk

+ 1 - 1
doc/src/guide/getting_started.asciidoc

@@ -242,7 +242,7 @@ target:
 
 [source,bash]
 $ make list-templates
-Available templates: cowboy_http cowboy_loop cowboy_rest cowboy_ws gen_fsm gen_server ranch_protocol supervisor
+Available templates: cowboy_http cowboy_loop cowboy_rest cowboy_ws gen_fsm gen_server gen_statem ranch_protocol supervisor
 
 To generate a module, let's say a `gen_server`, all you need to
 do is to call `make new` with the appropriate arguments:

+ 45 - 0
plugins/bootstrap.mk

@@ -252,6 +252,51 @@ code_change(_OldVsn, StateName, StateData, _Extra) ->
 	{ok, StateName, StateData}.
 endef
 
+define tpl_gen_statem
+-module($(n)).
+-behaviour(gen_statem).
+
+%% API.
+-export([start_link/0]).
+
+%% gen_statem.
+-export([callback_mode/0]).
+-export([init/1]).
+-export([state_name/3]).
+-export([handle_event/4]).
+-export([terminate/3]).
+-export([code_change/4]).
+
+-record(state, {
+}).
+
+%% API.
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+	gen_statem:start_link(?MODULE, [], []).
+
+%% gen_statem.
+
+callback_mode() ->
+	state_functions.
+
+init([]) ->
+	{ok, state_name, #state{}}.
+
+state_name(_EventType, _EventData, StateData) ->
+	{next_state, state_name, StateData}.
+
+handle_event(_EventType, _EventData, StateName, StateData) ->
+	{next_state, StateName, StateData}.
+
+terminate(_Reason, _StateName, _StateData) ->
+	ok.
+
+code_change(_OldVsn, StateName, StateData, _Extra) ->
+	{ok, StateName, StateData}.
+endef
+
 define tpl_cowboy_loop
 -module($(n)).
 -behaviour(cowboy_loop_handler).

+ 3 - 1
test/plugin_bootstrap.mk

@@ -185,6 +185,7 @@ bootstrap-templates: build clean
 
 	$i "Generate one of each template"
 	$t $(MAKE) -C $(APP) --no-print-directory new t=gen_fsm n=my_fsm
+	$t $(MAKE) -C $(APP) --no-print-directory new t=gen_statem n=my_statem
 	$t $(MAKE) -C $(APP) --no-print-directory new t=gen_server n=my_server
 	$t $(MAKE) -C $(APP) --no-print-directory new t=supervisor n=my_sup
 	$t $(MAKE) -C $(APP) --no-print-directory new t=cowboy_http n=my_http
@@ -201,6 +202,7 @@ bootstrap-templates: build clean
 	$i "Check that all compiled files exist"
 	$t test -f $(APP)/ebin/$(APP).app
 	$t test -f $(APP)/ebin/my_fsm.beam
+	$t test -f $(APP)/ebin/my_statem.beam
 	$t test -f $(APP)/ebin/my_server.beam
 	$t test -f $(APP)/ebin/my_sup.beam
 	$t test -f $(APP)/ebin/my_module.beam
@@ -208,7 +210,7 @@ bootstrap-templates: build clean
 	$i "Check that all the modules can be loaded"
 	$t $(ERL) -pa $(APP)/ebin/ -eval " \
 		ok = application:start($(APP)), \
-		{ok, Mods = [my_fsm, my_http, my_loop, my_module, my_protocol, my_rest, my_server, my_sup, my_ws]} \
+		{ok, Mods = [my_fsm, my_http, my_loop, my_module, my_protocol, my_rest, my_server, my_statem, my_sup, my_ws]} \
 			= application:get_key($(APP), modules), \
 		[{module, M} = code:load_file(M) || M <- Mods], \
 		halt()"