Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

0.2 - some cleaning

  • Loading branch information...
commit f486245888b022d6b01307ac2749d4f43bc6f786 1 parent 145c1c2
@timcharper timcharper authored
View
16 lib/spork/bootstrapper.rb → assets/bootstrap.rb
@@ -1,9 +1,3 @@
-class Spork::Bootstrapper
- def self.run
- puts "Bootstrapping #{SPEC_HELPER_FILE}"
- contents = File.read(SPEC_HELPER_FILE)
- File.open(SPEC_HELPER_FILE, "wb") do |f|
- f.puts <<-BOOTSTRAP
require 'rubygems'
require 'spork'
@@ -33,13 +27,3 @@ def self.run
-
-BOOTSTRAP
-
- f.puts(contents)
- end
-
- puts "Done. Edit #{SPEC_HELPER_FILE} now with your favorite text editor and follow the instructions."
- true
- end
-end
View
49 bin/spork
@@ -1,10 +1,11 @@
#!/usr/bin/env ruby
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
-SPEC_HELPER_FILE = File.join(Dir.pwd, "spec/spec_helper.rb")
require 'rubygems'
+require 'spork'
+require 'optparse'
-unless File.exist?(SPEC_HELPER_FILE)
+unless File.exist?(Spork::SPEC_HELPER_FILE)
puts <<-USEFUL_ERROR
Bummer!
@@ -12,33 +13,12 @@ I can't find the file spec/spec_helper.rb, which I need in order to run.
Are you running me from a project directory that has rspec set up?
USEFUL_ERROR
+ exit 1
end
-ENV["DRB"] = 'true'
-ENV["RAILS_ENV"] ||= 'test' if File.exist?("config/environment.rb")
-
-require 'rubygems'
-require 'spork'
-
-if File.read(SPEC_HELPER_FILE).include?("Spork.prefork")
- puts "Loading Spork.prefork block..."
- Spork.preforking!
- load SPEC_HELPER_FILE
-else
- puts <<-NO_SPORK_BLOCK
-spec_helper.rb is has not been sporked. Run spork --bootstrap to do so.
-NO_SPORK_BLOCK
- # are we in a rails app?
- if File.exist?("config/environment.rb")
- puts "Preloading Rails environment"
- require "config/environment.rb"
- else
- puts "There's nothing I can really do for you. Bailing."
- end
-
-end
-require 'optparse'
+ENV["DRB"] = 'true'
+ENV["RAILS_ENV"] ||= 'test' if Spork.using_rails?
options = {}
parser = OptionParser.new
@@ -47,18 +27,21 @@ parser.on("-b", "--bootstrap") {|ignore| options[:bootstrap] = true }
parser.on("-p", "--pid PIDFILE"){|pid| options[:pid] = pid }
parser.parse!(ARGV)
-case
-when options[:bootstrap]
- require 'spork/bootstrapper'
- if Spork::Bootstrapper.run
+
+if options[:bootstrap]
+ if Spork.bootstrap
exit 0
else
exit 1
end
-when options[:daemon]
- require 'spork/spec_server'
+end
+
+require 'spork/spec_server'
+Spork.preload || exit(1)
+
+if options[:daemon]
::Spork::SpecServer.daemonize(options[:pid])
else
- require 'spork/spec_server'
::Spork::SpecServer.run
end
+
View
44 lib/spork.rb
@@ -1,4 +1,6 @@
-class Spork
+module Spork
+ SPEC_HELPER_FILE = File.join(Dir.pwd, "spec/spec_helper.rb")
+
def self.prefork(&block)
return if @already_preforked
@already_preforked = true
@@ -22,4 +24,44 @@ def self.running!
def self.state
@state ||= :not_using_spork
end
+
+ def self.using_rails?
+ File.exist?("config/environment.rb")
+ end
+
+ def self.using_prefork?
+ File.read(SPEC_HELPER_FILE).include?("Spork.prefork")
+ end
+
+ def self.bootstrap
+ puts "Bootstrapping #{SPEC_HELPER_FILE}"
+ contents = File.read(SPEC_HELPER_FILE)
+ bootstrap_code = File.read(File.dirname(__FILE__) + "/../assets/bootstrap.rb")
+ File.open(SPEC_HELPER_FILE, "wb") do |f|
+ f.puts bootstrap_code
+ f.puts contents
+ end
+
+ puts "Done. Edit #{SPEC_HELPER_FILE} now with your favorite text editor and follow the instructions."
+ true
+ end
+
+ def self.preload
+ if using_prefork?
+ puts "Loading Spork.prefork block..."
+ Spork.preforking!
+ load SPEC_HELPER_FILE
+ else
+ puts "spec_helper.rb is has not been sporked. Run spork --bootstrap to do so."
+ # are we in a rails app?
+ if using_rails?
+ puts "Preloading Rails environment"
+ require "config/environment.rb"
+ else
+ puts "There's nothing I can really do for you. Bailing."
+ return false
+ end
+ end
+ true
+ end
end
View
4 lib/spork/spec_server.rb
@@ -30,7 +30,7 @@ def self.daemonize(pid_file = nil)
File.open(pid_file,"w"){|f| f.puts pid } if pid_file
exit! 0
end
-
+
def self.run
trap("USR2") { ::Spork::SpecServer.restart_test_server } if Signal.list.has_key?("USR2")
DRb.start_service("druby://127.0.0.1:#{DRB_PORT}", ::Spork::SpecServer.new)
@@ -43,7 +43,7 @@ def run(argv, stderr, stdout)
$stderr = stderr
child_pid = Kernel.fork do
Spork.running!
- load SPEC_HELPER_FILE
+ load ::Spork::SPEC_HELPER_FILE
::Spec::Runner::CommandLine.run(
::Spec::Runner::OptionParser.parse(
View
4 spork.gemspec
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{spork}
- s.version = "0.1"
+ s.version = "0.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Tim Harper"]
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.email = ["timcharper+spork@gmail.com"]
s.executables = ["spork"]
s.extra_rdoc_files = ["README.rdoc"]
- s.files = ["README.rdoc"] + Dir["lib/**/*"]
+ s.files = ["README.rdoc"] + Dir["lib/**/*"] + Dir["assets/**/*"]
s.has_rdoc = true
s.homepage = %q{http://github.com/timcharper/spork}
s.rdoc_options = ["--main", "README.rdoc"]
Please sign in to comment.
Something went wrong with that request. Please try again.