vegas 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-04-13
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Aaron Quint, Quirkey NYC, LLC.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/vegas.rb
7
+ lib/vegas/runner.rb
8
+ test/test_apps.rb
9
+ test/test_helper.rb
10
+ test/test_vegas_runner.rb
@@ -0,0 +1,32 @@
1
+ = vegas
2
+
3
+ http://code.quirkey.com/vegas
4
+
5
+ == DESCRIPTION:
6
+
7
+ Vegas aims to solve the simple problem of creating executable versions of Sinatra/Rack apps.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ Currently, Vegas just includes a single class Vegas::Runner which wraps your Sinatra app to give it command line options, daemonization, PID/URL tracking, and browser launching (using Launchy).
12
+
13
+ Lets say you have a gem with a sinatra application. With Vegas you can create a bin that looks like
14
+
15
+ #!/usr/bin/env ruby
16
+ # ./bin/myapp
17
+
18
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/myapp")
19
+ require 'vegas'
20
+
21
+ Vegas::Runner.new(Sinatra::Application, 'myapp')
22
+
23
+
24
+ See the website: http://code.quirkey.com/vegas for full usage/options.
25
+
26
+ == INSTALL:
27
+
28
+ sudo gem install vegas
29
+
30
+ == LICENSE:
31
+
32
+ MIT LICENSE, see /LICENSE for details
@@ -0,0 +1,31 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/vegas'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('vegas', Vegas::VERSION) do |p|
7
+ p.developer('Aaron Quint', 'aaron@quirkey.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = 'quirkey'
11
+ p.extra_deps = [
12
+ ['sinatra','>= 0.9.1'],
13
+ ['launchy','>= 0.3.3']
14
+ ]
15
+ p.extra_dev_deps = [
16
+ ['newgem', ">= #{::Newgem::VERSION}"],
17
+ ['nokogiri', ">= 1.0.6"],
18
+ ['bacon', ">= 1.1.0"]
19
+ ]
20
+
21
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
22
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
23
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
24
+ p.rsync_args = '-av --delete --ignore-errors'
25
+ end
26
+
27
+ require 'newgem/tasks' # load /tasks/*.rake
28
+ Dir['tasks/**/*.rake'].each { |t| load t }
29
+
30
+ # TODO - want other tests/tasks run by default? Add them to the list
31
+ # task :default => [:spec, :features]
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'launchy'
4
+
5
+ $LOAD_PATH.unshift File.dirname(__FILE__)
6
+
7
+ module Vegas
8
+ VERSION = "0.0.1"
9
+
10
+ autoload :Runner, 'vegas/runner'
11
+ end
@@ -0,0 +1,236 @@
1
+ require 'open-uri'
2
+ require 'logger'
3
+
4
+ module Vegas
5
+ class Runner
6
+ attr_reader :app, :app_name, :rack_handler, :port, :host, :options
7
+
8
+ ROOT_DIR = File.expand_path(File.join('~', '.vegas'))
9
+ PORT = 5678
10
+ HOST = '0.0.0.0'
11
+
12
+ def initialize(app, app_name, set_options = {}, &block)
13
+ # initialize
14
+ @app = app
15
+ @app_name = app_name
16
+ @options = set_options || {}
17
+ @rack_handler = @app.send :detect_rack_handler
18
+ # load options from opt parser
19
+ define_options do |opts|
20
+ if block_given?
21
+ opts.separator ''
22
+ opts.separator "#{app_name} options:"
23
+ yield(opts, app)
24
+ end
25
+ end
26
+ # set app options
27
+ @host = options[:host] || HOST
28
+ @app.set options
29
+ # initialize app dir
30
+ FileUtils.mkdir_p(app_dir)
31
+
32
+ logger.info "== Starting #{app_name}"
33
+
34
+ check_for_running
35
+ find_port
36
+ write_url
37
+ launch!
38
+
39
+ begin
40
+ daemonize! unless options[:foreground]
41
+ run!
42
+ rescue RuntimeError => e
43
+ logger.warn "== There was an error starting #{app_name}: #{e}"
44
+ exit
45
+ end
46
+ end
47
+
48
+ def app_dir
49
+ File.join(ROOT_DIR, app_name)
50
+ end
51
+
52
+ def pid_file
53
+ File.join(app_dir, "#{app_name}.pid")
54
+ end
55
+
56
+ def url_file
57
+ File.join(app_dir, "#{app_name}.url")
58
+ end
59
+
60
+ def url
61
+ "http://#{host}:#{port}"
62
+ end
63
+
64
+ def log_file
65
+ File.join(app_dir, "#{app_name}.log")
66
+ end
67
+
68
+ def handler_name
69
+ rack_handler.name.gsub(/.*::/, '')
70
+ end
71
+
72
+ def find_port
73
+ if @port = options[:port]
74
+ if !port_open?
75
+ logger.warn "== Port #{port} is already in use. Please try another or don't use -P, for auto-port"
76
+ end
77
+ else
78
+ @port = PORT
79
+ logger.info "== Trying to start #{app_name} on Port #{port}"
80
+ while !port_open?
81
+ @port += 1
82
+ logger.info "== Trying to start #{app_name} on Port #{port}"
83
+ end
84
+ end
85
+ end
86
+
87
+ def port_open?(check_url = nil)
88
+ begin
89
+ open(check_url || url)
90
+ false
91
+ rescue Errno::ECONNREFUSED => e
92
+ true
93
+ end
94
+ end
95
+
96
+ def write_url
97
+ File.open(url_file, 'w') {|f| f << url }
98
+ end
99
+
100
+ def check_for_running
101
+ if File.exists?(pid_file) && File.exists?(url_file)
102
+ running_url = File.read(url_file)
103
+ if !port_open?(running_url)
104
+ logger.warn "== #{app_name} is already running at #{running_url}"
105
+ launch!(running_url)
106
+ exit!
107
+ end
108
+ end
109
+ end
110
+
111
+ def run!
112
+ rack_handler.run app, :Host => host, :Port => port do |server|
113
+ trap(:INT) do
114
+ ## Use thins' hard #stop! if available, otherwise just #stop
115
+ server.respond_to?(:stop!) ? server.stop! : server.stop
116
+ logger.info "== #{app_name} received INT ... stopping : #{Time.now}"
117
+ end
118
+ end
119
+ end
120
+
121
+ # Adapted from Rackup
122
+ def daemonize!
123
+ if RUBY_VERSION < "1.9"
124
+ exit if fork
125
+ Process.setsid
126
+ exit if fork
127
+ Dir.chdir "/"
128
+ File.umask 0000
129
+ STDIN.reopen "/dev/null"
130
+ STDOUT.reopen log_file, "a"
131
+ STDERR.reopen log_file, "a"
132
+ else
133
+ Process.daemon
134
+ end
135
+
136
+ File.open(pid_file, 'w'){ |f| f.write("#{Process.pid}") }
137
+ at_exit { File.delete(pid_file) if File.exist?(pid_file) }
138
+ end
139
+
140
+ def launch!(specific_url = nil)
141
+ Launchy.open(specific_url || url)
142
+ end
143
+
144
+ def kill!
145
+ pid = File.read(pid_file)
146
+ if pid
147
+ logger.warn "== Sending INT to #{pid}"
148
+ Process.kill('INT', pid.to_i)
149
+ else
150
+ logger.warn "== pid not found at #{pid_file}"
151
+ end
152
+ end
153
+
154
+ def status
155
+ if File.exists?(pid_file)
156
+ logger.info "== #{app_name} running"
157
+ logger.info "== PID #{File.read(pid_file)}"
158
+ logger.info "== URL #{File.read(url_file)}" if File.exists?(url_file)
159
+ else
160
+ logger.info "== #{app_name} not running!"
161
+ end
162
+ end
163
+
164
+ def self.logger
165
+ @logger ||= Logger.new(STDOUT)
166
+ end
167
+
168
+ def logger
169
+ self.class.logger
170
+ end
171
+
172
+ private
173
+ def define_options
174
+ OptionParser.new("", 24, ' ') { |opts|
175
+ opts.banner = "Usage: #{app_name} [options]"
176
+
177
+ opts.separator ""
178
+ opts.separator "Vegas options:"
179
+
180
+ opts.on("-s", "--server SERVER", "serve using SERVER (webrick/mongrel)") { |s|
181
+ @rack_handler = Rack::Handler.get(s)
182
+ }
183
+
184
+ opts.on("-o", "--host HOST", "listen on HOST (default: #{HOST})") { |host|
185
+ @options[:host] = host
186
+ }
187
+
188
+ opts.on("-p", "--port PORT", "use PORT (default: #{PORT})") { |port|
189
+ @options[:port] = port
190
+ }
191
+
192
+ opts.on("-e", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e|
193
+ @options[:environment] = e
194
+ }
195
+
196
+ opts.on("-F", "--foreground", "don't daemonize, run in the foreground") { |f|
197
+ @options[:foreground] = true
198
+ }
199
+
200
+ opts.on('-K', "--kill", "kill the running process and exit") {|k|
201
+ kill!
202
+ exit
203
+ }
204
+
205
+ opts.on('-S', "--status", "display the current running PID and URL then quit") {|s|
206
+ status
207
+ exit!
208
+ }
209
+
210
+ yield opts if block_given?
211
+
212
+ opts.separator ""
213
+ opts.separator "Common options:"
214
+
215
+ opts.on_tail("-h", "--help", "Show this message") do
216
+ puts opts
217
+ exit
218
+ end
219
+
220
+ opts.on_tail("--version", "Show version") do
221
+ if app.respond_to?(:version)
222
+ puts "#{app_name} #{app.version}"
223
+ end
224
+ puts "sinatra #{Sinatra::VERSION}"
225
+ puts "vegas #{Vegas::VERSION}"
226
+ exit
227
+ end
228
+
229
+ opts.parse! ARGV
230
+ }
231
+ end
232
+
233
+
234
+
235
+ end
236
+ end
@@ -0,0 +1,22 @@
1
+ class TestApp1 < Sinatra::Default
2
+
3
+ get '/' do
4
+ 'TestApp1 Index'
5
+ end
6
+
7
+ get '/route' do
8
+ 'TestApp1 route'
9
+ end
10
+ end
11
+
12
+
13
+ class TestApp2 < Sinatra::Default
14
+
15
+ get '/' do
16
+ 'TestApp2 Index'
17
+ end
18
+
19
+ get '/route' do
20
+ 'TestApp2 route'
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'bacon'
3
+ require 'sinatra'
4
+ require 'rack/test'
5
+
6
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'vegas.rb')
7
+
8
+ require 'nokogiri'
9
+
10
+ module TestHelper
11
+ def rackup(app)
12
+ Rack::Test::Session.new(app)
13
+ end
14
+
15
+ def body
16
+ last_response.body.to_s
17
+ end
18
+
19
+ def instance_of(klass)
20
+ lambda {|obj| obj.is_a?(klass) }
21
+ end
22
+
23
+ def html_body
24
+ body =~ /^\<html/ ? body : "<html><body>#{body}</body></html>"
25
+ end
26
+
27
+ end
28
+
29
+ Bacon::Context.send(:include, TestHelper)
30
+
31
+ class Should
32
+
33
+ def have_element(search, content = nil)
34
+ satisfy "have element matching #{search}" do
35
+ doc = Nokogiri.parse(@object.to_s)
36
+ node_set = doc.search(search)
37
+ if node_set.empty?
38
+ false
39
+ else
40
+ collected_content = node_set.collect {|t| t.content }.join(' ')
41
+ case content
42
+ when Regexp
43
+ collected_content =~ content
44
+ when String
45
+ collected_content.include?(content)
46
+ when nil
47
+ true
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ describe 'Vegas::Runner' do
4
+
5
+
6
+
7
+ end
8
+
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vegas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Quint
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-13 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: launchy
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.3
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: newgem
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.2.3
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: nokogiri
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.6
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: bacon
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 1.1.0
64
+ version:
65
+ - !ruby/object:Gem::Dependency
66
+ name: hoe
67
+ type: :development
68
+ version_requirement:
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 1.8.0
74
+ version:
75
+ description: Vegas aims to solve the simple problem of creating executable versions of Sinatra/Rack apps.
76
+ email:
77
+ - aaron@quirkey.com
78
+ executables: []
79
+
80
+ extensions: []
81
+
82
+ extra_rdoc_files:
83
+ - History.txt
84
+ - Manifest.txt
85
+ - README.rdoc
86
+ files:
87
+ - History.txt
88
+ - LICENSE
89
+ - Manifest.txt
90
+ - README.rdoc
91
+ - Rakefile
92
+ - lib/vegas.rb
93
+ - lib/vegas/runner.rb
94
+ - test/test_apps.rb
95
+ - test/test_helper.rb
96
+ - test/test_vegas_runner.rb
97
+ has_rdoc: true
98
+ homepage: http://code.quirkey.com/vegas
99
+ post_install_message: PostInstall.txt
100
+ rdoc_options:
101
+ - --main
102
+ - README.rdoc
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "0"
110
+ version:
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: "0"
116
+ version:
117
+ requirements: []
118
+
119
+ rubyforge_project: quirkey
120
+ rubygems_version: 1.3.1
121
+ signing_key:
122
+ specification_version: 2
123
+ summary: Vegas aims to solve the simple problem of creating executable versions of Sinatra/Rack apps.
124
+ test_files:
125
+ - test/test_apps.rb
126
+ - test/test_helper.rb
127
+ - test/test_vegas_runner.rb