ruboty-generator 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +256 -0
- data/Rakefile +8 -0
- data/bin/ruboty-generator +32 -0
- data/lib/ruboty/dsl.rb +3 -0
- data/lib/ruboty/dsl/dsl.rb +50 -0
- data/lib/ruboty/dsl/dsl_model.rb +24 -0
- data/lib/ruboty/generator.rb +10 -0
- data/lib/ruboty/generator/generate.rb +198 -0
- data/lib/ruboty/generator/version.rb +7 -0
- data/lib/ruboty/models.rb +9 -0
- data/lib/ruboty/models/command.rb +30 -0
- data/lib/ruboty/models/env.rb +26 -0
- data/ruboty-generator.gemspec +26 -0
- data/spec/ruboty/generator/generate_spec.rb +229 -0
- data/spec/spec_helper.rb +6 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cf075a10391f9133668d23707a8deab3fe6ced5b
|
4
|
+
data.tar.gz: 25fc534f94402271fb61b1322512c8483d916421
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 09bf3ef39f86743f3e66273cb7513bb4ebb6aba24af50a7b3989d386e89c41e56f41029005d07795dc0ab721e0f65e08050157330e77ce89c45e92c1e03312b6
|
7
|
+
data.tar.gz: f875567f7f8ed690e0f90247fa133e77178e801a558a8b7d9079748b60aa91c94b36e216d7c4fb759cf13c149096fdb2b08b84aeb78f33b586ec5978ea83a269
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 tbpgr
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,256 @@
|
|
1
|
+
# Ruboty::Gen::Readme
|
2
|
+
|
3
|
+
Generate Ruboty Handler + Actions plugin.
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/ruboty-megen.svg)](http://badge.fury.io/rb/ruboty-generator)
|
6
|
+
[![Build Status](https://travis-ci.org/tbpgr/ruboty-generator.png?branch=master)](https://travis-ci.org/tbpgr/ruboty-generator)
|
7
|
+
|
8
|
+
[Ruboty](https://github.com/r7kamura/ruboty) is Chat bot framework. Ruby + Bot = Ruboty
|
9
|
+
|
10
|
+
## :arrow_down: Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'ruboty-generator'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install ruboty-generator
|
25
|
+
|
26
|
+
## :blue_book: Rubotygenerator
|
27
|
+
### Setting File Parameters
|
28
|
+
|
29
|
+
|key|value|example|
|
30
|
+
|:--|:--|:--|
|
31
|
+
|user_name|github user name|tbpgr|
|
32
|
+
|gem_class_name|gem class name|Hoge|
|
33
|
+
|gem_name|gem name|hoge|
|
34
|
+
|description|description|An Ruboty Handler + Actions to output hige.|
|
35
|
+
|env/name|ENV variable name|DEFAULT_HOGE_TEXT|
|
36
|
+
|env/description|ENV description|default hoge text|
|
37
|
+
|commands/command name|Ruboty::Handler.on name|hoge|
|
38
|
+
|commands/command pattern|Ruboty::Handler.on pattern|/hoge\z/ |
|
39
|
+
|commands/command description|Ruboty::Handler.on description|output hige|
|
40
|
+
|
41
|
+
## :scroll: Usage
|
42
|
+
### init
|
43
|
+
generate Rubotygenerator template file.
|
44
|
+
|
45
|
+
~~~bash
|
46
|
+
$ ruboty-generator init
|
47
|
+
$ ls -1 | grep Rubotygenerator
|
48
|
+
Rubotygenerator
|
49
|
+
~~~
|
50
|
+
|
51
|
+
### generate
|
52
|
+
generate Ruboty Handler + Action template
|
53
|
+
|
54
|
+
* edit Rubotygenerator file
|
55
|
+
|
56
|
+
~~~ruby
|
57
|
+
# encoding: utf-8
|
58
|
+
user_name "tbpgr"
|
59
|
+
|
60
|
+
gem_class_name "Hoge"
|
61
|
+
gem_name "hoge"
|
62
|
+
|
63
|
+
description "An Ruboty Handler + Actions to hoge-hige"
|
64
|
+
|
65
|
+
env do |e|
|
66
|
+
e.name "DEFAULT_HOGE_TEXT1"
|
67
|
+
e.description "DEFAULT_HOGE_TEXT1 desc"
|
68
|
+
end
|
69
|
+
|
70
|
+
env do |e|
|
71
|
+
e.name "DEFAULT_HOGE_TEXT2"
|
72
|
+
e.description "DEFAULT_HOGE_TEXT2 desc"
|
73
|
+
end
|
74
|
+
|
75
|
+
command do |c|
|
76
|
+
c.name "hoge"
|
77
|
+
c.pattern "hoge\\z"
|
78
|
+
c.description "output hige"
|
79
|
+
end
|
80
|
+
|
81
|
+
command do |c|
|
82
|
+
c.name "hige"
|
83
|
+
c.pattern "hige\\z"
|
84
|
+
c.description "output hige"
|
85
|
+
end
|
86
|
+
~~~
|
87
|
+
|
88
|
+
#### generate plugin with action
|
89
|
+
|
90
|
+
~~~bash
|
91
|
+
$ ruboty-generator generate -a
|
92
|
+
~~~
|
93
|
+
|
94
|
+
* output
|
95
|
+
|
96
|
+
```bash
|
97
|
+
.
|
98
|
+
└── ruboty-hoge
|
99
|
+
├── Gemfile
|
100
|
+
├── LICENSE.txt
|
101
|
+
├── README.md
|
102
|
+
├── Rakefile
|
103
|
+
├── bin
|
104
|
+
│ ├── console
|
105
|
+
│ └── setup
|
106
|
+
├── lib
|
107
|
+
│ └── ruboty
|
108
|
+
│ ├── handlers
|
109
|
+
│ │ └── hoge.rb
|
110
|
+
│ ├── hoge
|
111
|
+
│ │ ├── actions
|
112
|
+
│ │ │ ├── hige.rb
|
113
|
+
│ │ │ └── hoge.rb
|
114
|
+
│ │ └── version.rb
|
115
|
+
│ └── hoge.rb
|
116
|
+
└── ruboty-hoge.gemspec
|
117
|
+
```
|
118
|
+
|
119
|
+
* check generated handler
|
120
|
+
|
121
|
+
~~~ruby
|
122
|
+
require "ruboty/hoge/actions/hoge"
|
123
|
+
require "ruboty/hoge/actions/hige"
|
124
|
+
|
125
|
+
module Ruboty
|
126
|
+
module Handlers
|
127
|
+
# An Ruboty Handler + Actions to hoge-hige
|
128
|
+
class Hoge < Base
|
129
|
+
on /hoge\z/, name: 'hoge', description: 'output hige'
|
130
|
+
on /hige\z/, name: 'hige', description: 'output hige'
|
131
|
+
env :DEFAULT_HOGE_TEXT1, "DEFAULT_HOGE_TEXT1 desc"
|
132
|
+
env :DEFAULT_HOGE_TEXT2, "DEFAULT_HOGE_TEXT2 desc"
|
133
|
+
|
134
|
+
def hoge(message)
|
135
|
+
Ruboty::Hoge::Actions::Hoge.new(message).call
|
136
|
+
end
|
137
|
+
|
138
|
+
def hige(message)
|
139
|
+
Ruboty::Hoge::Actions::Hige.new(message).call
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
~~~
|
146
|
+
|
147
|
+
* check generated action
|
148
|
+
|
149
|
+
hoge.rb
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
module Ruboty
|
153
|
+
module Hoge
|
154
|
+
module Actions
|
155
|
+
class Hoge < Ruboty::Actions::Base
|
156
|
+
def call
|
157
|
+
message.reply(hoge)
|
158
|
+
rescue => e
|
159
|
+
message.reply(e.message)
|
160
|
+
end
|
161
|
+
|
162
|
+
private
|
163
|
+
def hoge
|
164
|
+
# TODO: main logic
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
```
|
171
|
+
|
172
|
+
hige.rb
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
module Ruboty
|
176
|
+
module Hoge
|
177
|
+
module Actions
|
178
|
+
class Hige < Ruboty::Actions::Base
|
179
|
+
def call
|
180
|
+
message.reply(hige)
|
181
|
+
rescue => e
|
182
|
+
message.reply(e.message)
|
183
|
+
end
|
184
|
+
|
185
|
+
private
|
186
|
+
def hige
|
187
|
+
# TODO: main logic
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
```
|
194
|
+
|
195
|
+
#### generate plugin without action
|
196
|
+
|
197
|
+
~~~bash
|
198
|
+
$ ruboty-generator generate
|
199
|
+
~~~
|
200
|
+
|
201
|
+
* output
|
202
|
+
|
203
|
+
```bash
|
204
|
+
.
|
205
|
+
└── ruboty-hoge
|
206
|
+
├── Gemfile
|
207
|
+
├── README.md
|
208
|
+
├── Rakefile
|
209
|
+
├── bin
|
210
|
+
│ ├── console
|
211
|
+
│ └── setup
|
212
|
+
├── lib
|
213
|
+
│ └── ruboty
|
214
|
+
│ ├── handlers
|
215
|
+
│ │ └── hoge.rb
|
216
|
+
│ ├── hoge
|
217
|
+
│ │ └── version.rb
|
218
|
+
│ └── hoge.rb
|
219
|
+
└── ruboty-hoge.gemspec
|
220
|
+
```
|
221
|
+
|
222
|
+
* check generated handler
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
|
226
|
+
|
227
|
+
module Ruboty
|
228
|
+
module Handlers
|
229
|
+
# An Ruboty Handler + Actions to hoge-hige
|
230
|
+
class Hoge < Base
|
231
|
+
on /hoge\z/, name: 'hoge', description: 'output hige'
|
232
|
+
on /hige\z/, name: 'hige', description: 'output hige'
|
233
|
+
env :DEFAULT_HOGE_TEXT1, "DEFAULT_HOGE_TEXT1 desc"
|
234
|
+
env :DEFAULT_HOGE_TEXT2, "DEFAULT_HOGE_TEXT2 desc"
|
235
|
+
|
236
|
+
def hoge(message)
|
237
|
+
# TODO: implement your action
|
238
|
+
end
|
239
|
+
|
240
|
+
def hige(message)
|
241
|
+
# TODO: implement your action
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
```
|
248
|
+
|
249
|
+
## :two_men_holding_hands: Contributing :two_women_holding_hands:
|
250
|
+
|
251
|
+
1. Fork it ( https://github.com/tbpgr/ruboty-generator/fork )
|
252
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
253
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
254
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
255
|
+
5. Create a new Pull Request
|
256
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'ruboty/generator'
|
5
|
+
require 'ruboty/generator/version'
|
6
|
+
require 'thor'
|
7
|
+
|
8
|
+
module Ruboty
|
9
|
+
# = RubotyGenerator CLI
|
10
|
+
class CLI < Thor
|
11
|
+
class_option :help, type: :boolean, aliases: '-h', desc: 'help message.'
|
12
|
+
class_option :version, type: :boolean, desc: 'version'
|
13
|
+
|
14
|
+
desc 'generate', 'Generate Ruboty Plugin template'
|
15
|
+
option :a, type: :boolean, desc: 'Generate Ruboty Handler Plugin template with Actions'
|
16
|
+
def generate
|
17
|
+
Ruboty::Generator.generate(options)
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'init', 'generate Rubotygenerator template'
|
21
|
+
def init
|
22
|
+
Ruboty::Generator.init
|
23
|
+
end
|
24
|
+
|
25
|
+
desc 'version', 'version'
|
26
|
+
def version
|
27
|
+
p Ruboty::Generator::VERSION
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Ruboty::CLI.start(ARGV)
|
data/lib/ruboty/dsl.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'ruboty/dsl/dsl_model'
|
3
|
+
require 'ruboty/models/env'
|
4
|
+
require 'ruboty/models/command'
|
5
|
+
|
6
|
+
module Ruboty
|
7
|
+
# Dsl
|
8
|
+
class Dsl
|
9
|
+
attr_accessor :ruboty_generator
|
10
|
+
|
11
|
+
[:user_name, :gem_class_name, :gem_name, :description].each do |f|
|
12
|
+
define_method f do |value|
|
13
|
+
@ruboty_generator.send("#{f}=", value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def env
|
18
|
+
e = Ruboty::Models::Env.new
|
19
|
+
yield(e)
|
20
|
+
@ruboty_generator.env << e
|
21
|
+
end
|
22
|
+
|
23
|
+
def command
|
24
|
+
c = Ruboty::Models::Command.new
|
25
|
+
yield(c)
|
26
|
+
@ruboty_generator.commands << c
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
@ruboty_generator = Ruboty::DslModel.new
|
31
|
+
@ruboty_generator.user_name = 'your github username'
|
32
|
+
@ruboty_generator.gem_class_name = 'your_gem_class_name'
|
33
|
+
@ruboty_generator.gem_name = 'your_gem_name'
|
34
|
+
@ruboty_generator.description = 'description'
|
35
|
+
@ruboty_generator.env = []
|
36
|
+
@ruboty_generator.commands = []
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_s
|
40
|
+
<<-EOS
|
41
|
+
user_name = #{@ruboty_generator.user_name}
|
42
|
+
gem_class_name = #{@ruboty_generator.gem_class_name}
|
43
|
+
gem_name = #{@ruboty_generator.gem_name}
|
44
|
+
description = #{@ruboty_generator.description}
|
45
|
+
env = #{@ruboty_generator.env}
|
46
|
+
commands = #{@ruboty_generator.commands}
|
47
|
+
EOS
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
# DslModel
|
5
|
+
class DslModel
|
6
|
+
# user_name
|
7
|
+
attr_accessor :user_name
|
8
|
+
|
9
|
+
# gem_class_name
|
10
|
+
attr_accessor :gem_class_name
|
11
|
+
|
12
|
+
# description
|
13
|
+
attr_accessor :description
|
14
|
+
|
15
|
+
# gem_name
|
16
|
+
attr_accessor :gem_name
|
17
|
+
|
18
|
+
# env
|
19
|
+
attr_accessor :env
|
20
|
+
|
21
|
+
# commands
|
22
|
+
attr_accessor :commands
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'erb'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module Ruboty
|
6
|
+
# Generator Core
|
7
|
+
class Generator
|
8
|
+
README = 'README.md'
|
9
|
+
RUBOTY_GENERATOR_FILE = 'Rubotygenerator'
|
10
|
+
RUBOTY_HANDLER_FILE = 'lib/ruboty/handlers'
|
11
|
+
RUBOTY_BASE_FILE = 'lib/ruboty'
|
12
|
+
RUBOTY_GENERATOR_FILE_TEMPLATE = <<-EOS
|
13
|
+
user_name "user name"
|
14
|
+
|
15
|
+
gem_class_name "Gem class name"
|
16
|
+
gem_name "gem name"
|
17
|
+
|
18
|
+
description "An Ruboty Handler description"
|
19
|
+
|
20
|
+
env do |e|
|
21
|
+
e.name "ENV1"
|
22
|
+
e.description "ENV1 desc"
|
23
|
+
end
|
24
|
+
|
25
|
+
command do |c|
|
26
|
+
c.name "name"
|
27
|
+
c.pattern "pattern\\\\z"
|
28
|
+
c.description "description"
|
29
|
+
end
|
30
|
+
EOS
|
31
|
+
|
32
|
+
RUBOTY_HANDLER_TEMPLATE = <<-EOS
|
33
|
+
<%=action_requires%>
|
34
|
+
|
35
|
+
module Ruboty
|
36
|
+
module Handlers
|
37
|
+
# <%=description%>
|
38
|
+
class <%=gem_class_name%> < Base
|
39
|
+
<%=action_macros%>
|
40
|
+
<%=envs%>
|
41
|
+
|
42
|
+
<%=action_definitions%>
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
EOS
|
47
|
+
|
48
|
+
RUBOTY_ACTION_TEMPLATE = <<-EOS
|
49
|
+
module Ruboty
|
50
|
+
module <%=gem_class_name%>
|
51
|
+
module Actions
|
52
|
+
class <%=action_name.capitalize%> < Ruboty::Actions::Base
|
53
|
+
def call
|
54
|
+
message.reply(<%=action_name%>)
|
55
|
+
rescue => e
|
56
|
+
message.reply(e.message)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
def <%=action_name%>
|
61
|
+
# TODO: main logic
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
EOS
|
68
|
+
|
69
|
+
# generate Rubotymegenfile to current directory.
|
70
|
+
def self.init
|
71
|
+
File.open(RUBOTY_GENERATOR_FILE, 'w') do |f|
|
72
|
+
f.puts RUBOTY_GENERATOR_FILE_TEMPLATE
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# generate ruboty template.
|
77
|
+
def self.generate(options = {})
|
78
|
+
config = load_config
|
79
|
+
execute_bundle_gem(config)
|
80
|
+
handler_src = generate_handler(config, options[:a])
|
81
|
+
output_handler(handler_src, config.gem_name)
|
82
|
+
return unless options[:a]
|
83
|
+
actions = generate_actions(config)
|
84
|
+
output_actions(actions, config.gem_name)
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.load_config
|
88
|
+
src = read_dsl
|
89
|
+
dsl = Ruboty::Dsl.new
|
90
|
+
dsl.instance_eval(src)
|
91
|
+
dsl.ruboty_generator
|
92
|
+
end
|
93
|
+
private_class_method :load_config
|
94
|
+
|
95
|
+
def self.read_dsl
|
96
|
+
File.open(RUBOTY_GENERATOR_FILE, &:read)
|
97
|
+
end
|
98
|
+
private_class_method :read_dsl
|
99
|
+
|
100
|
+
def self.execute_bundle_gem(config)
|
101
|
+
`bundle gem --mit ruboty-#{config.gem_name}`
|
102
|
+
end
|
103
|
+
private_class_method :execute_bundle_gem
|
104
|
+
|
105
|
+
def self.generate_handler(config, have_actions)
|
106
|
+
gem_class_name = config.gem_class_name
|
107
|
+
gem_name = config.gem_name
|
108
|
+
description = config.description
|
109
|
+
actions = generate_action_definitions(config, gem_class_name, have_actions)
|
110
|
+
action_requires = actions[:action_requires]
|
111
|
+
action_definitions = actions[:action_definitions]
|
112
|
+
action_macros = actions[:action_macros]
|
113
|
+
envs = generate_env(config)
|
114
|
+
erb = ERB.new(RUBOTY_HANDLER_TEMPLATE)
|
115
|
+
erb.result(binding)
|
116
|
+
end
|
117
|
+
private_class_method :generate_handler
|
118
|
+
|
119
|
+
def self.output_handler(handler_src, handler_name)
|
120
|
+
handler_path = "./ruboty-#{handler_name}/#{Ruboty::Generator::RUBOTY_HANDLER_FILE}"
|
121
|
+
FileUtils.mkdir_p(handler_path)
|
122
|
+
File.open("#{handler_path}/#{handler_name}.rb", 'w:utf-8') { |e| e.puts handler_src }
|
123
|
+
end
|
124
|
+
private_class_method :output_handler
|
125
|
+
|
126
|
+
def self.generate_action_definitions(config, gem_class_name, have_actions)
|
127
|
+
if have_actions
|
128
|
+
action_requires = config.commands.map { |e| "require \"ruboty/#{config.gem_name}/actions/#{e.read_name}\"" }.join("\n")
|
129
|
+
action_definitions = generate_action_definitions_with_action(config, gem_class_name)
|
130
|
+
else
|
131
|
+
action_requires = ''
|
132
|
+
action_definitions = generate_action_definitions_without_action(config)
|
133
|
+
end
|
134
|
+
action_macros = generate_action_macros(config)
|
135
|
+
{ action_requires: action_requires, action_definitions: action_definitions, action_macros: action_macros }
|
136
|
+
end
|
137
|
+
private_class_method :generate_action_definitions
|
138
|
+
|
139
|
+
def self.generate_action_definitions_with_action(config, gem_class_name)
|
140
|
+
definitions = config.commands.map do |e|
|
141
|
+
<<-EOS
|
142
|
+
def #{e.read_name}(message)
|
143
|
+
Ruboty::#{gem_class_name}::Actions::#{e.read_name.capitalize}.new(message).call
|
144
|
+
end
|
145
|
+
EOS
|
146
|
+
end
|
147
|
+
definitions.join("\n")
|
148
|
+
end
|
149
|
+
private_class_method :generate_action_definitions_with_action
|
150
|
+
|
151
|
+
def self.generate_action_definitions_without_action(config)
|
152
|
+
definitions = config.commands.map do |e|
|
153
|
+
<<-EOS
|
154
|
+
def #{e.read_name}(message)
|
155
|
+
# TODO: implement your action
|
156
|
+
end
|
157
|
+
EOS
|
158
|
+
end
|
159
|
+
definitions.join("\n")
|
160
|
+
end
|
161
|
+
private_class_method :generate_action_definitions_without_action
|
162
|
+
|
163
|
+
def self.generate_action_macros(config)
|
164
|
+
action_macros = config.commands.map do |e|
|
165
|
+
" on /#{e.read_pattern}/, name: '#{e.read_name}', description: '#{e.read_description}'"
|
166
|
+
end
|
167
|
+
action_macros.join("\n")
|
168
|
+
end
|
169
|
+
private_class_method :generate_action_macros
|
170
|
+
|
171
|
+
def self.generate_env(config)
|
172
|
+
config.env.map { |e| " env :#{e.read_name}, \"#{e.read_description}\"" }.join("\n")
|
173
|
+
end
|
174
|
+
private_class_method :generate_env
|
175
|
+
|
176
|
+
def self.generate_actions(config)
|
177
|
+
gem_class_name = config.gem_class_name
|
178
|
+
config.commands.each_with_object([]) do |e, memo|
|
179
|
+
action_name = e.read_name
|
180
|
+
erb = ERB.new(RUBOTY_ACTION_TEMPLATE)
|
181
|
+
action = {}
|
182
|
+
action[:name] = action_name
|
183
|
+
action[:contents] = erb.result(binding)
|
184
|
+
memo << action
|
185
|
+
end
|
186
|
+
end
|
187
|
+
private_class_method :generate_actions
|
188
|
+
|
189
|
+
def self.output_actions(actions, handler_name)
|
190
|
+
actions.each do |action|
|
191
|
+
actions_dir_path = "./ruboty-#{handler_name}/#{Ruboty::Generator::RUBOTY_BASE_FILE}/#{handler_name}/actions"
|
192
|
+
FileUtils.mkdir_p(actions_dir_path)
|
193
|
+
File.open("#{actions_dir_path}/#{action[:name]}.rb", 'w:utf-8') { |e| e.puts action[:contents] }
|
194
|
+
end
|
195
|
+
end
|
196
|
+
private_class_method :output_actions
|
197
|
+
end
|
198
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module Models
|
5
|
+
# Command
|
6
|
+
class Command
|
7
|
+
def initialize
|
8
|
+
@name, @pattern, @description = ''
|
9
|
+
end
|
10
|
+
|
11
|
+
def name(name)
|
12
|
+
@name = name
|
13
|
+
end
|
14
|
+
|
15
|
+
def pattern(pattern)
|
16
|
+
@pattern = pattern
|
17
|
+
end
|
18
|
+
|
19
|
+
def description(description)
|
20
|
+
@description = description
|
21
|
+
end
|
22
|
+
|
23
|
+
[:name, :pattern, :description].each do |m|
|
24
|
+
define_method :"read_#{m}" do
|
25
|
+
instance_variable_get("@#{m}")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Ruboty
|
4
|
+
module Models
|
5
|
+
# Env
|
6
|
+
class Env
|
7
|
+
def initialize
|
8
|
+
@name, @description = ''
|
9
|
+
end
|
10
|
+
|
11
|
+
def name(name)
|
12
|
+
@name = name
|
13
|
+
end
|
14
|
+
|
15
|
+
def description(description)
|
16
|
+
@description = description
|
17
|
+
end
|
18
|
+
|
19
|
+
[:name, :description].each do |m|
|
20
|
+
define_method :"read_#{m}" do
|
21
|
+
instance_variable_get("@#{m}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ruboty/generator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ruboty-generator'
|
8
|
+
spec.version = Ruboty::Generator::VERSION
|
9
|
+
spec.authors = ['tbpgr']
|
10
|
+
spec.email = ['tbpgr@tbpgr.jp']
|
11
|
+
spec.summary = 'ruboty-generator generate ruboty plugin template.'
|
12
|
+
spec.description = 'ruboty-generator generate ruboty plugin template.'
|
13
|
+
spec.homepage = 'https://github.com/tbpgr/ruboty-generator'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'thor', '~> 0.18.1'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 2.14.1'
|
26
|
+
end
|
@@ -0,0 +1,229 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'ruboty/generator'
|
4
|
+
|
5
|
+
describe Ruboty::Generator do
|
6
|
+
context :init do
|
7
|
+
let(:tmp) { 'tmp' }
|
8
|
+
|
9
|
+
cases = [
|
10
|
+
{
|
11
|
+
case_no: 1,
|
12
|
+
case_title: 'valid case',
|
13
|
+
expected: Ruboty::Generator::RUBOTY_GENERATOR_FILE_TEMPLATE
|
14
|
+
}
|
15
|
+
]
|
16
|
+
|
17
|
+
cases.each do |c|
|
18
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
19
|
+
begin
|
20
|
+
case_before c
|
21
|
+
|
22
|
+
# -- given --
|
23
|
+
# nothing
|
24
|
+
|
25
|
+
# -- when --
|
26
|
+
Ruboty::Generator.init
|
27
|
+
actual = File.open(Ruboty::Generator::RUBOTY_GENERATOR_FILE, 'r:utf-8', &:read)
|
28
|
+
|
29
|
+
# -- then --
|
30
|
+
expect(actual).to eq(c[:expected])
|
31
|
+
ensure
|
32
|
+
case_after c
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def case_before(_c)
|
37
|
+
FileUtils.mkdir_p(tmp) unless Dir.exist? tmp
|
38
|
+
Dir.chdir(tmp)
|
39
|
+
end
|
40
|
+
|
41
|
+
def case_after(_c)
|
42
|
+
Dir.chdir('../')
|
43
|
+
FileUtils.rm_rf(tmp) if Dir.exist? tmp
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context :generate do
|
49
|
+
let(:tmp) { 'tmp_generate' }
|
50
|
+
let(:template) do
|
51
|
+
template = <<-EOS
|
52
|
+
user_name "tbpgr"
|
53
|
+
|
54
|
+
gem_class_name "Hoge"
|
55
|
+
gem_name "hoge"
|
56
|
+
|
57
|
+
description "An Ruboty Handler + Actions to output hige."
|
58
|
+
|
59
|
+
env do |e|
|
60
|
+
e.name "DEFAULT_HOGE_TEXT1"
|
61
|
+
e.description "DEFAULT_HOGE_TEXT1 desc"
|
62
|
+
end
|
63
|
+
|
64
|
+
env do |e|
|
65
|
+
e.name "DEFAULT_HOGE_TEXT2"
|
66
|
+
e.description "DEFAULT_HOGE_TEXT2 desc"
|
67
|
+
end
|
68
|
+
|
69
|
+
command do |c|
|
70
|
+
c.name "hoge"
|
71
|
+
c.pattern "hoge\\\\z"
|
72
|
+
c.description "output hige"
|
73
|
+
end
|
74
|
+
|
75
|
+
command do |c|
|
76
|
+
c.name "hige"
|
77
|
+
c.pattern "hige\\\\z"
|
78
|
+
c.description "output hige"
|
79
|
+
end
|
80
|
+
EOS
|
81
|
+
template
|
82
|
+
end
|
83
|
+
|
84
|
+
cases = [
|
85
|
+
{
|
86
|
+
case_no: 1,
|
87
|
+
case_title: 'valid case with actions',
|
88
|
+
options: { a: true },
|
89
|
+
handler: 'hoge',
|
90
|
+
actions: %w(hoge hige),
|
91
|
+
expected_handler: <<-EOS,
|
92
|
+
require "ruboty/hoge/actions/hoge"
|
93
|
+
require "ruboty/hoge/actions/hige"
|
94
|
+
|
95
|
+
module Ruboty
|
96
|
+
module Handlers
|
97
|
+
# An Ruboty Handler + Actions to output hige.
|
98
|
+
class Hoge < Base
|
99
|
+
on /hoge\\z/, name: 'hoge', description: 'output hige'
|
100
|
+
on /hige\\z/, name: 'hige', description: 'output hige'
|
101
|
+
env :DEFAULT_HOGE_TEXT1, "DEFAULT_HOGE_TEXT1 desc"
|
102
|
+
env :DEFAULT_HOGE_TEXT2, "DEFAULT_HOGE_TEXT2 desc"
|
103
|
+
|
104
|
+
def hoge(message)
|
105
|
+
Ruboty::Hoge::Actions::Hoge.new(message).call
|
106
|
+
end
|
107
|
+
|
108
|
+
def hige(message)
|
109
|
+
Ruboty::Hoge::Actions::Hige.new(message).call
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
EOS
|
116
|
+
expected_action_hoge: <<-EOS,
|
117
|
+
module Ruboty
|
118
|
+
module Hoge
|
119
|
+
module Actions
|
120
|
+
class Hoge < Ruboty::Actions::Base
|
121
|
+
def call
|
122
|
+
message.reply(hoge)
|
123
|
+
rescue => e
|
124
|
+
message.reply(e.message)
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
def hoge
|
129
|
+
# TODO: main logic
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
EOS
|
136
|
+
expected_action_hige: <<-EOS,
|
137
|
+
module Ruboty
|
138
|
+
module Hoge
|
139
|
+
module Actions
|
140
|
+
class Hige < Ruboty::Actions::Base
|
141
|
+
def call
|
142
|
+
message.reply(hige)
|
143
|
+
rescue => e
|
144
|
+
message.reply(e.message)
|
145
|
+
end
|
146
|
+
|
147
|
+
private
|
148
|
+
def hige
|
149
|
+
# TODO: main logic
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
EOS
|
156
|
+
},
|
157
|
+
{
|
158
|
+
case_no: 2,
|
159
|
+
case_title: 'valid case without actions',
|
160
|
+
options: { a: false },
|
161
|
+
handler: 'hoge',
|
162
|
+
actions: [],
|
163
|
+
expected_handler: <<-EOS,
|
164
|
+
|
165
|
+
|
166
|
+
module Ruboty
|
167
|
+
module Handlers
|
168
|
+
# An Ruboty Handler + Actions to output hige.
|
169
|
+
class Hoge < Base
|
170
|
+
on /hoge\\z/, name: 'hoge', description: 'output hige'
|
171
|
+
on /hige\\z/, name: 'hige', description: 'output hige'
|
172
|
+
env :DEFAULT_HOGE_TEXT1, "DEFAULT_HOGE_TEXT1 desc"
|
173
|
+
env :DEFAULT_HOGE_TEXT2, "DEFAULT_HOGE_TEXT2 desc"
|
174
|
+
|
175
|
+
def hoge(message)
|
176
|
+
# TODO: implement your action
|
177
|
+
end
|
178
|
+
|
179
|
+
def hige(message)
|
180
|
+
# TODO: implement your action
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
EOS
|
187
|
+
}
|
188
|
+
]
|
189
|
+
|
190
|
+
cases.each do |c|
|
191
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
192
|
+
begin
|
193
|
+
case_before c
|
194
|
+
|
195
|
+
# -- given --
|
196
|
+
# nothing
|
197
|
+
|
198
|
+
# -- when --
|
199
|
+
Ruboty::Generator.generate(c[:options])
|
200
|
+
actual_handler = File.open("./ruboty-#{c[:handler]}/#{Ruboty::Generator::RUBOTY_HANDLER_FILE}/#{c[:handler]}.rb", 'r:utf-8', &:read)
|
201
|
+
|
202
|
+
# -- then --
|
203
|
+
expect(actual_handler).to eq(c[:expected_handler])
|
204
|
+
|
205
|
+
c[:actions].each do |action|
|
206
|
+
actuals_action = File.open("./ruboty-#{c[:handler]}/#{Ruboty::Generator::RUBOTY_BASE_FILE}/#{c[:handler]}/actions/#{action}.rb", 'r:utf-8', &:read)
|
207
|
+
puts "expected_action_#{action}"
|
208
|
+
expect(actuals_action).to eq(c[:"expected_action_#{action}"])
|
209
|
+
end
|
210
|
+
ensure
|
211
|
+
case_after c
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def case_before(_c)
|
216
|
+
FileUtils.mkdir_p(tmp) unless Dir.exist? tmp
|
217
|
+
Dir.chdir(tmp)
|
218
|
+
File.open(Ruboty::Generator::RUBOTY_GENERATOR_FILE, 'w:utf-8') do |file|
|
219
|
+
file.puts template
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def case_after(_c)
|
224
|
+
Dir.chdir('../')
|
225
|
+
FileUtils.rm_rf(tmp) if Dir.exist? tmp
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruboty-generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tbpgr
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.18.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.18.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.14.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.14.1
|
69
|
+
description: ruboty-generator generate ruboty plugin template.
|
70
|
+
email:
|
71
|
+
- tbpgr@tbpgr.jp
|
72
|
+
executables:
|
73
|
+
- ruboty-generator
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- .rspec
|
79
|
+
- .rubocop.yml
|
80
|
+
- .travis.yml
|
81
|
+
- CHANGELOG.md
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/ruboty-generator
|
87
|
+
- lib/ruboty/dsl.rb
|
88
|
+
- lib/ruboty/dsl/dsl.rb
|
89
|
+
- lib/ruboty/dsl/dsl_model.rb
|
90
|
+
- lib/ruboty/generator.rb
|
91
|
+
- lib/ruboty/generator/generate.rb
|
92
|
+
- lib/ruboty/generator/version.rb
|
93
|
+
- lib/ruboty/models.rb
|
94
|
+
- lib/ruboty/models/command.rb
|
95
|
+
- lib/ruboty/models/env.rb
|
96
|
+
- ruboty-generator.gemspec
|
97
|
+
- spec/ruboty/generator/generate_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
homepage: https://github.com/tbpgr/ruboty-generator
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.3.0
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: ruboty-generator generate ruboty plugin template.
|
123
|
+
test_files:
|
124
|
+
- spec/ruboty/generator/generate_spec.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
has_rdoc:
|