nekonote-framework 1.0.0.pre.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +49 -0
- data/bin/nekonote +45 -0
- data/data/structure/Gemfile +25 -0
- data/data/structure/config.ru +14 -0
- data/data/structure/handler/base.rb +21 -0
- data/data/structure/handler/error.rb +35 -0
- data/data/structure/handler/welcome.rb +11 -0
- data/data/structure/lib/.gitkeep +0 -0
- data/data/structure/preference/development/logger.yml +62 -0
- data/data/structure/preference/development/middlewares.rb +152 -0
- data/data/structure/preference/development/public.yml +29 -0
- data/data/structure/preference/development/route.yml +30 -0
- data/data/structure/preference/development/route_error.yml +28 -0
- data/data/structure/preference/development/route_include.yml +22 -0
- data/data/structure/preference/development/server/puma.rb +63 -0
- data/data/structure/preference/development/setting/example.yml +40 -0
- data/data/structure/preference/development/setting/site.yml +3 -0
- data/data/structure/preference/development/setting/welcome.yml +7 -0
- data/data/structure/public/css/layout/common.css +11 -0
- data/data/structure/public/css/layout/default.css +3 -0
- data/data/structure/public/css/layout/error.css +3 -0
- data/data/structure/public/css/welcome.css +47 -0
- data/data/structure/public/favicon.ico +0 -0
- data/data/structure/public/img/.gitkeep +0 -0
- data/data/structure/public/img/logo.png +0 -0
- data/data/structure/public/js/.gitkeep +0 -0
- data/data/structure/static/layout/default.tpl +19 -0
- data/data/structure/static/layout/error.tpl +15 -0
- data/data/structure/static/sass/welcome.scss +52 -0
- data/data/structure/static/template/error.tpl +4 -0
- data/data/structure/static/template/welcome/index.tpl +26 -0
- data/data/structure/tmp/pids/.gitkeep +0 -0
- data/lib/loader.rb +83 -0
- data/lib/nekonote.rb +9 -0
- data/lib/nekonote/cli.rb +702 -0
- data/lib/nekonote/cmd_parser.rb +55 -0
- data/lib/nekonote/core.rb +116 -0
- data/lib/nekonote/env.rb +56 -0
- data/lib/nekonote/exception/cli_error.rb +34 -0
- data/lib/nekonote/exception/error.rb +75 -0
- data/lib/nekonote/exception/handler_error.rb +5 -0
- data/lib/nekonote/exception/logger_error.rb +8 -0
- data/lib/nekonote/exception/page_cache_error.rb +6 -0
- data/lib/nekonote/exception/preference_error.rb +11 -0
- data/lib/nekonote/exception/view_error.rb +7 -0
- data/lib/nekonote/handler.rb +274 -0
- data/lib/nekonote/handler/protected_methods.rb +119 -0
- data/lib/nekonote/liquid/tag_env_get.rb +12 -0
- data/lib/nekonote/liquid/tag_setting_get.rb +12 -0
- data/lib/nekonote/logger.rb +135 -0
- data/lib/nekonote/page_cache.rb +111 -0
- data/lib/nekonote/preference.rb +215 -0
- data/lib/nekonote/puma.rb +131 -0
- data/lib/nekonote/rack/rack_static.rb +17 -0
- data/lib/nekonote/rack/rack_static_file.rb +19 -0
- data/lib/nekonote/rack/url_mapper.rb +193 -0
- data/lib/nekonote/rackup.rb +319 -0
- data/lib/nekonote/request.rb +295 -0
- data/lib/nekonote/setting.rb +59 -0
- data/lib/nekonote/spec.rb +22 -0
- data/lib/nekonote/util/filer.rb +69 -0
- data/lib/nekonote/util/process.rb +43 -0
- data/lib/nekonote/view.rb +398 -0
- data/lib/nekonote/yaml_access.rb +60 -0
- metadata +144 -0
data/lib/nekonote.rb
ADDED
data/lib/nekonote/cli.rb
ADDED
@@ -0,0 +1,702 @@
|
|
1
|
+
module Nekonote
|
2
|
+
@@from_cli = true
|
3
|
+
|
4
|
+
class Cli
|
5
|
+
STRING_RELOADABLE_PREFS = %('setting/', 'logger.yml')
|
6
|
+
STRING_CAT_AND_MOUSE = ' . . . - - - - - - /=^x^)=/ - - ~~(=**)='
|
7
|
+
SPACE = ' '
|
8
|
+
|
9
|
+
# @param string cmd
|
10
|
+
# @param string subcmd
|
11
|
+
# @param string val
|
12
|
+
# @param hash options
|
13
|
+
public
|
14
|
+
def initialize(cmd, subcmd=nil, val=nil, options=nil)
|
15
|
+
path_structure = File.expand_path(File.dirname(__FILE__)).split '/'
|
16
|
+
path_structure.pop
|
17
|
+
path_structure.pop
|
18
|
+
@path_structure =%(#{path_structure.join('/')}/data/structure)
|
19
|
+
|
20
|
+
Cli.usage if !cmd.is_a? String
|
21
|
+
subcmd ||= ''
|
22
|
+
val ||= ''
|
23
|
+
options ||= {}
|
24
|
+
|
25
|
+
# set-up properties
|
26
|
+
root = options[:root]
|
27
|
+
root ||= './'
|
28
|
+
root += '/' if !root.end_with? '/'
|
29
|
+
@root = root
|
30
|
+
|
31
|
+
@cmd = cmd.intern
|
32
|
+
@val = val.to_s
|
33
|
+
@options = options
|
34
|
+
|
35
|
+
if subcmd == ''
|
36
|
+
@subcmd = subcmd
|
37
|
+
else
|
38
|
+
@subcmd = subcmd.intern
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# execute task
|
43
|
+
public
|
44
|
+
def exec
|
45
|
+
case @cmd
|
46
|
+
when :new
|
47
|
+
# generate something
|
48
|
+
case @subcmd
|
49
|
+
when :app, :application
|
50
|
+
generate_structure @val
|
51
|
+
|
52
|
+
when :env
|
53
|
+
Nekonote.set_root @root
|
54
|
+
generate_env @val
|
55
|
+
|
56
|
+
when :handler
|
57
|
+
Nekonote.set_root @root
|
58
|
+
generate_handler @val
|
59
|
+
|
60
|
+
when :template, :tpl
|
61
|
+
Nekonote.set_root @root
|
62
|
+
generate_template @val
|
63
|
+
|
64
|
+
when :layout
|
65
|
+
Nekonote.set_root @root
|
66
|
+
generate_layout @val
|
67
|
+
|
68
|
+
when ''
|
69
|
+
# show usage
|
70
|
+
Nekonote::Cli.usage_new
|
71
|
+
|
72
|
+
else
|
73
|
+
raise CLIError, CLIError::MSG_UNKNOWN_AFTER_NEW% @subcmd
|
74
|
+
end
|
75
|
+
|
76
|
+
when :server, :s
|
77
|
+
# control server
|
78
|
+
case @subcmd
|
79
|
+
when :start, :st
|
80
|
+
Nekonote.set_root @root
|
81
|
+
start_server
|
82
|
+
|
83
|
+
when :status, :stop, :halt, :restart, :phased_restart
|
84
|
+
Nekonote.set_root @root
|
85
|
+
ctl_server @subcmd
|
86
|
+
|
87
|
+
when :config, :conf
|
88
|
+
Nekonote.set_root @root
|
89
|
+
disp_server_config
|
90
|
+
|
91
|
+
when ''
|
92
|
+
# show usage
|
93
|
+
Nekonote::Cli.usage_server
|
94
|
+
|
95
|
+
else
|
96
|
+
raise CLIError, CLIError::MSG_UNKNOWN_AFTER_SERVER% @subcmd
|
97
|
+
end
|
98
|
+
|
99
|
+
when :reload_pref, :rp
|
100
|
+
# reload preferences
|
101
|
+
Nekonote.set_root @root
|
102
|
+
reload_preference
|
103
|
+
|
104
|
+
when :page_cache_clear, :pcc
|
105
|
+
# page caches clear
|
106
|
+
Nekonote.set_root @root
|
107
|
+
page_cache_clear
|
108
|
+
|
109
|
+
when :env
|
110
|
+
# disp env
|
111
|
+
disp_env
|
112
|
+
|
113
|
+
when :info
|
114
|
+
# disp information
|
115
|
+
disp_info
|
116
|
+
|
117
|
+
else
|
118
|
+
# wrong sub command
|
119
|
+
raise CLIError, CLIError::MSG_UNKNOWN_SUB_COMMAND% @cmd
|
120
|
+
end
|
121
|
+
|
122
|
+
exit 0
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
def start_server
|
127
|
+
begin
|
128
|
+
puts %(Environment: #{Nekonote.get_env})
|
129
|
+
ctl_server :start
|
130
|
+
|
131
|
+
rescue SystemExit
|
132
|
+
exit 0
|
133
|
+
|
134
|
+
rescue Exception => e
|
135
|
+
CLIError.warning CLIError::MSG_FAILED_START_SERVER
|
136
|
+
raise e
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# @param cmd symbol
|
141
|
+
private
|
142
|
+
def ctl_server(cmd)
|
143
|
+
# selectable only puma so far
|
144
|
+
(::Nekonote::Puma.new Nekonote.get_root, Nekonote.get_env).ctl_server cmd
|
145
|
+
end
|
146
|
+
|
147
|
+
# make temp file to reload preferences
|
148
|
+
private
|
149
|
+
def reload_preference
|
150
|
+
Util::Filer::safe_make_empty_file(Nekonote.get_root_path + Preference::FILE_NAME_FOR_RELOAD)
|
151
|
+
disp_success_message %(Now ready for reloading -> #{STRING_RELOADABLE_PREFS})
|
152
|
+
end
|
153
|
+
|
154
|
+
private
|
155
|
+
def page_cache_clear
|
156
|
+
if PageCache.instance.cache_clear
|
157
|
+
disp_success_message 'Page cache files was removed.'
|
158
|
+
else
|
159
|
+
msg = 'No page cache file found. Nothing to do.'
|
160
|
+
puts msg
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# @param string env_name
|
165
|
+
private
|
166
|
+
def generate_handler(path)
|
167
|
+
stack = get_absolute_path_list path, 'handler'
|
168
|
+
handler_path = stack.pop
|
169
|
+
handler_name = File.basename handler_path
|
170
|
+
handler_path += '.rb'
|
171
|
+
gen_dir_list = stack
|
172
|
+
|
173
|
+
# check whether the handler exists?
|
174
|
+
if File.exists? handler_path
|
175
|
+
raise CLIError, CLIError::MSG_ALREADY_EXISTS_HANDLER% handler_path
|
176
|
+
end
|
177
|
+
|
178
|
+
# get handler class name
|
179
|
+
handler_class_name = ''
|
180
|
+
handler_name.split('_').each do |word|
|
181
|
+
next if word.empty?
|
182
|
+
handler_class_name += word.slice!(0).upcase + word
|
183
|
+
end
|
184
|
+
if handler_class_name == ''
|
185
|
+
raise CLIError, CLIError::MSG_INVALID_HANDLER_NAME% path
|
186
|
+
end
|
187
|
+
handler_class_name += 'Handler'
|
188
|
+
|
189
|
+
# make directories if necessery
|
190
|
+
generate_directory gen_dir_list
|
191
|
+
puts '' if gen_dir_list.size > 0
|
192
|
+
|
193
|
+
# make handler class
|
194
|
+
content = <<EOS
|
195
|
+
class #{handler_class_name} < BaseHandler
|
196
|
+
def index
|
197
|
+
end
|
198
|
+
end
|
199
|
+
EOS
|
200
|
+
|
201
|
+
begin
|
202
|
+
File.open(handler_path, 'a+') do |f|
|
203
|
+
f.print content
|
204
|
+
end
|
205
|
+
rescue => e
|
206
|
+
raise CLIError, CLIError::MSG_FAILED_CREATE_HANDLER% e.message
|
207
|
+
end
|
208
|
+
disp_success_message %(Created a new Handler '#{handler_class_name}', '#{handler_path}')
|
209
|
+
|
210
|
+
disp_note <<EOS
|
211
|
+
In order to use the created Handler, there is a need to configure routes as:
|
212
|
+
|
213
|
+
e.g.
|
214
|
+
#{handler_class_name}:
|
215
|
+
path: /#{handler_name}
|
216
|
+
execute: index
|
217
|
+
EOS
|
218
|
+
end
|
219
|
+
|
220
|
+
# @param string path
|
221
|
+
private
|
222
|
+
def generate_template(path)
|
223
|
+
stack = get_absolute_path_list path, 'static/template'
|
224
|
+
template_path = stack.pop + '.tpl'
|
225
|
+
gen_dir_list = stack
|
226
|
+
|
227
|
+
path = path.sub /\/$/, '' if path.end_with? '/'
|
228
|
+
|
229
|
+
# check whether the template exists?
|
230
|
+
if File.exists? template_path
|
231
|
+
raise CLIError, CLIError::MSG_ALREADY_EXISTS_TEMPLATE% template_path
|
232
|
+
end
|
233
|
+
|
234
|
+
# make directories if necessery
|
235
|
+
generate_directory gen_dir_list
|
236
|
+
puts '' if gen_dir_list.size > 0
|
237
|
+
|
238
|
+
# make template
|
239
|
+
content = %()
|
240
|
+
|
241
|
+
begin
|
242
|
+
File.open(template_path, 'a+') do |f|
|
243
|
+
f.print content
|
244
|
+
end
|
245
|
+
rescue => e
|
246
|
+
raise CLIError, CLIError::MSG_FAILED_CREATE_TEMPLATE% e.message
|
247
|
+
end
|
248
|
+
disp_success_message %(Created a new template '#{template_path}')
|
249
|
+
|
250
|
+
disp_note <<EOS
|
251
|
+
In order to use the template file you created, you need to set 'template' directive in your route as:
|
252
|
+
|
253
|
+
e.g.
|
254
|
+
ExampleHandler
|
255
|
+
path: /example
|
256
|
+
execute: index
|
257
|
+
template: #{path}
|
258
|
+
EOS
|
259
|
+
end
|
260
|
+
|
261
|
+
# @param string path
|
262
|
+
private
|
263
|
+
def generate_layout(path)
|
264
|
+
stack = get_absolute_path_list path, 'static/layout'
|
265
|
+
layout_path = stack.pop + '.tpl'
|
266
|
+
gen_dir_list = stack
|
267
|
+
|
268
|
+
path = path.sub /\/$/, '' if path.end_with? '/'
|
269
|
+
|
270
|
+
# check whether the layout exists?
|
271
|
+
if File.exists? layout_path
|
272
|
+
raise CLIError, CLIError::MSG_ALREADY_EXISTS_LAYOUT% layout_path
|
273
|
+
end
|
274
|
+
|
275
|
+
# make directories if necessery
|
276
|
+
generate_directory gen_dir_list
|
277
|
+
puts '' if gen_dir_list.size > 0
|
278
|
+
|
279
|
+
# make layout
|
280
|
+
content = %()
|
281
|
+
|
282
|
+
begin
|
283
|
+
File.open(layout_path, 'a+') do |f|
|
284
|
+
f.print content
|
285
|
+
end
|
286
|
+
rescue => e
|
287
|
+
raise CLIError, CLIError::MSG_FAILED_CREATE_LAYOUT% e.message
|
288
|
+
end
|
289
|
+
disp_success_message %(Created a new layout '#{layout_path}')
|
290
|
+
|
291
|
+
disp_note <<EOS
|
292
|
+
In order to use the layout file you created, you need to set 'layout' directive in your route as:
|
293
|
+
|
294
|
+
e.g.
|
295
|
+
ExampleHandler
|
296
|
+
path: /example
|
297
|
+
execute: index
|
298
|
+
layout: #{path}
|
299
|
+
EOS
|
300
|
+
end
|
301
|
+
|
302
|
+
# @param string env
|
303
|
+
private
|
304
|
+
def generate_env(env)
|
305
|
+
stack = get_absolute_path_list env, 'preference'
|
306
|
+
if stack.size != 1
|
307
|
+
raise CLIError, CLIError::MSG_INVALID_ENV_NAME% env
|
308
|
+
end
|
309
|
+
env_path = stack.pop
|
310
|
+
|
311
|
+
# check whether the envinronment exists or not
|
312
|
+
if File.exists? env_path
|
313
|
+
raise CLIError, CLIError::MSG_ALREADY_EXISTS_ENV% env_path
|
314
|
+
end
|
315
|
+
|
316
|
+
# get base path
|
317
|
+
base_path = get_base_path 'preference/development'
|
318
|
+
|
319
|
+
# copy
|
320
|
+
begin
|
321
|
+
FileUtils.cp_r base_path, env_path
|
322
|
+
rescue => e
|
323
|
+
msg = 'Failed to create a new envinroment by the following reason.' + $/
|
324
|
+
msg += e.message
|
325
|
+
raise CLIError, msg
|
326
|
+
end
|
327
|
+
|
328
|
+
# replace
|
329
|
+
replace_puma_config Nekonote.get_root, env
|
330
|
+
|
331
|
+
# display message
|
332
|
+
disp_success_message %(Created a new environment '#{env}' on the application #{Nekonote.get_root})
|
333
|
+
disp_note <<EOS
|
334
|
+
In order to use the environment you created, you need to set '#{env}' to a shell variable called NEKONOTE_ENV.
|
335
|
+
EOS
|
336
|
+
end
|
337
|
+
|
338
|
+
# @param string app_name
|
339
|
+
private
|
340
|
+
def generate_structure(app_name)
|
341
|
+
# validation
|
342
|
+
option = {:allow_first_slash => true}
|
343
|
+
check_new_name app_name, option
|
344
|
+
|
345
|
+
base_dir = File.expand_path File.dirname(app_name)
|
346
|
+
name = File.basename app_name
|
347
|
+
|
348
|
+
# is it possible to make application structure in base_dir?
|
349
|
+
if !Util::Filer.available_dir? base_dir
|
350
|
+
raise CLIError, CLIError::MSG_PERMIT_MAKE_DIR% base_dir
|
351
|
+
end
|
352
|
+
|
353
|
+
# application root
|
354
|
+
app_root = base_dir + '/' + name
|
355
|
+
|
356
|
+
# check whether there is something or not?
|
357
|
+
if File.exist? app_root
|
358
|
+
raise CLIError, CLIError::MSG_ALREADY_EXISTS_APP% app_root
|
359
|
+
end
|
360
|
+
|
361
|
+
# check whether there is the directory for generating structure
|
362
|
+
if !File.directory? @path_structure
|
363
|
+
raise CLIError, CLIError::MSG_MISSING_STRUCTURE_DIR% @path_structure
|
364
|
+
end
|
365
|
+
|
366
|
+
# generate apps root dir
|
367
|
+
Dir.mkdir app_root
|
368
|
+
|
369
|
+
# generate files and directories
|
370
|
+
cp_from = @path_structure + '/'
|
371
|
+
files = ['Gemfile', 'config.ru']
|
372
|
+
dirs = ['handler', 'lib', 'preference', 'public', 'static', 'tmp']
|
373
|
+
free_dirs_to_make = ['cache', 'log']
|
374
|
+
|
375
|
+
# copy files
|
376
|
+
files.each do |name|
|
377
|
+
file_path = "#{app_root}/#{name}"
|
378
|
+
FileUtils.cp cp_from + name, file_path
|
379
|
+
CE.once.fg(:green).tx(:bold)
|
380
|
+
puts SPACE + "* Created a file -> #{file_path}"
|
381
|
+
end
|
382
|
+
|
383
|
+
# copy directories
|
384
|
+
dirs.each do |name|
|
385
|
+
dir_path = "#{app_root}/#{name}"
|
386
|
+
FileUtils.cp_r cp_from + name, dir_path
|
387
|
+
CE.once.fg(:yellow).tx(:bold)
|
388
|
+
puts SPACE + "* Created a directory -> #{dir_path}"
|
389
|
+
|
390
|
+
files = Dir[dir_path + '/**/*']
|
391
|
+
files.each do |path|
|
392
|
+
if File.directory? path
|
393
|
+
CE.once.fg :yellow
|
394
|
+
puts SPACE + " Created a directory -> #{path}"
|
395
|
+
else
|
396
|
+
CE.once.fg :green
|
397
|
+
puts SPACE + " Created a file -> #{path}"
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
# replace
|
403
|
+
env = 'development'
|
404
|
+
replace_puma_config app_root, env
|
405
|
+
replace_gemfile app_root, Nekonote::VERSION
|
406
|
+
|
407
|
+
# change the permissions
|
408
|
+
puts ''
|
409
|
+
free_dirs_to_make.each do |name|
|
410
|
+
file_path = "#{app_root}/#{name}"
|
411
|
+
Dir.mkdir file_path
|
412
|
+
FileUtils.chmod 0777, file_path
|
413
|
+
CE.once.fg(:yellow).tx(:bold)
|
414
|
+
puts SPACE + "* Created a directory (0777) -> #{file_path}"
|
415
|
+
|
416
|
+
keep_file = "#{file_path}/.gitkeep"
|
417
|
+
File.open keep_file, 'w' do end
|
418
|
+
CE.once.fg :green
|
419
|
+
puts SPACE + " Created a file -> #{keep_file}"
|
420
|
+
end
|
421
|
+
|
422
|
+
# display success messages
|
423
|
+
puts ''
|
424
|
+
disp_success_message "Generated a new application -> #{app_root}"
|
425
|
+
|
426
|
+
# display the note
|
427
|
+
msg = <<EOS
|
428
|
+
The generated application will be published on TCP port 2002 after starting the web server by
|
429
|
+
'NEKONOTE_ENV=#{env} nekonote server start --root #{app_root}' or 'cd #{app_root} && NEKONOTE_ENV=#{env} nekonote server start'
|
430
|
+
The web server will accpet accessing from any IP addresses.
|
431
|
+
EOS
|
432
|
+
disp_note msg, '1'
|
433
|
+
|
434
|
+
msg = <<EOS
|
435
|
+
It's possible to configure preferences for the web server by the configuration file located in the structure you just generated.
|
436
|
+
You can display the path to the configuration file by typing 'NEKONOTE_ENV=#{env} nekonote server conf --root #{app_root}'
|
437
|
+
EOS
|
438
|
+
disp_note msg, '2'
|
439
|
+
|
440
|
+
msg = <<EOS
|
441
|
+
The web server is deamonized by default. You can see the help for controlling the web server just typing 'nekonote server'.
|
442
|
+
In order to start the web server, Please typing 'NEKONOTE_ENV=#{env} nekonote server start --root #{app_root}'
|
443
|
+
As of now you have just one environment named '#{env}'.
|
444
|
+
When the shell variable NEKONOTE_ENV is set on your shell, you don't need to place NEKONOTE_ENV=#{env} to the beginning of 'nekonote' command.
|
445
|
+
EOS
|
446
|
+
disp_note msg, '3'
|
447
|
+
|
448
|
+
msg = <<EOS
|
449
|
+
Have you had installed the dependent libraries?
|
450
|
+
If you didn't yet, please install them by 'Bundler' like following:
|
451
|
+
|
452
|
+
* bundle install
|
453
|
+
Installing dependencies into all system gems.
|
454
|
+
|
455
|
+
* bundle install --path vendor/bundle
|
456
|
+
Installing into a specific directory in your application structure.
|
457
|
+
EOS
|
458
|
+
disp_note msg, '4'
|
459
|
+
|
460
|
+
puts STRING_CAT_AND_MOUSE + $/ + $/
|
461
|
+
end
|
462
|
+
|
463
|
+
# @param string app_root
|
464
|
+
# @param string env
|
465
|
+
private
|
466
|
+
def replace_puma_config(app_root, env)
|
467
|
+
after = ''
|
468
|
+
config_path = "#{app_root}/preference/#{env}/server/puma.rb"
|
469
|
+
File.open(config_path, 'r+') do |f|
|
470
|
+
after = f.read.sub 'REPLACE_ME_TO_APP_ROOT', app_root
|
471
|
+
f.truncate 0
|
472
|
+
end
|
473
|
+
File.open(config_path, 'r+') do |f|
|
474
|
+
f.print after
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
# @param string app_root
|
479
|
+
# @param string env
|
480
|
+
private
|
481
|
+
def replace_gemfile(app_root, version)
|
482
|
+
after = ''
|
483
|
+
gemfile_path = "#{app_root}/Gemfile"
|
484
|
+
File.open(gemfile_path, 'r+') do |f|
|
485
|
+
after = f.read.sub 'REPLACE_ME_TO_NEKONOTE_VERSION', version
|
486
|
+
f.truncate 0
|
487
|
+
end
|
488
|
+
File.open(gemfile_path, 'r+') do |f|
|
489
|
+
f.print after
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
# @param string path
|
494
|
+
# @param string dest
|
495
|
+
# @return array
|
496
|
+
private
|
497
|
+
def get_absolute_path_list(path, dest)
|
498
|
+
# validation
|
499
|
+
check_new_name path
|
500
|
+
|
501
|
+
# check if there is the directory to generate new handler
|
502
|
+
base_path = Nekonote.get_root_path + dest
|
503
|
+
if !File.directory? base_path
|
504
|
+
raise CLIError, CLIError::MSG_MISSING_DEST_PATH% base_path
|
505
|
+
end
|
506
|
+
base_path += '/'
|
507
|
+
|
508
|
+
# perse handler name and its path
|
509
|
+
stack = []
|
510
|
+
dir = base_path
|
511
|
+
path.split('/').each do |name|
|
512
|
+
stack << dir + name
|
513
|
+
dir += name + '/'
|
514
|
+
end
|
515
|
+
|
516
|
+
if stack.size == 0
|
517
|
+
raise CLIError, CLIError::MSG_INVALID_NEW_NAME% path
|
518
|
+
end
|
519
|
+
|
520
|
+
return stack
|
521
|
+
end
|
522
|
+
|
523
|
+
# @param string path path to base that without starting with slash
|
524
|
+
private
|
525
|
+
def get_base_path(path = '')
|
526
|
+
# check there is the directory for generating structure
|
527
|
+
if !File.directory? @path_structure
|
528
|
+
raise CLIError, CLIError::MSG_MISSING_STRUCTURE_DIR% @path_structure
|
529
|
+
end
|
530
|
+
|
531
|
+
# check there is the base-directory for generating new env
|
532
|
+
base_path = @path_structure + '/' + path
|
533
|
+
if !File.directory? base_path
|
534
|
+
raise CLIError, CLIError::MSG_MISSING_STRUCTURE_DIR% base_path
|
535
|
+
end
|
536
|
+
|
537
|
+
return base_path
|
538
|
+
end
|
539
|
+
|
540
|
+
# @param array stack
|
541
|
+
private
|
542
|
+
def generate_directory(stack)
|
543
|
+
stack.each do |dir|
|
544
|
+
if File.directory? dir
|
545
|
+
# nothing to do
|
546
|
+
elsif File.exist? dir
|
547
|
+
raise CLIError, CLIError::MSG_FAILED_GEN_DIR_BY_FILE% dir
|
548
|
+
else
|
549
|
+
Dir.mkdir dir
|
550
|
+
CE.once.fg :yellow
|
551
|
+
puts SPACE + '* Created a directory -> ' + dir
|
552
|
+
end
|
553
|
+
end
|
554
|
+
end
|
555
|
+
|
556
|
+
# @param string name
|
557
|
+
# @param hash option
|
558
|
+
# @throw CLIError
|
559
|
+
# @return void
|
560
|
+
private
|
561
|
+
def check_new_name(name, option = {})
|
562
|
+
if name == ''
|
563
|
+
raise CLIError, CLIError::MSG_MISSING_NEW_PATH
|
564
|
+
end
|
565
|
+
|
566
|
+
option = {} if !option.is_a? Hash
|
567
|
+
if name =~ /^[^0-9a-zA-Z]/
|
568
|
+
if !option[:allow_first_slash]
|
569
|
+
raise CLIError, CLIError::MSG_INVALID_NEW_NAME% name
|
570
|
+
end
|
571
|
+
end
|
572
|
+
end
|
573
|
+
|
574
|
+
# @param string msg
|
575
|
+
private
|
576
|
+
def disp_success_message(msg)
|
577
|
+
CE.times(2).fg :cyan
|
578
|
+
puts SPACE + 'Success!'
|
579
|
+
puts SPACE + ' ' + msg + $/
|
580
|
+
end
|
581
|
+
|
582
|
+
# @param string msg
|
583
|
+
# @param string|nil
|
584
|
+
private
|
585
|
+
def disp_note(msg, num = nil)
|
586
|
+
puts ''
|
587
|
+
CE.once.fg(:white).tx(:bold)
|
588
|
+
if num.is_a? String
|
589
|
+
header = %(Note #{num}:)
|
590
|
+
else
|
591
|
+
header = %(Note:)
|
592
|
+
end
|
593
|
+
puts SPACE + header
|
594
|
+
|
595
|
+
CE.fg(:white)
|
596
|
+
msg.lines do |line|
|
597
|
+
puts SPACE + ' ' + line
|
598
|
+
end
|
599
|
+
CE.off
|
600
|
+
puts ''
|
601
|
+
end
|
602
|
+
|
603
|
+
# disp information
|
604
|
+
private
|
605
|
+
def disp_info
|
606
|
+
env = Nekonote.has_valid_env? ? Nekonote.get_env : 'Environment is not set. Please set NEKONOTE_ENV.'
|
607
|
+
puts %(Version: #{VERSION})
|
608
|
+
puts %(Current Environment: #{env})
|
609
|
+
puts $/ + STRING_CAT_AND_MOUSE + $/ + $/
|
610
|
+
end
|
611
|
+
|
612
|
+
# disp current environment
|
613
|
+
private
|
614
|
+
def disp_env
|
615
|
+
begin
|
616
|
+
env = Nekonote.get_env
|
617
|
+
rescue Nekonote::Error
|
618
|
+
env = nil
|
619
|
+
end
|
620
|
+
|
621
|
+
if env != nil
|
622
|
+
puts env
|
623
|
+
else
|
624
|
+
puts 'Missing Environment:'
|
625
|
+
disp_note <<EOS
|
626
|
+
You haven't set any environment now!
|
627
|
+
You need to set some environment for your application by declaring a shell variable called NEKONOTE_ENV.
|
628
|
+
EOS
|
629
|
+
end
|
630
|
+
end
|
631
|
+
|
632
|
+
private
|
633
|
+
def disp_server_config
|
634
|
+
puma = ::Nekonote::Puma.new Nekonote.get_root, Nekonote.get_env
|
635
|
+
puts puma.get_config_path
|
636
|
+
end
|
637
|
+
|
638
|
+
# ----------------
|
639
|
+
# class methods
|
640
|
+
# ----------------
|
641
|
+
# output the version
|
642
|
+
def self.version(status = 0)
|
643
|
+
puts VERSION
|
644
|
+
exit status if status != nil
|
645
|
+
end
|
646
|
+
|
647
|
+
# out put the usage for nekonote new
|
648
|
+
def self.usage_new
|
649
|
+
puts <<-EOS
|
650
|
+
[Usage]
|
651
|
+
nekonote new <sub_command> <value>
|
652
|
+
|
653
|
+
[Sub Commands]
|
654
|
+
application, app <application_name> Generates a new nekonote application structure.
|
655
|
+
env <environment_name> Create a new environment for an existing application structure.
|
656
|
+
handler <handler_name> Create a new handler for an existing application structure. You may pass a relative path.
|
657
|
+
template, tpl <template_name> Create a new template file for an existing application structure. You may pass a relative path.
|
658
|
+
layout <layout_name> Create a new layout file for an existing application structure. You may pass a relative path.
|
659
|
+
EOS
|
660
|
+
end
|
661
|
+
|
662
|
+
# out put the usage for nekonote server
|
663
|
+
def self.usage_server
|
664
|
+
puts <<-EOS
|
665
|
+
[Usage]
|
666
|
+
nekonote server, s <sub_command>
|
667
|
+
|
668
|
+
[Sub Commands]
|
669
|
+
start, st Starts the web server.
|
670
|
+
stop Stop the web server.
|
671
|
+
halt Halts the web server.
|
672
|
+
restart Restarts the web server.
|
673
|
+
phased_restart Reqests phased restart to puma web server.
|
674
|
+
status Displays status of the web server.
|
675
|
+
config, conf Displays a path to the configuration file.
|
676
|
+
EOS
|
677
|
+
end
|
678
|
+
|
679
|
+
# output the usage
|
680
|
+
def self.usage
|
681
|
+
puts <<-EOS
|
682
|
+
[Usage]
|
683
|
+
nekonote [options] <command> [<sub_command> [<value>]]
|
684
|
+
|
685
|
+
[Commands]
|
686
|
+
new Creates something. You can see the details by just typing 'nekonote new'.
|
687
|
+
server, s Controls the web server. You can see the details by just typing 'nekonote server'.
|
688
|
+
reload_pref, rp Reloads preferences without restarting or reloading server. Only #{STRING_RELOADABLE_PREFS} are reloadable.
|
689
|
+
page_cache_clear, pcc Removes page cache files in the 'cache' directory.
|
690
|
+
env Displays the current application environment.
|
691
|
+
info Displays some information.
|
692
|
+
|
693
|
+
[Options]
|
694
|
+
-v, --version Shows the version of your Nekonote Framework.
|
695
|
+
-h, --help Shows this information.
|
696
|
+
--root <path> Declares an application root. The default is your current directory.
|
697
|
+
EOS
|
698
|
+
|
699
|
+
exit 0
|
700
|
+
end
|
701
|
+
end
|
702
|
+
end
|