nephos-server 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e144905879152af79fc251c1b0b2d3a178dfa066
4
- data.tar.gz: d31444aa6d75c9dec7fe795bef0204067e257d86
3
+ metadata.gz: 9fdc970288b999aeab76422947f72cfbfcba0710
4
+ data.tar.gz: 38042ea637910e98ca204e6119d457b51417c3a0
5
5
  SHA512:
6
- metadata.gz: 25a38d64cd6e7258a5dd2a136bf1024e5521cca2a68ab12c0450737c415fca756f26adf8f2cd905465f4544c5adbb7a3a46cb395e0b1dc70061636c345fb101b
7
- data.tar.gz: 1b061583e821e28d60521770a119b0a0e8cbb4a42fa304fd3d11f362a530b5ecd413db9ad0ae5f1d1d6c16758bbc60379e82c1239c33a4ef321c64696e91266b
6
+ metadata.gz: e95c52c9519e04511f62db2d89197923ebf5bc485bad8997c0facf3142cf423953b010e997345df57ff1db1ff03e73f275fc747ef36a2fd1e2057b4ba48dc858
7
+ data.tar.gz: a8e1e84c5d8f58b0598b5987759ea1e545258977f96a0745f81fdbf1394f23055daa2485cf29501cd7c41ead58f2e19cf62ad0b8fb9df491121984013361ee1c
data/bin/nephos-generator CHANGED
@@ -22,107 +22,141 @@ def raise_invalid_appli
22
22
  end
23
23
  end
24
24
 
25
- def route_exists? line
26
- File.read("routes.rb").split("\n").include? line
27
- end
28
-
29
- def write_route! line
30
- File.open("routes.rb", "a") do |f|
31
- f.puts line
32
- end
33
- puts("Route created: ".green + line)
34
- end
35
-
36
- def generate_route(verb, url, dest_c, dest_m=nil)
37
- raise_invalid_appli
38
- raise InvalidVerb, "\"#{verb}\" doesn't match with /\w+/" if not verb.to_s.match(/^\w+$/)
39
- raise InvalidUrl if not url.match(/^\/?(:?\w+)(\/:?\w+)*\/?$/)
40
- raise "Option dest_m is not avaiable for now" if not dest_m.nil?
41
- raise "Option dest_c must match with \"controller#method\"" if not dest_c.match(/^\w+\#\w+$/)
42
- controller = dest_c.split("#")[0]
43
- method = dest_c.split("#")[1]
44
- line = "add_route \"#{verb}\", url: \"#{url}\", controller: \"#{controller}\", method: \"#{method}\""
45
- if route_exists? line
46
- puts("Route already exists: ".yellow + line)
47
- else
48
- write_route!(line)
49
- end
50
- end
25
+ module Nephos
26
+ module Generator
27
+
28
+ module Route
29
+ def self.exists? line
30
+ File.read("routes.rb").split("\n").include? line
31
+ end
32
+
33
+ def self.write! line
34
+ File.open("routes.rb", "a") do |f|
35
+ f.puts line
36
+ end
37
+ puts("Route created: ".green + line)
38
+ end
39
+
40
+ def self.generate!(verb, url, dest_c, dest_m=nil)
41
+ raise_invalid_appli
42
+ raise InvalidVerb, "\"#{verb}\" doesn't match with /\w+/" if not verb.to_s.match(/^\w+$/)
43
+ raise InvalidUrl if not url.match(/^\/?(:?\w+)(\/:?\w+)*\/?$/)
44
+ raise "Option dest_c must match with \"controller#method\"" if not dest_c.match(/^\w+\#\w+$/) and dest_m.nil?
45
+ raise "Option dest_c must match with \"controllerName\"" if not dest_m.nil? and not dest_c.match(/^\w+$/)
46
+ raise "Option dest_m must match with \"methodName\"" if not dest_m.nil? and not dest_c.match(/^\w+$/)
47
+ controller, method = dest_c, dest_m
48
+ if dest_m.nil?
49
+ controller = dest_c.split("#")[0]
50
+ method = dest_c.split("#")[1]
51
+ end
52
+ line = "add_route \"#{verb}\", url: \"#{url}\", controller: \"#{controller}\", method: \"#{method}\""
53
+ if exists? line
54
+ if $remove
55
+ routes = File.read("routes.rb").split("\n")
56
+ puts "Route deleted: ".green + routes.delete(line).to_s
57
+ File.write("routes.rb", routes.join("\n") + "\n")
58
+ else
59
+ puts("Route already exists: ".yellow + line)
60
+ end
61
+ else
62
+ if $remove
63
+ puts("Route doesn't exists: ".yellow + line)
64
+ else
65
+ write!(line)
66
+ end
67
+ end
68
+ end
69
+ end
51
70
 
52
- def generate_controller(name, file)
53
- raise_invalid_appli
54
- if File.exists? file
55
- print "The file #{file} already exists. Are you sure to erase it ? (y/N)"
56
- r = STDIN.gets.to_s.chomp
57
- raise "File #{file} already exists" unless r.match(/y(es)?/)
58
- end
59
- f = File.open(file, 'w')
60
- f << <<EOF
71
+ module Controller
72
+ def self.generate!(name, file)
73
+ raise_invalid_appli
74
+ if File.exists? file
75
+ print "The file #{file} already exists. Are you sure to erase it ? (y/N)"
76
+ r = STDIN.gets.to_s.chomp
77
+ raise "File #{file} already exists" unless r.match(/y(es)?/)
78
+ end
79
+ f = File.open(file, 'w')
80
+ f << <<EOF
61
81
  class #{name} < Nephos::Controller
62
82
  def root
63
83
  return {plain: "index"}
64
84
  end
65
85
  end
66
86
  EOF
67
- puts "Controller \"#{name}\" created at location \"#{file}\""
68
- end
87
+ puts "Controller \"#{name}\" created at location \"#{file}\""
88
+ end
89
+ end
69
90
 
70
- def create_application_dir dir
71
- raise "Directory #{dir} already exists" if Dir.exists? dir
72
- Dir.mkdir dir
73
- end
91
+ module Application
92
+ def self.create_application_dir dir
93
+ raise "Directory #{dir} already exists" if Dir.exists? dir
94
+ Dir.mkdir dir
95
+ end
96
+
97
+ def self.move_to_application_dir dir
98
+ Dir.chdir dir
99
+ end
100
+
101
+ def self.initialize!
102
+ raise "Not an empty dir" unless Dir[File.expand_path "*"].empty?
103
+ File.write "routes.rb", ROUTE_RB
104
+ File.write "Gemfile", GEMFILE
105
+ Dir.mkdir "app"
106
+ begin
107
+ `git init .`
108
+ puts "Git repository initialized"
109
+ rescue Errno::ENOENT => err
110
+ puts "Warning: git repository not initialized"
111
+ rescue => err
112
+ puts "Error: #{err.message}"
113
+ end
114
+ exec("bundle install")
115
+ end
116
+ end
74
117
 
75
- def move_to_application_dir dir
76
- Dir.chdir dir
77
- end
118
+ def self.main opt
119
+ case opt[0]
120
+ when "c", "controller"
121
+ if opt[1].to_s.match(/[\w\-\.]+/)
122
+ Controller.generate!("#{opt[1].capitalize}Controller", "app/#{opt[1].downcase}.rb")
123
+ else
124
+ puts "error"
125
+ end
126
+ when "a", "appli", "application"
127
+ if not opt[1].to_s.empty?
128
+ Application.create_application_dir(opt[1])
129
+ puts "Application #{opt[1]} created"
130
+ Application.move_to_application_dir(opt[1])
131
+ end
132
+ Application.initialize!
133
+ puts "Application initialized"
134
+ when "r", "route"
135
+ Route.generate!(*(opt[1..4]))
136
+ else
137
+ puts "\"#{opt[0]}\" not recognized has a command"
138
+ end
139
+ end
78
140
 
79
- def initialize_application
80
- raise "Not an empty dir" unless Dir[File.expand_path "*"].empty?
81
- File.write "routes.rb", ROUTE_RB
82
- File.write "Gemfile", GEMFILE
83
- Dir.mkdir "app"
84
- begin
85
- `git init .`
86
- puts "Git repository initialized"
87
- rescue Errno::ENOENT => err
88
- puts "Warning: git repository not initialized"
89
- rescue => err
90
- puts "Error: #{err.message}"
91
141
  end
92
- exec("bundle install")
93
142
  end
94
143
 
95
144
  begin
96
145
  opt = OptionParser.new do |opts|
97
146
  opts.banner = "Usage: nephos-generator [controller name] [appli [name]]"
98
147
 
148
+ opts.on("--rm") do
149
+ $remove = true
150
+ end
151
+
99
152
  opts.on("--debug") do
100
153
  $debug = true
101
154
  end
102
-
103
155
  end.parse!
104
156
 
105
- case opt[0]
106
- when "c", "controller"
107
- if opt[1].to_s.match(/[\w\-\.]+/)
108
- generate_controller("#{opt[1].capitalize}Controller", "app/#{opt[1].downcase}.rb")
109
- else
110
- puts "error"
111
- end
112
- when "a", "appli", "application"
113
- if not opt[1].to_s.empty?
114
- create_application_dir(opt[1])
115
- puts "Application #{opt[1]} created"
116
- move_to_application_dir(opt[1])
117
- end
118
- initialize_application
119
- puts "Application initialized"
120
- when "r", "route"
121
- generate_route(*(opt[1..4]))
122
- else
123
- puts "\"#{opt[0]}\" not recognized has a command"
124
- end
157
+ Nephos::Generator.main(opt)
125
158
 
126
159
  rescue => err
127
- puts "Error: #{err.message}"
160
+ puts "Error:".red + " #{err.message}"
161
+ puts err.backtrace
128
162
  end
data/bin/nephos-server CHANGED
@@ -29,6 +29,7 @@ begin
29
29
  raise InvalidApplication
30
30
  end
31
31
 
32
+ puts "Running Nephos::Server version #{Nephos::VERSION}"
32
33
  Nephos::Server.start($server_port)
33
34
 
34
35
  rescue RoutingError => err
data/bin/nephos-status CHANGED
@@ -3,11 +3,20 @@
3
3
  require 'optparse'
4
4
  require 'nephos-server/bin-helpers'
5
5
 
6
- OptionParser.new do |opts|
6
+ opt = OptionParser.new do |opts|
7
7
  opts.banner = "Usage: nephos-status [appli directory]"
8
+
9
+ opts.on("--rm") do
10
+ $remove = true
11
+ end
12
+
13
+ opts.on("--debug") do
14
+ $debug = true
15
+ end
16
+
8
17
  end.parse!
9
18
 
10
- $dir = ARGV[0] || "."
19
+ $dir = opt[0] || "."
11
20
 
12
21
  begin
13
22
  if Dir.exists? $dir
@@ -19,8 +28,13 @@ begin
19
28
  i = File.read($gfl).split.index("nephos-server") + 1
20
29
  version = File.read($gfl).split[i]
21
30
  path = $gfl.gsub(/Gemfile\.lock$/, "")
31
+ n_controllers = 0
32
+ Dir[File.expand_path 'app/*.rb'].each do |f|
33
+ n_controllers += 1 if File.read(f).include? "< Nephos::Controller"
34
+ end
22
35
  puts "Full path: #{path}"
23
36
  puts "Installed nephos-version: #{version}"
37
+ puts "Controllers (#{n_controllers})"
24
38
  end
25
39
  else
26
40
  raise "\"#{$dir}\" is not a valid directory"
data/lib/nephos-server.rb CHANGED
@@ -13,3 +13,8 @@ require_relative 'nephos-server/controller'
13
13
  require_relative 'nephos-server/router/main'
14
14
  # server
15
15
  require_relative 'nephos-server/server/main'
16
+
17
+ module Nephos
18
+ VERSION_FILE = __FILE__.split("/")[0..-3].join("/") + "/version"
19
+ VERSION = File.read(VERSION_FILE).strip
20
+ end
@@ -31,6 +31,7 @@ test/responder.rb
31
31
  test/router.rb
32
32
  test/params.rb
33
33
  test/controller.rb
34
+ test/generator.rb
34
35
  routes.rb
35
36
  app/dataset.rb
36
37
  app/image.jpg
data/test/generator.rb ADDED
@@ -0,0 +1,47 @@
1
+ class TestNephosServerGenerator < Test::Unit::TestCase
2
+
3
+ def test_generator_application
4
+ # Dir.chdir("/tmp")
5
+ # name = Time.now.to_i.to_s
6
+ # `nephos-server a #{name}`
7
+ end
8
+
9
+ def test_generator_controller
10
+ `rm -f app/test.rb`
11
+
12
+ `nephos-generator --debug c test`
13
+ assert File.exists? "app/test.rb"
14
+ assert_equal "class TestController < Nephos::Controller", File.read("app/test.rb").split("\n").first
15
+ `rm -f app/test.rb`
16
+
17
+ `nephos-generator c test --debug`
18
+ assert File.exists? "app/test.rb"
19
+ assert_equal "class TestController < Nephos::Controller", File.read("app/test.rb").split("\n").first
20
+ `rm -f app/test.rb`
21
+ end
22
+
23
+ # test simple and rm
24
+ def test_generator_route1
25
+ s1 = File.read("routes.rb")
26
+ `./bin/nephos-generator --debug r get test ctr#mth`
27
+ s2 = File.read("routes.rb")
28
+ `./bin/nephos-generator --debug r get test ctr#mth --rm`
29
+ s3 = File.read("routes.rb")
30
+ assert_equal s1, s3
31
+ assert_not_equal s1, s2
32
+ end
33
+
34
+ # test if ctr#mth == ctr mth
35
+ def test_generator_route2
36
+ s1 = File.read("routes.rb")
37
+ `./bin/nephos-generator --debug r get test ctr#mth`
38
+ s2 = File.read("routes.rb")
39
+ `./bin/nephos-generator --debug r get test ctr mth`
40
+ s3 = File.read("routes.rb")
41
+ `./bin/nephos-generator --debug r get test ctr mth --rm`
42
+ s4 = File.read("routes.rb")
43
+ assert_equal s2, s3
44
+ assert_equal s1, s4
45
+ end
46
+
47
+ end
data/version CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nephos-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - poulet_a
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-05 00:00:00.000000000 Z
11
+ date: 2015-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nomorebeer
@@ -78,6 +78,7 @@ email:
78
78
  - test/router.rb
79
79
  - test/params.rb
80
80
  - test/controller.rb
81
+ - test/generator.rb
81
82
  - routes.rb
82
83
  - app/dataset.rb
83
84
  - app/image.jpg
@@ -116,6 +117,7 @@ files:
116
117
  - nephos-server.gemspec
117
118
  - routes.rb
118
119
  - test/controller.rb
120
+ - test/generator.rb
119
121
  - test/params.rb
120
122
  - test/responder.rb
121
123
  - test/router.rb