pistol 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,20 +1,34 @@
1
- task :start_app do
2
- require "./test/fixtures/helloworld/app.rb"
1
+ task :test => :start_app do
2
+ require "cutest"
3
+ Cutest.run(Dir["test/*_test.rb"])
4
+ end
5
+
6
+ task :start_app => :runner do
7
+ require "./test/fixtures/sinatra/app.rb"
8
+ require "./test/fixtures/rackapp/app.rb"
9
+ require "./test/fixtures/cuba/app.rb"
3
10
  require "logger"
4
11
 
5
- Thread.new do
6
- Rack::Handler::WEBrick.run(App.new,
7
- :Port => 9595,
8
- :Logger => ::Logger.new(File.open("/dev/null", "w")),
9
- :AccessLog => []
10
- )
11
- end
12
+ logger = ::Logger.new(File.open("/dev/null", "w"))
13
+
14
+ run(App.new, 9595, logger)
15
+ run($rackapp, 9696, logger)
16
+ run(Cuba, 9797, logger)
12
17
 
13
18
  # Let's simply wait for the sinatra app to start.
14
19
  sleep 2
15
20
  end
16
21
 
17
- task :test => :start_app do
18
- require "cutest"
19
- Cutest.run(Dir["test/*_test.rb"])
20
- end
22
+ task :runner do
23
+ def run(app, port, logger)
24
+ Thread.new do
25
+ Rack::Handler::WEBrick.run(app,
26
+ :Port => port,
27
+ :Logger => logger,
28
+ :AccessLog => []
29
+ )
30
+ end
31
+ end
32
+ end
33
+
34
+ task :default => :test
@@ -1,19 +1,17 @@
1
1
  class Pistol
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
 
4
- attr :options
5
-
6
- def initialize(app, options = {})
7
- @options = optimize_options(options)
8
- @app = app
9
- @app_file = options[:files].first
10
- @last = last_mtime
4
+ def initialize(app, files, &block)
5
+ @app = app
6
+ @files = files.map { |file| File.expand_path(file) }
7
+ @block = block
8
+ @last = last_mtime
11
9
  end
12
10
 
13
11
  def call(env)
14
12
  current = last_mtime
15
13
 
16
- if current >= @last
14
+ if current > @last
17
15
  if Thread.list.size > 1
18
16
  Thread.exclusive { reload! }
19
17
  else
@@ -28,20 +26,15 @@ class Pistol
28
26
 
29
27
  private
30
28
  def reload!
31
- options[:files].each { |file| $LOADED_FEATURES.delete(file) }
32
-
33
- @app.class.reset! if @app.class.respond_to?(:reset!)
34
- require @app_file
29
+ @files.each { |file| $LOADED_FEATURES.delete(file) }
30
+ @block.call(changed) if @block
35
31
  end
36
32
 
37
- def last_mtime
38
- options[:files].map { |file| ::File.mtime(file) }.max
33
+ def changed
34
+ @files.select { |file| ::File.mtime(file) > @last }
39
35
  end
40
36
 
41
- def optimize_options(options)
42
- opts = options.dup
43
- opts[:files] ||= Dir["./app/**/*.rb"]
44
- opts[:files].map! { |file| File.expand_path(file) }
45
- opts
37
+ def last_mtime
38
+ @files.map { |file| ::File.mtime(file) }.max
46
39
  end
47
40
  end
@@ -0,0 +1,32 @@
1
+ require File.expand_path("./helper", File.dirname(__FILE__))
2
+
3
+ scope do
4
+ app "cuba"
5
+ server "http://localhost:9797"
6
+
7
+ test "/hello" do
8
+ assert "Hello" == get("/hello")
9
+ end
10
+
11
+ # test "/hello when modified" do
12
+ # modify("app.rb", %{"Hello"}, %{"New Hello"}) do
13
+ # assert "New Hello" == get("/hello")
14
+ # end
15
+ # end
16
+
17
+ test "/article changes when Article is changed" do
18
+ assert "Hello World v1" == get("/article")
19
+
20
+ modify("app/article.rb", %{"Hello World v1"}, %{"Hello World v2"}) do
21
+ assert "Hello World v2" == get("/article")
22
+ end
23
+ end
24
+
25
+ test "/book doesn't change since Book is in lib" do
26
+ assert "Sinatra Book" == get("/book")
27
+
28
+ modify("lib/book.rb", %{"Sinatra Book"}, %{"Rack Book"}) do
29
+ assert "Sinatra Book" == get("/book")
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ require "cuba"
2
+ require File.expand_path("./app/article", File.dirname(__FILE__))
3
+ require File.expand_path("./lib/book", File.dirname(__FILE__))
4
+ require File.expand_path("../../../lib/pistol", File.dirname(__FILE__))
5
+
6
+ Cuba.use Pistol, Dir[__FILE__,
7
+ File.expand_path("./app/article.rb", File.dirname(__FILE__))] do
8
+ Cuba.reset!
9
+ load File.expand_path(__FILE__)
10
+ end
11
+
12
+ Cuba.define do
13
+ on get do
14
+ on path("hello") do
15
+ res.write "Hello"
16
+ end
17
+
18
+ on path("article") do
19
+ res.write Article.content
20
+ end
21
+
22
+ on path("book") do
23
+ res.write Book.title
24
+ end
25
+ end
26
+ end
27
+
28
+ if __FILE__ == $0
29
+ Rack::Handler::WEBrick.run(Cuba,
30
+ :Port => 9797,
31
+ :AccessLog => []
32
+ )
33
+ end
@@ -0,0 +1,5 @@
1
+ class Article
2
+ def self.content
3
+ "Hello World v1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path("./app/article", File.dirname(__FILE__))
2
+ require File.expand_path("./lib/book", File.dirname(__FILE__))
3
+ require File.expand_path("../../../lib/pistol", File.dirname(__FILE__))
4
+
5
+ $rackapp = Rack::Builder.app do
6
+ use Pistol, Dir[__FILE__,
7
+ File.expand_path("app/article.rb", File.dirname(__FILE__))] do
8
+ load __FILE__
9
+ end
10
+
11
+ run(lambda { |env|
12
+ case env["PATH_INFO"]
13
+ when "/hello"
14
+ [200, { "Content-Type" => "text/html" }, ["Hello"]]
15
+ when "/article"
16
+ [200, { "Content-Type" => "text/html" }, [Article.content]]
17
+ when "/book"
18
+ [200, { "Content-Type" => "text/html" }, [Book.title]]
19
+ else
20
+ [404, { "Content-Type" => "text/html" }, ["Not Found"]]
21
+ end
22
+ })
23
+ end
@@ -0,0 +1,5 @@
1
+ class Book
2
+ def self.title
3
+ "Sinatra Book"
4
+ end
5
+ end
@@ -7,7 +7,10 @@ class App < Sinatra::Base
7
7
  set :root, lambda { |*args| File.join(File.dirname(__FILE__), *args) }
8
8
  set :port, 9595
9
9
 
10
- use Pistol, :files => Dir[__FILE__, root("app/**/*.rb")]
10
+ use Pistol, Dir[__FILE__, root("app/**/*.rb")] do
11
+ self.reset!
12
+ require __FILE__
13
+ end
11
14
 
12
15
  get "/hello" do
13
16
  "Hello"
@@ -0,0 +1,5 @@
1
+ class Article
2
+ def self.content
3
+ "Hello World v1"
4
+ end
5
+ end
File without changes
@@ -0,0 +1,5 @@
1
+ class Book
2
+ def self.title
3
+ "Sinatra Book"
4
+ end
5
+ end
@@ -1,16 +1,24 @@
1
1
  require "cutest"
2
2
  require "open-uri"
3
+ require "fileutils"
3
4
 
4
- TEST_SERVER = "http://localhost:9595"
5
+ class Cutest::Scope
6
+ def app(app = nil)
7
+ @app = app if app
8
+ @app
9
+ end
10
+
11
+ def server(server = nil)
12
+ @server = server if server
13
+ @server
14
+ end
5
15
 
6
- module Kernel
7
- private
8
16
  def get(path)
9
- open([TEST_SERVER, path].join).read
17
+ open([server, path].join).read
10
18
  end
11
19
 
12
20
  def app_root(*args)
13
- File.join(File.dirname(__FILE__), "fixtures", "helloworld", *args)
21
+ File.join(File.dirname(__FILE__), "fixtures", app, *args)
14
22
  end
15
23
 
16
24
  def modify(file, old, new)
@@ -18,14 +26,15 @@ private
18
26
 
19
27
  prev = File.read(path)
20
28
  change(path, prev.gsub(old, new))
29
+ sleep 1
21
30
  FileUtils.touch(path)
22
-
23
31
  yield
24
32
  ensure
25
- change(app_root(file), prev)
33
+ change(path, prev)
26
34
  end
27
35
 
28
36
  def updated(file)
37
+ sleep 1
29
38
  FileUtils.touch(app_root(file))
30
39
  yield
31
40
  end
@@ -0,0 +1,34 @@
1
+ require File.expand_path("./helper", File.dirname(__FILE__))
2
+
3
+ scope do
4
+ app "rackapp"
5
+ server "http://localhost:9696"
6
+
7
+ test "/hello" do
8
+ assert "Hello" == get("/hello")
9
+ end
10
+
11
+ # test "/hello when modified" do
12
+ # modify("app.rb", %{"Hello"}, %{"New Hello"}) do
13
+ # assert "New Hello" == get("/hello")
14
+ # end
15
+ # end
16
+
17
+ test "/article changes when Article is changed" do
18
+ updated("app/article.rb") do
19
+ assert "Hello World v1" == get("/article")
20
+ end
21
+
22
+ modify("app/article.rb", %{"Hello World v1"}, %{"Hello World v2"}) do
23
+ assert "Hello World v2" == get("/article")
24
+ end
25
+ end
26
+
27
+ test "/book doesn't change since Book is in lib" do
28
+ assert "Sinatra Book" == get("/book")
29
+
30
+ modify("lib/book.rb", %{"Sinatra Book"}, %{"Rack Book"}) do
31
+ assert "Sinatra Book" == get("/book")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path("./helper", File.dirname(__FILE__))
2
+
3
+ scope do
4
+ app "sinatra"
5
+ server "http://localhost:9595"
6
+
7
+ test "/hello" do
8
+ assert "Hello" == get("/hello")
9
+ end
10
+
11
+ test "/hello when modified" do
12
+ modify("app.rb", %{"Hello"}, %{"New Hello"}) do
13
+ assert "New Hello" == get("/hello")
14
+ end
15
+ end
16
+
17
+ test "/article changes when Article is changed" do
18
+ assert "Hello World v1" == get("/article")
19
+
20
+ modify("app/article.rb", %{"Hello World v1"}, %{"Hello World v2"}) do
21
+ assert "Hello World v2" == get("/article")
22
+ end
23
+ end
24
+
25
+ test "/book doesn't change since Book is in lib" do
26
+ assert "Sinatra Book" == get("/book")
27
+
28
+ modify("lib/book.rb", %{"Sinatra Book"}, %{"Rack Book"}) do
29
+ assert "Sinatra Book" == get("/book")
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michel Martens
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-10-08 00:00:00 +08:00
19
+ date: 2010-11-14 00:00:00 +08:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -34,7 +34,7 @@ dependencies:
34
34
  type: :development
35
35
  version_requirements: *id001
36
36
  - !ruby/object:Gem::Dependency
37
- name: sinatra
37
+ name: rack
38
38
  prerelease: false
39
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
40
  none: false
@@ -47,6 +47,34 @@ dependencies:
47
47
  version: "1.0"
48
48
  type: :development
49
49
  version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: sinatra
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 1
60
+ - 0
61
+ version: "1.0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: cuba
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 1
74
+ - 0
75
+ version: "1.0"
76
+ type: :development
77
+ version_requirements: *id004
50
78
  description: |-
51
79
  Extremely lightweight reloading tool for rack &
52
80
  sinatra apps.
@@ -65,12 +93,20 @@ files:
65
93
  - README.markdown
66
94
  - LICENSE
67
95
  - Rakefile
68
- - test/fixtures/helloworld/app/article.rb
69
- - test/fixtures/helloworld/app.rb
70
- - test/fixtures/helloworld/lib/article.rb
71
- - test/fixtures/helloworld/lib/book.rb
96
+ - test/cuba_test.rb
97
+ - test/fixtures/cuba/app/article.rb
98
+ - test/fixtures/cuba/app.rb
99
+ - test/fixtures/cuba/lib/article.rb
100
+ - test/fixtures/cuba/lib/book.rb
101
+ - test/fixtures/rackapp/app/article.rb
102
+ - test/fixtures/rackapp/app.rb
103
+ - test/fixtures/rackapp/lib/book.rb
104
+ - test/fixtures/sinatra/app/article.rb
105
+ - test/fixtures/sinatra/app.rb
106
+ - test/fixtures/sinatra/lib/article.rb
107
+ - test/fixtures/sinatra/lib/book.rb
72
108
  - test/helper.rb
73
- - test/pistol_test.rb
109
+ - test/rack_app_test.rb
74
110
  - test/sinatra_test.rb
75
111
  has_rdoc: true
76
112
  homepage: http://github.com/monk/pistol
@@ -1,27 +0,0 @@
1
- require File.expand_path("./helper", File.dirname(__FILE__))
2
-
3
- test "/hello" do
4
- assert "Hello" == get("/hello")
5
- end
6
-
7
- test "/hello when modified" do
8
- modify("app.rb", %{"Hello"}, %{"New Hello"}) do
9
- assert "New Hello" == get("/hello")
10
- end
11
- end
12
-
13
- test "/article changes when Article is changed" do
14
- assert "Hello World v1" == get("/article")
15
-
16
- modify("app/article.rb", %{"Hello World v1"}, %{"Hello World v2"}) do
17
- assert "Hello World v2" == get("/article")
18
- end
19
- end
20
-
21
- test "/book doesn't change since Book is in lib" do
22
- assert "Sinatra Book" == get("/book")
23
-
24
- modify("lib/book.rb", %{"Sinatra Book"}, %{"Rack Book"}) do
25
- assert "Sinatra Book" == get("/book")
26
- end
27
- end