nyara 0.1.pre.0 → 0.1.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/bin/nyara +2 -14
  3. data/changes +3 -0
  4. data/example/factorial.rb +19 -0
  5. data/example/hello.rb +5 -0
  6. data/example/project.rb +11 -0
  7. data/example/stream.rb +14 -0
  8. data/ext/extconf.rb +19 -0
  9. data/ext/hashes.c +160 -57
  10. data/ext/inc/ary_intern.h +36 -0
  11. data/ext/route.cc +2 -1
  12. data/ext/url_encoded.c +1 -0
  13. data/lib/nyara.rb +1 -0
  14. data/lib/nyara/command.rb +60 -79
  15. data/lib/nyara/config.rb +19 -1
  16. data/lib/nyara/controller.rb +64 -7
  17. data/lib/nyara/hashes/config_hash.rb +3 -20
  18. data/lib/nyara/hashes/header_hash.rb +2 -2
  19. data/lib/nyara/hashes/param_hash.rb +1 -0
  20. data/lib/nyara/nyara.rb +45 -37
  21. data/lib/nyara/part.rb +2 -2
  22. data/lib/nyara/reload.rb +63 -64
  23. data/lib/nyara/route.rb +7 -6
  24. data/lib/nyara/session.rb +4 -0
  25. data/lib/nyara/templates/{Gemfile → Gemfile.tt} +4 -0
  26. data/lib/nyara/templates/Linnerfile +28 -0
  27. data/lib/nyara/templates/Rakefile +16 -2
  28. data/lib/nyara/templates/app/assets/files/favicon.ico +1 -0
  29. data/lib/nyara/templates/app/assets/files/robots.txt +5 -0
  30. data/lib/nyara/templates/app/assets/scripts/app.coffee +4 -0
  31. data/lib/nyara/templates/app/assets/scripts/module-example.coffee +4 -0
  32. data/lib/nyara/templates/app/assets/styles/app.scss +2 -0
  33. data/lib/nyara/templates/app/controllers/application_controller.rb +8 -0
  34. data/lib/nyara/templates/app/views/layouts/application.erb.tt +12 -0
  35. data/lib/nyara/templates/config/application.rb +4 -0
  36. data/lib/nyara/templates/config/{database.yml → database.yml.tt} +3 -3
  37. data/lib/nyara/templates/config/production.rb +2 -0
  38. data/lib/nyara/view.rb +6 -2
  39. data/nyara.gemspec +3 -1
  40. data/rakefile +7 -26
  41. data/spec/apps/reload.rb +35 -0
  42. data/spec/command_spec.rb +24 -10
  43. data/spec/config_spec.rb +19 -8
  44. data/spec/controller_spec.rb +14 -0
  45. data/spec/evented_io_spec.rb +3 -1
  46. data/spec/ext_route_spec.rb +25 -3
  47. data/spec/hashes_spec.rb +45 -21
  48. data/spec/integration_spec.rb +28 -2
  49. data/spec/path_helper_spec.rb +7 -0
  50. data/spec/performance_spec.rb +1 -1
  51. data/spec/public/test.css +1 -0
  52. data/{lib/nyara/templates/public/robot.txt → spec/public/test.jpg} +0 -0
  53. data/spec/public/test.js +1 -0
  54. data/spec/reload_spec.rb +50 -0
  55. data/spec/route_spec.rb +4 -4
  56. data/spec/spec_helper.rb +15 -9
  57. data/spec/url_encoded_spec.rb +5 -0
  58. data/spec/view_spec.rb +7 -0
  59. data/spec/views/_partial.slim +1 -1
  60. data/tools/bug.rb +53 -0
  61. data/tools/hello.rb +49 -0
  62. data/tools/memcheck.rb +33 -0
  63. metadata +73 -41
  64. data/ext/inc/status_codes.inc +0 -64
  65. data/lib/nyara/templates/app/views/layouts/application.erb +0 -12
  66. data/lib/nyara/templates/public/css/app.css +0 -1
  67. data/lib/nyara/templates/public/js/app.js +0 -1
@@ -0,0 +1 @@
1
+ favicon.ico
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
@@ -0,0 +1,4 @@
1
+ # Linner let you organize script assets more easily.
2
+ # see module-example.coffee for how to define a module
3
+ module = require 'module-example'
4
+ module.bounce()
@@ -0,0 +1,4 @@
1
+ # see app.coffee for how to use a module
2
+ module.exports =
3
+ bounce: ->
4
+ console.log 'bounce from module example'
@@ -0,0 +1,2 @@
1
+ // You may:
2
+ // @import "compass"
@@ -1,3 +1,11 @@
1
1
  class ApplicationController < Nyara::Controller
2
2
  set_default_layout 'layouts/application'
3
+
4
+ def asset_path(path)
5
+ if Nyara.production?
6
+ Nyara::Config['manifest'][path]
7
+ else
8
+ path
9
+ end
10
+ end
3
11
  end
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
5
+ <title><%= @app_name %></title>
6
+ <link type="text/css" rel="stylesheet" href="<%%= asset_path("/assets/app.css") %>">
7
+ <script type="text/javascript" src="<%%= asset_path("/assets/app.js") %>"></script>
8
+ </head>
9
+ <body>
10
+ <%%== yield %>
11
+ </body>
12
+ </html>
@@ -5,6 +5,8 @@ configure do
5
5
  set :env, ENV['NYARA_ENV'] || 'development'
6
6
 
7
7
  set :views, 'app/views'
8
+
9
+ set :assets, "app/assets"
8
10
 
9
11
  set :session, :name, '_aaa'
10
12
 
@@ -14,6 +16,8 @@ configure do
14
16
  ## Default session expires when browser closes.
15
17
  ## If you need time-based expiration, 30 minutes for example:
16
18
  # set :session, :expires, 30 * 60
19
+
20
+ set 'session', 'key', File.read(project_path 'config/session.key')
17
21
 
18
22
  # Routing
19
23
  map '/', 'welcome'
@@ -8,7 +8,7 @@ development:
8
8
  default:
9
9
  hosts:
10
10
  - 127.0.0.1:27017
11
- database: '<%= app_name %>_dev'
11
+ database: '<%= @app_name %>_dev'
12
12
 
13
13
  test:
14
14
  <<: *defaults
@@ -16,7 +16,7 @@ test:
16
16
  default:
17
17
  hosts:
18
18
  - 127.0.0.1:27017
19
- database: '<%= app_name %>_test'
19
+ database: '<%= @app_name %>_test'
20
20
 
21
21
  # set these environment variables on your prod server
22
22
  production:
@@ -25,4 +25,4 @@ production:
25
25
  default:
26
26
  hosts:
27
27
  - 127.0.0.1:27017
28
- database: '<%= app_name %>'
28
+ database: '<%= @app_name %>'
@@ -4,5 +4,7 @@ configure do
4
4
  # set :workers, 4
5
5
  set :logger, true
6
6
 
7
+ set :manifest, (YAML.load_file Nyara.project_path 'public/manifest.yml')
8
+
7
9
  # todo after_fork
8
10
  end
data/lib/nyara/view.rb CHANGED
@@ -171,8 +171,7 @@ module Nyara
171
171
  RENDER[meth] = Renderable.make_render_method path, 0, sig, src
172
172
  else
173
173
  t = Dir.chdir @root do
174
- # todo display template error
175
- Tilt.new path rescue return
174
+ Tilt.new path
176
175
  end
177
176
  # partly precompiled
178
177
  RENDER[meth] = TiltRenderable.new t
@@ -210,6 +209,7 @@ module Nyara
210
209
  #
211
210
  # `[meth_obj, ext_without_dot]`
212
211
  def template path, locals={}
212
+ raw_path = path
213
213
  if File.extname(path).empty?
214
214
  Dir.chdir @root do
215
215
  paths = Dir.glob("#{path}.{#@ext_list}")
@@ -220,6 +220,10 @@ module Nyara
220
220
  end
221
221
  end
222
222
 
223
+ if path.blank?
224
+ raise ArgumentError, "template '#{raw_path}' file not found in view dir."
225
+ end
226
+
223
227
  meth = path2meth path
224
228
  ext = @meth2ext[meth]
225
229
  return [RENDER[meth], ext] if ext
data/nyara.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "nyara"
3
- s.version = "0.1.pre.0"
3
+ s.version = "0.1.pre.1"
4
4
  s.author = "Zete Lui"
5
5
  s.email = "nobody@example.com"
6
6
  s.homepage = "https://github.com/luikore/nyara"
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.rubygems_version = '2.0.3'
23
23
  s.add_runtime_dependency 'tilt', '>= 1.3'
24
24
  s.add_runtime_dependency 'listen', '>= 1.1.3'
25
+ s.add_runtime_dependency 'thor', '>= 0.18'
25
26
 
26
27
  s.rdoc_options += %w[
27
28
  -v
@@ -33,5 +34,6 @@ Gem::Specification.new do |s|
33
34
  -x ext/inc/.*
34
35
  -x .*\.o
35
36
  -x .*\.bundle
37
+ -x .*\.so
36
38
  ]
37
39
  end
data/rakefile CHANGED
@@ -3,13 +3,9 @@ require "rake"
3
3
  Dir.chdir __dir__
4
4
 
5
5
  status_file = "ext/inc/status_codes.h"
6
- version_file = "ext/inc/version.inc"
7
6
  makefile = "ext/Makefile"
8
7
  extconf = "ext/extconf.rb"
9
8
 
10
- desc "code generate"
11
- task :gen => [version_file]
12
-
13
9
  desc "generate #{status_file}"
14
10
  file status_file => __FILE__ do
15
11
  puts "generating: #{status_file}"
@@ -27,25 +23,8 @@ file status_file => __FILE__ do
27
23
  f.close
28
24
  end
29
25
 
30
- desc "generate #{version_file}"
31
- file version_file => ['nyara.gemspec', __FILE__] do
32
- puts "generating: #{version_file}"
33
- lines = File.readlines('nyara.gemspec')
34
- version = nil
35
- lines.each do |line|
36
- if line =~ /s\.version =/
37
- version = line[/\d+(\.\d+)*(\.pre\.\d+)?/]
38
- break
39
- end
40
- end
41
- abort 'version not found' unless version
42
- File.open version_file, 'w' do |f|
43
- f.puts %Q{#define NYARA_VERSION "#{version}"}
44
- end
45
- end
46
-
47
26
  desc "generate makefile"
48
- file makefile => extconf do
27
+ file makefile => [extconf, 'nyara.gemspec', __FILE__] do
49
28
  Dir.chdir 'ext' do
50
29
  sh 'make clean' if File.exist?('Makefile')
51
30
  sh 'ruby extconf.rb'
@@ -65,16 +44,18 @@ task :test => :build do
65
44
  end
66
45
 
67
46
  desc "build and test"
68
- task :default => [:gen, :test]
47
+ task :default => :test
69
48
 
70
49
  desc "build and install gem"
71
- task :gem => 'gen' do
50
+ task :gem do
72
51
  Dir.glob('*.gem') do |f|
73
52
  sh 'rm', f
74
53
  end
54
+ # we need to run gem command without the mess of bundler
55
+ ENV['RUBYOPT'] &&= ENV['RUBYOPT'].gsub /\S*bundler\S*/, ''
75
56
  sh 'gem', 'build', 'nyara.gemspec'
76
57
  gem_package = Dir.glob('*.gem').first
77
- sh 'gem', 'install', '--rdoc', '--ri', gem_package
58
+ sh 'gem', 'install', '-l', '--rdoc', '--ri', gem_package
78
59
  end
79
60
 
80
61
  desc "clean"
@@ -146,7 +127,7 @@ end
146
127
  desc "audit arity of rb_define_method/rb_define_singleton_method"
147
128
  task :audit_arity do
148
129
  Dir.glob 'ext/*.{c,cc}' do |f|
149
- puts "validatign #{f}"
130
+ puts "validating #{f}"
150
131
  arities = {}
151
132
  data = File.read f
152
133
  data.scan /^(?:static )?VALUE (\w+)\((.+)\)/ do |func, params|
@@ -0,0 +1,35 @@
1
+ # reload views and files
2
+
3
+ require_relative "../../lib/nyara"
4
+
5
+ if ENV['RELOAD_ROOT']
6
+ configure do
7
+ set :logger, false
8
+ end
9
+ else
10
+ puts "running outside spec, ENV['RELOAD_ROOT'] is nil, creating root dir"
11
+ require 'tmpdir'
12
+ require 'pry'
13
+ ENV['RELOAD_ROOT'] = Dir.mktmpdir 'root'
14
+ Dir.mkdir ENV['RELOAD_ROOT'] + '/views'
15
+ File.open ENV['RELOAD_ROOT'] + '/reloadee.rb', 'w' do |f|
16
+ f << "RELOADEE = 1"
17
+ end
18
+ File.open ENV['RELOAD_ROOT'] + '/views/index.slim', 'w' do |f|
19
+ f << "== 1"
20
+ end
21
+ end
22
+
23
+ configure do
24
+ set :port, 3004
25
+ set :root, ENV['RELOAD_ROOT']
26
+ set :app_files, 'reloadee.rb'
27
+ end
28
+
29
+ get '/views' do
30
+ render 'index'
31
+ end
32
+
33
+ get '/app' do
34
+ send_string RELOADEE
35
+ end
data/spec/command_spec.rb CHANGED
@@ -2,34 +2,48 @@ require_relative "spec_helper"
2
2
 
3
3
  module Nyara
4
4
  describe Command do
5
- it "#help" do
6
- assert_include(stdout { Nyara::Command.help }, "Usage")
5
+ before :each do
6
+ @command = Command.new
7
7
  end
8
8
 
9
9
  it "#version" do
10
- assert_equal(stdout { Nyara::Command.version }.strip, "Nyara #{Nyara::VERSION}")
10
+ assert_equal(capture(:stdout) { @command.version }.strip, "Nyara #{Nyara::VERSION}")
11
11
  end
12
12
 
13
- describe "#new_project" do
14
- before :all do
13
+ it "#generate" do
14
+ pending
15
+ end
16
+
17
+ it "#server" do
18
+ pending
19
+ end
20
+
21
+ it "#console" do
22
+ pending
23
+ end
24
+
25
+ describe "#new" do
26
+ before :each do
27
+ GC.stress = false
15
28
  @tmp_dir = Dir.mktmpdir 'nyara'
16
29
  @old_dir = File.dirname __dir__
17
30
  FileUtils.mkdir_p(@tmp_dir)
18
31
  @app_name = "app_#{Time.now.to_i}"
19
- @stdout = stdout do
20
- Dir.chdir @tmp_dir do
21
- Nyara::Command.new_project(@app_name)
32
+ Dir.chdir @tmp_dir do
33
+ @stdout = capture(:stdout) do
34
+ @command = Command.new
35
+ @command.new(@app_name)
22
36
  end
23
37
  end
24
38
  end
25
39
 
26
- after :all do
40
+ after :each do
27
41
  FileUtils.rm_rf(@tmp_dir)
28
42
  end
29
43
 
30
44
  describe "should create app dir" do
31
45
  it "should run finish" do
32
- assert_include(@stdout, "Enjoy!")
46
+ assert_include(@stdout, "👻")
33
47
  end
34
48
 
35
49
  it "should copy same files into new dir" do
data/spec/config_spec.rb CHANGED
@@ -34,25 +34,26 @@ module Nyara
34
34
  assert_equal 3000, Config['port']
35
35
  end
36
36
 
37
- it "views and public default" do
38
- Config[:root] = '/root'
37
+ it "views, assets and public default" do
38
+ Config[:root] = __dir__
39
39
  Config.init
40
40
  assert_equal nil, Config['public']
41
- assert_equal '/root/views', Config['views']
41
+ assert_equal __dir__ + '/views', Config['views']
42
+ assert_equal nil, Config['assets']
42
43
  end
43
44
 
44
45
  context "#project_path" do
45
46
  before :each do
46
47
  Config.configure do
47
48
  reset
48
- set :root, '/a'
49
+ set :root, __dir__
49
50
  init
50
51
  end
51
52
  end
52
53
 
53
54
  it "works" do
54
55
  path = Config.project_path 'b/../../c', false
55
- assert_equal '/c', path
56
+ assert_equal File.dirname(__dir__) + '/c', path
56
57
  end
57
58
 
58
59
  it "restrict mode ensures dir safety" do
@@ -62,18 +63,18 @@ module Nyara
62
63
 
63
64
  it "restrict mode allows '..' if it doesn't get outside" do
64
65
  path = Config.project_path 'b/../c', true
65
- assert_equal '/a/c', path
66
+ assert_equal __dir__ + '/c', path
66
67
  end
67
68
  end
68
69
 
69
70
  it "#public_path" do
70
71
  Config.configure do
71
- set :root, '/root'
72
+ set :root, __dir__
72
73
  set :public, 'a'
73
74
  init
74
75
  end
75
76
  path = Config.public_path '/b'
76
- assert_equal '/root/a/b', path
77
+ assert_equal __dir__ + '/a/b', path
77
78
  end
78
79
 
79
80
  it "#views_path" do
@@ -86,6 +87,16 @@ module Nyara
86
87
  assert_equal '/', path
87
88
  end
88
89
 
90
+ it "#assets_path" do
91
+ Config.configure do
92
+ set :root, '/'
93
+ set :assets, '/a'
94
+ init
95
+ end
96
+ path = Config.assets_path '../..', false
97
+ assert_equal '/', path
98
+ end
99
+
89
100
  it "env helpers" do
90
101
  Config.set :env, 'test'
91
102
  assert_equal true, Config.test?
@@ -20,5 +20,19 @@ module Nyara
20
20
  assert_equal nil, AChildController.controller_name
21
21
  assert_equal 'll', AChildController.default_layout
22
22
  end
23
+
24
+ context "argument validation" do
25
+ class DummyController < Controller
26
+ get '/' do
27
+ end
28
+ end
29
+
30
+ it "#redirect_to checks first parameter" do
31
+ c = DummyController.new Ext.request_new
32
+ assert_raise ArgumentError do
33
+ c.redirect_to '/'
34
+ end
35
+ end
36
+ end
23
37
  end
24
38
  end
@@ -8,10 +8,12 @@ module Nyara
8
8
  exec 'ruby', __dir__ + '/apps/connect.rb'
9
9
  end
10
10
  GC.stress = false
11
- sleep 1.8 # wait for server startup
11
+ sleep 2.3 # wait for server startup
12
12
  end
13
13
 
14
14
  after :all do
15
+ Process.kill :TERM, @server
16
+ sleep 0.2
15
17
  Process.kill :KILL, @server
16
18
  end
17
19
 
@@ -23,17 +23,27 @@ module Nyara
23
23
  @controller = 'stub2'
24
24
  }
25
25
  @e3 = Route.new{
26
+ @http_method = 'GET'
27
+ @scope = '/hello'
28
+ @prefix = '/hello'
29
+ @suffix = '(\d+)'
30
+ @id = :'#third'
31
+ @conv = [:to_i]
32
+ @controller = 'stub3'
33
+ }
34
+ @e4 = Route.new{
26
35
  @http_method = 'GET'
27
36
  @scope = '/a目录'
28
37
  @prefix = '/a目录/'
29
38
  @suffix = '(\d+)-(\d+)-(\d+)'
30
39
  @id = :'#dir'
31
40
  @conv = [:to_i, :to_i, :to_i]
32
- @controller = 'stub3'
41
+ @controller = 'stub4'
33
42
  }
34
43
  Ext.register_route @e1
35
44
  Ext.register_route @e2
36
45
  Ext.register_route @e3
46
+ Ext.register_route @e4
37
47
  end
38
48
 
39
49
  after :all do
@@ -42,11 +52,12 @@ module Nyara
42
52
 
43
53
  it '#register_route sub-prefix optimization' do
44
54
  rules = Ext.list_route['GET']
45
- assert_equal 3, rules.size
55
+ assert_equal 4, rules.size
46
56
 
47
57
  assert_equal false, rules[0].first # first
48
58
  assert_equal true, rules[1].first # is sub of prev
49
- assert_equal false, rules[2].first # not sub of prev
59
+ assert_equal true, rules[2].first # is sub of prev
60
+ assert_equal false, rules[3].first # not sub of prev
50
61
  end
51
62
 
52
63
  it '#lookup_route' do
@@ -55,11 +66,22 @@ module Nyara
55
66
  assert_equal @e2.controller, cont
56
67
  assert_equal [:'#second'], args
57
68
 
69
+ scope, cont, args = Ext.lookup_route 'GET', '/hello.js', nil
70
+ assert_equal @e2.scope, scope
71
+ assert_equal @e2.controller, cont
72
+ assert_equal [:'#second'], args
73
+
58
74
  scope, cont, args = Ext.lookup_route 'GET', '/hello/3world', nil
59
75
  assert_equal @e1.scope, scope
60
76
  assert_equal @e1.controller, cont
61
77
  assert_equal [:'#1', 3], args
62
78
 
79
+ # same prefix as @e2, but should not be affected by @e2
80
+ scope, cont, args = Ext.lookup_route 'GET', '/hello3', nil
81
+ assert_equal @e3.scope, scope
82
+ assert_equal @e3.controller, cont
83
+ assert_equal [:'#third', 3], args
84
+
63
85
  scope, _ = Ext.lookup_route 'GET', '/world', nil
64
86
  assert_equal nil, scope
65
87