erlgen 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,9 +2,19 @@
2
2
 
3
3
  erlgen is a Erlang application generator. It generates the basic application layout with some simple files to build upon.
4
4
 
5
+
6
+ == Usage
7
+
8
+ The basic command is erlgen projectname to generate a skeleton erlang project named **projectname**. The command-line options include:
9
+ --with-git
10
+ initializes a git repository in the project folder (does not commit anything).
11
+ --gen_server/--gen_fsm/--gen_event
12
+ instead of generating a application, generates a skeleton gen_server/fsm/event module with the name specified. If you're in the root of your project, it will write the fiel to the _src_ directory. If not, it will write it to the current directory.
13
+ --help
14
+ reminds you about the options.
15
+
5
16
  == Planned features:
6
17
 
7
- * Include rake tasks (or command line arguments) to generate default gen_server, gen_fsm, etc. modules.
8
18
  * Option to choose between Rake/Make/Emake.
9
19
 
10
20
  == Note on Patches/Pull Requests
@@ -17,6 +27,11 @@ erlgen is a Erlang application generator. It generates the basic application lay
17
27
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
18
28
  * Send me a pull request. Bonus points for topic branches.
19
29
 
30
+ == Thanks
31
+
32
+ I would like to thank the authors of the jeweler gem, lots of code in this gem comes from their work.
33
+
34
+
20
35
  == Copyright
21
36
 
22
37
  Copyright (c) 2010 Dimitri Krassovski. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/erlgen.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{erlgen}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dimitri Krassovski"]
@@ -33,6 +33,9 @@ Gem::Specification.new do |s|
33
33
  "lib/erlgen/templates/application.app",
34
34
  "lib/erlgen/templates/application.erl",
35
35
  "lib/erlgen/templates/application_sup.erl",
36
+ "lib/erlgen/templates/gen_event.erl",
37
+ "lib/erlgen/templates/gen_fsm.erl",
38
+ "lib/erlgen/templates/gen_server.erl",
36
39
  "spec/erlgen_spec.rb",
37
40
  "spec/spec.opts",
38
41
  "spec/spec_helper.rb"
@@ -1,13 +1,13 @@
1
1
  require 'optparse'
2
2
  require 'fileutils'
3
3
  require 'erb'
4
- require 'git'
4
+ require 'date'
5
+ #require 'git'
5
6
 
6
7
  class Erlgen
7
8
  class Generator
8
9
  attr_accessor :project_name, :options
9
10
  def initialize(*args)
10
-
11
11
  @options = {}
12
12
  @opts = OptionParser.new do |o|
13
13
  o.banner = "Usage: #{File.basename($0)} [options] appname\ne.g. #{File.basename($0)} superapp"
@@ -15,15 +15,28 @@ class Erlgen
15
15
  @options[:with_git] = true
16
16
  end
17
17
 
18
+ o.on('--gen_server', 'create a skeleton gen_server module') do
19
+ @options[:file_only] = true
20
+ @options[:gen] = "gen_server"
21
+ end
22
+
23
+ o.on('--gen_fsm', 'create a skeleton gen_fsm module') do
24
+ @options[:file_only] = true
25
+ @options[:gen] = "gen_fsm"
26
+ end
27
+
28
+ o.on('--gen_event', 'create a skeleton gen_event module') do
29
+ @options[:file_only] = true
30
+ @options[:gen] = "gen_event"
31
+ end
32
+
33
+
18
34
  o.on_tail('-h', '--help', 'display this help and exit') do
19
35
  @options[:show_help] = true
20
36
  end
21
37
  end
22
38
  @opts.parse!(args)
23
-
24
39
  @project_name = args.shift
25
-
26
-
27
40
  end
28
41
 
29
42
  def run
@@ -36,8 +49,12 @@ class Erlgen
36
49
  $stderr.puts @opts
37
50
  return 1
38
51
  end
39
-
40
- create_files
52
+
53
+ if @options[:file_only]
54
+ create_gen
55
+ else
56
+ create_files
57
+ end
41
58
 
42
59
  if @options[:with_git]
43
60
  create_version_control
@@ -58,10 +75,15 @@ class Erlgen
58
75
  output_template_in_target 'application.erl', File.join('src', "#{@project_name}.erl")
59
76
  output_template_in_target 'application_sup.erl', File.join('src', "#{@project_name}_sup.erl")
60
77
  output_template_in_target 'Rakefile'
78
+ touch_in_target File.join('include', "#{@project_name}.hrl")
61
79
  end
62
80
 
81
+ def create_gen
82
+ output_template_in_target "#{@options[:gen]}.erl", "#{@project_name}.erl"
83
+ end
84
+
63
85
  def target_dir
64
- @project_name
86
+ @options[:file_only] ? (Dir['*'].include?('src') ? 'src' : Dir.pwd) : @project_name
65
87
  end
66
88
 
67
89
  def mkdir_in_target(directory)
@@ -86,6 +108,12 @@ class Erlgen
86
108
 
87
109
  $stdout.puts "\tcreate\t#{destination}"
88
110
  end
111
+
112
+ def touch_in_target(destination)
113
+ final_destination = File.join(target_dir, destination)
114
+ FileUtils.touch final_destination
115
+ $stdout.puts "\tcreate\t#{destination}"
116
+ end
89
117
 
90
118
  def template_dir
91
119
  File.join(File.dirname(__FILE__), 'templates')
@@ -0,0 +1,105 @@
1
+ %%%-------------------------------------------------------------------
2
+ %%% File : <%= @project_name%>.erl
3
+ %%% Author :
4
+ %%% Description :
5
+ %%%
6
+ %%% Created : <%= Date.today.year%>
7
+ %%%-------------------------------------------------------------------
8
+ -module(<%= @project_name%>).
9
+
10
+ -behaviour(gen_event).
11
+
12
+ %% API
13
+ -export([start_link/0, add_handler/0]).
14
+
15
+ %% gen_event callbacks
16
+ -export([init/1, handle_event/2, handle_call/2,
17
+ handle_info/2, terminate/2, code_change/3]).
18
+
19
+ -record(state, {}).
20
+
21
+ %%====================================================================
22
+ %% gen_event callbacks
23
+ %%====================================================================
24
+ %%--------------------------------------------------------------------
25
+ %% Function: start_link() -> {ok,Pid} | {error,Error}
26
+ %% Description: Creates an event manager.
27
+ %%--------------------------------------------------------------------
28
+ start_link() ->
29
+ gen_event:start_link({local, ?SERVER}).
30
+
31
+ %%--------------------------------------------------------------------
32
+ %% Function: add_handler() -> ok | {'EXIT',Reason} | term()
33
+ %% Description: Adds an event handler
34
+ %%--------------------------------------------------------------------
35
+ add_handler() ->
36
+ gen_event:add_handler(?SERVER, ?MODULE, []).
37
+
38
+ %%====================================================================
39
+ %% gen_event callbacks
40
+ %%====================================================================
41
+ %%--------------------------------------------------------------------
42
+ %% Function: init(Args) -> {ok, State}
43
+ %% Description: Whenever a new event handler is added to an event manager,
44
+ %% this function is called to initialize the event handler.
45
+ %%--------------------------------------------------------------------
46
+ init([]) ->
47
+ {ok, #state{}}.
48
+
49
+ %%--------------------------------------------------------------------
50
+ %% Function:
51
+ %% handle_event(Event, State) -> {ok, State} |
52
+ %% {swap_handler, Args1, State1, Mod2, Args2} |
53
+ %% remove_handler
54
+ %% Description:Whenever an event manager receives an event sent using
55
+ %% gen_event:notify/2 or gen_event:sync_notify/2, this function is called for
56
+ %% each installed event handler to handle the event.
57
+ %%--------------------------------------------------------------------
58
+ handle_event(_Event, State) ->
59
+ {ok, State}.
60
+
61
+ %%--------------------------------------------------------------------
62
+ %% Function:
63
+ %% handle_call(Request, State) -> {ok, Reply, State} |
64
+ %% {swap_handler, Reply, Args1, State1,
65
+ %% Mod2, Args2} |
66
+ %% {remove_handler, Reply}
67
+ %% Description: Whenever an event manager receives a request sent using
68
+ %% gen_event:call/3,4, this function is called for the specified event
69
+ %% handler to handle the request.
70
+ %%--------------------------------------------------------------------
71
+ handle_call(_Request, State) ->
72
+ Reply = ok,
73
+ {ok, Reply, State}.
74
+
75
+ %%--------------------------------------------------------------------
76
+ %% Function:
77
+ %% handle_info(Info, State) -> {ok, State} |
78
+ %% {swap_handler, Args1, State1, Mod2, Args2} |
79
+ %% remove_handler
80
+ %% Description: This function is called for each installed event handler when
81
+ %% an event manager receives any other message than an event or a synchronous
82
+ %% request (or a system message).
83
+ %%--------------------------------------------------------------------
84
+ handle_info(_Info, State) ->
85
+ {ok, State}.
86
+
87
+ %%--------------------------------------------------------------------
88
+ %% Function: terminate(Reason, State) -> void()
89
+ %% Description:Whenever an event handler is deleted from an event manager,
90
+ %% this function is called. It should be the opposite of Module:init/1 and
91
+ %% do any necessary cleaning up.
92
+ %%--------------------------------------------------------------------
93
+ terminate(_Reason, _State) ->
94
+ ok.
95
+
96
+ %%--------------------------------------------------------------------
97
+ %% Function: code_change(OldVsn, State, Extra) -> {ok, NewState}
98
+ %% Description: Convert process state when code is changed
99
+ %%--------------------------------------------------------------------
100
+ code_change(_OldVsn, State, _Extra) ->
101
+ {ok, State}.
102
+
103
+ %%--------------------------------------------------------------------
104
+ %%% Internal functions
105
+ %%--------------------------------------------------------------------
@@ -0,0 +1,149 @@
1
+ %%%-------------------------------------------------------------------
2
+ %%% File : <%= @project_name%>.erl
3
+ %%% Author :
4
+ %%% Description :
5
+ %%%
6
+ %%% Created : <%= Date.today.year%>
7
+ %%%-------------------------------------------------------------------
8
+ -module(<%= @project_name%>).
9
+
10
+ -behaviour(gen_fsm).
11
+
12
+ %% API
13
+ -export([start_link/0]).
14
+
15
+ %% gen_fsm callbacks
16
+ -export([init/1, state_name/2, state_name/3, handle_event/3,
17
+ handle_sync_event/4, handle_info/3, terminate/3, code_change/4]).
18
+
19
+ -record(state, {}).
20
+
21
+ %%====================================================================
22
+ %% API
23
+ %%====================================================================
24
+ %%--------------------------------------------------------------------
25
+ %% Function: start_link() -> ok,Pid} | ignore | {error,Error}
26
+ %% Description:Creates a gen_fsm process which calls Module:init/1 to
27
+ %% initialize. To ensure a synchronized start-up procedure, this function
28
+ %% does not return until Module:init/1 has returned.
29
+ %%--------------------------------------------------------------------
30
+ start_link() ->
31
+ gen_fsm:start_link({local, ?SERVER}, ?MODULE, [], []).
32
+
33
+ %%====================================================================
34
+ %% gen_fsm callbacks
35
+ %%====================================================================
36
+ %%--------------------------------------------------------------------
37
+ %% Function: init(Args) -> {ok, StateName, State} |
38
+ %% {ok, StateName, State, Timeout} |
39
+ %% ignore |
40
+ %% {stop, StopReason}
41
+ %% Description:Whenever a gen_fsm is started using gen_fsm:start/[3,4] or
42
+ %% gen_fsm:start_link/3,4, this function is called by the new process to
43
+ %% initialize.
44
+ %%--------------------------------------------------------------------
45
+ init([]) ->
46
+ {ok, state_name, #state{}}.
47
+
48
+ %%--------------------------------------------------------------------
49
+ %% Function:
50
+ %% state_name(Event, State) -> {next_state, NextStateName, NextState}|
51
+ %% {next_state, NextStateName,
52
+ %% NextState, Timeout} |
53
+ %% {stop, Reason, NewState}
54
+ %% Description:There should be one instance of this function for each possible
55
+ %% state name. Whenever a gen_fsm receives an event sent using
56
+ %% gen_fsm:send_event/2, the instance of this function with the same name as
57
+ %% the current state name StateName is called to handle the event. It is also
58
+ %% called if a timeout occurs.
59
+ %%--------------------------------------------------------------------
60
+ state_name(_Event, State) ->
61
+ {next_state, state_name, State}.
62
+
63
+ %%--------------------------------------------------------------------
64
+ %% Function:
65
+ %% state_name(Event, From, State) -> {next_state, NextStateName, NextState} |
66
+ %% {next_state, NextStateName,
67
+ %% NextState, Timeout} |
68
+ %% {reply, Reply, NextStateName, NextState}|
69
+ %% {reply, Reply, NextStateName,
70
+ %% NextState, Timeout} |
71
+ %% {stop, Reason, NewState}|
72
+ %% {stop, Reason, Reply, NewState}
73
+ %% Description: There should be one instance of this function for each
74
+ %% possible state name. Whenever a gen_fsm receives an event sent using
75
+ %% gen_fsm:sync_send_event/2,3, the instance of this function with the same
76
+ %% name as the current state name StateName is called to handle the event.
77
+ %%--------------------------------------------------------------------
78
+ state_name(_Event, _From, State) ->
79
+ Reply = ok,
80
+ {reply, Reply, state_name, State}.
81
+
82
+ %%--------------------------------------------------------------------
83
+ %% Function:
84
+ %% handle_event(Event, StateName, State) -> {next_state, NextStateName,
85
+ %% NextState} |
86
+ %% {next_state, NextStateName,
87
+ %% NextState, Timeout} |
88
+ %% {stop, Reason, NewState}
89
+ %% Description: Whenever a gen_fsm receives an event sent using
90
+ %% gen_fsm:send_all_state_event/2, this function is called to handle
91
+ %% the event.
92
+ %%--------------------------------------------------------------------
93
+ handle_event(_Event, StateName, State) ->
94
+ {next_state, StateName, State}.
95
+
96
+ %%--------------------------------------------------------------------
97
+ %% Function:
98
+ %% handle_sync_event(Event, From, StateName,
99
+ %% State) -> {next_state, NextStateName, NextState} |
100
+ %% {next_state, NextStateName, NextState,
101
+ %% Timeout} |
102
+ %% {reply, Reply, NextStateName, NextState}|
103
+ %% {reply, Reply, NextStateName, NextState,
104
+ %% Timeout} |
105
+ %% {stop, Reason, NewState} |
106
+ %% {stop, Reason, Reply, NewState}
107
+ %% Description: Whenever a gen_fsm receives an event sent using
108
+ %% gen_fsm:sync_send_all_state_event/2,3, this function is called to handle
109
+ %% the event.
110
+ %%--------------------------------------------------------------------
111
+ handle_sync_event(Event, From, StateName, State) ->
112
+ Reply = ok,
113
+ {reply, Reply, StateName, State}.
114
+
115
+ %%--------------------------------------------------------------------
116
+ %% Function:
117
+ %% handle_info(Info,StateName,State)-> {next_state, NextStateName, NextState}|
118
+ %% {next_state, NextStateName, NextState,
119
+ %% Timeout} |
120
+ %% {stop, Reason, NewState}
121
+ %% Description: This function is called by a gen_fsm when it receives any
122
+ %% other message than a synchronous or asynchronous event
123
+ %% (or a system message).
124
+ %%--------------------------------------------------------------------
125
+ handle_info(_Info, StateName, State) ->
126
+ {next_state, StateName, State}.
127
+
128
+ %%--------------------------------------------------------------------
129
+ %% Function: terminate(Reason, StateName, State) -> void()
130
+ %% Description:This function is called by a gen_fsm when it is about
131
+ %% to terminate. It should be the opposite of Module:init/1 and do any
132
+ %% necessary cleaning up. When it returns, the gen_fsm terminates with
133
+ %% Reason. The return value is ignored.
134
+ %%--------------------------------------------------------------------
135
+ terminate(_Reason, _StateName, _State) ->
136
+ ok.
137
+
138
+ %%--------------------------------------------------------------------
139
+ %% Function:
140
+ %% code_change(OldVsn, StateName, State, Extra) -> {ok, StateName, NewState}
141
+ %% Description: Convert process state when code is changed
142
+ %%--------------------------------------------------------------------
143
+ code_change(_OldVsn, StateName, State, _Extra) ->
144
+ {ok, StateName, State}.
145
+
146
+ %%--------------------------------------------------------------------
147
+ %%% Internal functions
148
+ %%--------------------------------------------------------------------
149
+
@@ -0,0 +1,96 @@
1
+ %%%-------------------------------------------------------------------
2
+ %%% File : <%= @project_name%>.erl
3
+ %%% Author :
4
+ %%% Description :
5
+ %%%
6
+ %%% Created : <%= Date.today.year %>
7
+ %%%-------------------------------------------------------------------
8
+ -module(<%= @project_name%>).
9
+
10
+ -behaviour(gen_server).
11
+
12
+ %% API
13
+ -export([start_link/0]).
14
+
15
+ %% gen_server callbacks
16
+ -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
17
+ terminate/2, code_change/3]).
18
+
19
+ -record(state, {}).
20
+
21
+ %%====================================================================
22
+ %% API
23
+ %%====================================================================
24
+ %%--------------------------------------------------------------------
25
+ %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
26
+ %% Description: Starts the server
27
+ %%--------------------------------------------------------------------
28
+ start_link() ->
29
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
30
+
31
+ %%====================================================================
32
+ %% gen_server callbacks
33
+ %%====================================================================
34
+
35
+ %%--------------------------------------------------------------------
36
+ %% Function: init(Args) -> {ok, State} |
37
+ %% {ok, State, Timeout} |
38
+ %% ignore |
39
+ %% {stop, Reason}
40
+ %% Description: Initiates the server
41
+ %%--------------------------------------------------------------------
42
+ init([]) ->
43
+ {ok, #state{}}.
44
+
45
+ %%--------------------------------------------------------------------
46
+ %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
47
+ %% {reply, Reply, State, Timeout} |
48
+ %% {noreply, State} |
49
+ %% {noreply, State, Timeout} |
50
+ %% {stop, Reason, Reply, State} |
51
+ %% {stop, Reason, State}
52
+ %% Description: Handling call messages
53
+ %%--------------------------------------------------------------------
54
+ handle_call(_Request, _From, State) ->
55
+ Reply = ok,
56
+ {reply, Reply, State}.
57
+
58
+ %%--------------------------------------------------------------------
59
+ %% Function: handle_cast(Msg, State) -> {noreply, State} |
60
+ %% {noreply, State, Timeout} |
61
+ %% {stop, Reason, State}
62
+ %% Description: Handling cast messages
63
+ %%--------------------------------------------------------------------
64
+ handle_cast(_Msg, State) ->
65
+ {noreply, State}.
66
+
67
+ %%--------------------------------------------------------------------
68
+ %% Function: handle_info(Info, State) -> {noreply, State} |
69
+ %% {noreply, State, Timeout} |
70
+ %% {stop, Reason, State}
71
+ %% Description: Handling all non call/cast messages
72
+ %%--------------------------------------------------------------------
73
+ handle_info(_Info, State) ->
74
+ {noreply, State}.
75
+
76
+ %%--------------------------------------------------------------------
77
+ %% Function: terminate(Reason, State) -> void()
78
+ %% Description: This function is called by a gen_server when it is about to
79
+ %% terminate. It should be the opposite of Module:init/1 and do any necessary
80
+ %% cleaning up. When it returns, the gen_server terminates with Reason.
81
+ %% The return value is ignored.
82
+ %%--------------------------------------------------------------------
83
+ terminate(_Reason, _State) ->
84
+ ok.
85
+
86
+ %%--------------------------------------------------------------------
87
+ %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
88
+ %% Description: Convert process state when code is changed
89
+ %%--------------------------------------------------------------------
90
+ code_change(_OldVsn, State, _Extra) ->
91
+ {ok, State}.
92
+
93
+ %%--------------------------------------------------------------------
94
+ %%% Internal functions
95
+ %%--------------------------------------------------------------------
96
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dimitri Krassovski
@@ -69,6 +69,9 @@ files:
69
69
  - lib/erlgen/templates/application.app
70
70
  - lib/erlgen/templates/application.erl
71
71
  - lib/erlgen/templates/application_sup.erl
72
+ - lib/erlgen/templates/gen_event.erl
73
+ - lib/erlgen/templates/gen_fsm.erl
74
+ - lib/erlgen/templates/gen_server.erl
72
75
  - spec/erlgen_spec.rb
73
76
  - spec/spec.opts
74
77
  - spec/spec_helper.rb