Class: Enginery::Generator
- Inherits:
-
Object
- Object
- Enginery::Generator
show all
- Includes:
- Helpers
- Defined in:
- lib/enginery/generator.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helpers
#activerecord_associations, #app_config, #app_controllers, #app_models, #boot_app, #controller_exists?, #datamapper_associations, #dst_path, extract_setup, fail, #fail_unless_in_app_folder!, fail_verbosely, #in_app_folder?, #load_boot_rb, #migrations_by_model, #model_exists?, o, parse_input, #routes_by_controller, #sequel_associations, #src_path, #unrootify, #valid_controller?, valid_db_type?, valid_engine?, valid_orm?, #valid_route?, valid_server?, validate_constant_name, #validate_route_name, #view_setups_for
Constructor Details
#initialize(dst_root, setups = {}) ⇒ Generator
Returns a new instance of Generator.
7
8
9
|
# File 'lib/enginery/generator.rb', line 7
def initialize dst_root, setups = {}
@dst_root, @setups = dst_root, setups
end
|
Instance Attribute Details
#dst_root ⇒ Object
Returns the value of attribute dst_root.
5
6
7
|
# File 'lib/enginery/generator.rb', line 5
def dst_root
@dst_root
end
|
#setups ⇒ Object
Returns the value of attribute setups.
5
6
7
|
# File 'lib/enginery/generator.rb', line 5
def setups
@setups
end
|
Instance Method Details
#generate_controller(name) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/enginery/generator.rb', line 55
def generate_controller name
name.nil? || name.empty? && fail("Please provide controller name")
before, ctrl_name, after = namespace_to_source_code(name)
source_code, i = [], INDENT * before.size
before.each {|s| source_code << s}
source_code << "#{i}class #{ctrl_name} < E"
(@setups[:include] || []).each do |mdl|
source_code << "#{i + INDENT}include #{mdl}"
end
source_code << "#{i + INDENT}include %sHelpers if defined?(%sHelpers)" % [ctrl_name, ctrl_name]
source_code << "#{i + INDENT}# controller-wide setups"
if route = setups[:route]
source_code << "#{i + INDENT}map '#{route}'"
end
if engine = setups[:engine]
source_code << "#{i + INDENT}engine :#{engine}"
Configurator.new(dst_root, engine: engine).update_gemfile
end
if format = setups[:format]
source_code << "#{i + INDENT}format '#{format}'"
end
source_code << INDENT
source_code << "#{i}end"
after.each {|s| source_code << s}
path = dst_path(:controllers, class_to_route(name))
file = path + CONTROLLER_SUFFIX
File.exists?(file) && fail('"%s" controller already exists' % name)
o
o '=== Generating "%s" controller ===' % name
o '*** Creating "%s/" ***' % unrootify(path)
FileUtils.mkdir_p(path)
write_file file, source_code.join("\n")
output_source_code source_code
ctrl_name
end
|
#generate_helper(ctrl_name) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/enginery/generator.rb', line 149
def generate_helper ctrl_name
o
o '=== Generating helper file for "%s" controller ===' % ctrl_name
_, ctrl = valid_controller?(ctrl_name)
file = dst_path(:helpers, class_to_route(ctrl_name) + HELPER_SUFFIX)
path = File.dirname(file)
if File.exists?(path)
File.directory?(path) || fail('"%s" should be a directory' % unrootify(path))
else
o '*** Creating "%s/" ***' % unrootify(path)
FileUtils.mkdir_p(path)
end
if File.exists?(file)
File.file?(file) || fail('"%s" should be a file' % unrootify(file))
o('*** WARN: overwriting "%s" file ***' % unrootify(file))
else
o '*** Creating "%s" ***' % unrootify(file)
end
before, helper_name, after = namespace_to_source_code(ctrl_name)
source_code, i = [], INDENT * before.size
before.each {|s| source_code << s}
source_code << "#{i}module #{helper_name}Helpers"
source_code << "#{i + INDENT}include Helpers if defined?(Helpers)"
source_code << ""
source_code << "#{i + INDENT}# helper methods for %s controller" % ctrl_name
source_code << INDENT
source_code << "#{i}end"
after.each {|s| source_code << s}
write_file file, source_code.join("\n")
output_source_code source_code
file
end
|
#generate_model(name) ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/enginery/generator.rb', line 215
def generate_model name
name.nil? || name.empty? && fail("Please provide model name")
before, model_name, after = namespace_to_source_code(name)
superclass, insertions = '', []
if orm = valid_orm?(setups[:orm] || Cfg[:orm])
orm == :ActiveRecord && superclass = ' < ActiveRecord::Base'
orm == :Sequel && superclass = ' < Sequel::Model'
orm == :DataMapper && insertions << 'include DataMapper::Resource'
(@setups[:include] || []).each do |mdl|
insertions << "include #{mdl}"
end
insertions << ''
orm == :DataMapper && insertions << 'property :id, Serial'
insertions << ''
send(orm.to_s.downcase + '_associations', @setups).each do |a|
insertions << a
end
end
insertions << ''
source_code, i = [], INDENT * before.size
before.each {|s| source_code << s}
source_code << "#{i}class #{model_name + superclass}"
insertions.each do |line|
source_code << (i + INDENT + line.to_s)
end
source_code << "#{i}end"
after.each {|s| source_code << s}
source_code = source_code.join("\n")
file = dst_path(:models, class_to_route(name) + '.rb')
File.exists?(file) && fail('"%s" file already exists' % unrootify(file))
o
o '=== Generating "%s" model ===' % name
dir = File.dirname(file)
if File.exists?(dir)
File.directory?(dir) || fail("#{unrootify dir} should be a directory")
else
o '*** Creating "%s/" ***' % unrootify(dir)
FileUtils.mkdir_p(dir)
end
write_file file, source_code
output_source_code source_code.split("\n")
model_name
end
|
#generate_project(name = nil) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/enginery/generator.rb', line 11
def generate_project name = nil
name = name.to_s
if name.empty?
name = '.'
else
name =~ /\.\.|\// && fail('Project name can not contain "/" nor ".."')
@dst_root, @dst_path_map = File.join(@dst_root, name, ''), nil
end
Dir[dst_path(:root, '*')].any? && fail('"%s" should be a empty folder' % dst_path.root)
o
o '=== Generating "%s" project ===' % name
folders, files = Dir[src_path(:base, '**/{*,.[a-z]*}')].partition do |entry|
File.directory?(entry)
end
FileUtils.mkdir_p dst_path.root
o "#{name}/"
folders.each do |folder|
path = unrootify(folder, src_path.base)
o " D #{path}/"
FileUtils.mkdir_p dst_path(:root, path)
end
files.reject {|f| File.basename(f) == '.gitkeep'}.each do |file|
path = unrootify(file, src_path.base)
o " F #{path}"
FileUtils.cp file, dst_path(:root, path)
end
Configurator.new dst_root, setups do
update_gemfile
update_rakefile
update_boot_rb
update_config_yml
update_database_rb
update_database_yml
end
name
end
|
#generate_rear_controller(model) ⇒ Object
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/enginery/generator.rb', line 269
def generate_rear_controller model
model.nil? || model.empty? && fail('Please provide the model name to build controller for')
model_exists?(model) || fail('Seems "%s" model does not exists' % model)
source_code = []
source_code << "defined?(Rear) && Rear.register('#{model}') do"
source_code << " # Rear setups for #{model} model"
source_code << " # Details at https://github.com/espresso/rear"
source_code << " "
source_code << " "
source_code << "end"
source_code << ""
path = dst_path(:rear_controllers, class_to_route(model))
file = path + ADMIN_SUFFIX
File.exists?(file) && fail('A Rear controller for "%s" model already exists' % model)
o
o '=== Generating Rear controller for "%s" model ===' % model
FileUtils.mkdir_p(File.dirname(path))
write_file file, source_code.join("\n")
output_source_code source_code
model
end
|
#generate_route(ctrl_name, name) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/enginery/generator.rb', line 99
def generate_route ctrl_name, name
action_file, action = valid_route?(ctrl_name, name)
File.exists?(action_file) && fail('"%s" route already exists' % name)
before, ctrl_name, after = namespace_to_source_code(ctrl_name)
source_code, i = [], ' ' * before.size
before.each {|s| source_code << s}
source_code << "#{i}class #{ctrl_name}"
source_code << "#{i + INDENT}# action-specific setups"
source_code << ''
if format = setups[:format]
source_code << "#{i + INDENT}format_for :#{action}, '#{format}'"
end
if setups.reject {|k,v| k == :route}.any?
source_code << "#{i + INDENT}before :#{action} do"
if engine = setups[:engine]
source_code << "#{i + INDENT*2}engine :#{engine}"
Configurator.new(dst_root, engine: engine).update_gemfile
end
source_code << "#{i + INDENT}end"
source_code << ""
end
source_code << (i + INDENT + "def #{action}")
action_source_code = ["render"]
if block_given?
action_source_code = yield
action_source_code.is_a?(Array) || action_source_code = [action_source_code]
end
action_source_code.each do |line|
source_code << (i + INDENT*2 + line.to_s)
end
source_code << (i + INDENT + "end")
source_code << ''
source_code << "#{i}end"
after.each {|s| source_code << s}
o
o '=== Generating "%s" route ===' % name
write_file action_file, source_code.join("\n")
output_source_code source_code
action
end
|
#generate_spec(ctrl_name, name) ⇒ Object
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/enginery/generator.rb', line 295
def generate_spec ctrl_name, name
context = {}
_, context[:controller] = valid_controller?(ctrl_name)
_, context[:action] = valid_route?(ctrl_name, name)
context[:spec] = [ctrl_name, context[:action]]*'#'
o
o '=== Generating "%s#%s" spec ===' % [ctrl_name, name]
path = dst_path(:specs, class_to_route(ctrl_name), '/')
if File.exists?(path)
File.directory?(path) || fail("#{path} should be a directory")
else
o '*** Creating "%s" ***' % unrootify(path)
FileUtils.mkdir_p(path)
end
file = path + context[:action] + SPEC_SUFFIX
File.exists?(file) && o('*** WARN: overwriting "%s" file ***' % unrootify(file))
test_framework = setups[:test_framework] || DEFAULT_TEST_FRAMEWORK
engine = Tenjin::Engine.new(path: [src_path.specfiles], cache: false)
source_code = engine.render(test_framework.to_s + '.erb', context)
write_file file, source_code
output_source_code source_code.split("\n")
file
end
|
#generate_view(ctrl_name, name) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/enginery/generator.rb', line 190
def generate_view ctrl_name, name
_, action = valid_route?(ctrl_name, name)
_, ctrl = valid_controller?(ctrl_name)
path, ext = view_setups_for(ctrl, action)
o
o '=== Generating "%s" view ===' % name
if File.exists?(path)
File.directory?(path) || fail('"%s" should be a directory' % unrootify(path))
else
o '*** Creating "%s/" ***' % unrootify(path)
FileUtils.mkdir_p(path)
end
file = File.join(path, action + ext)
if File.exists?(file)
File.file?(file) || fail('"%s" should be a file' % unrootify(file))
o('*** WARN: overwriting "%s" file ***' % unrootify(file))
else
o '*** Creating "%s" ***' % unrootify(file)
end
File.open(file, 'w') {}
file
end
|