lib/spork/server.rb in spork-0.4.4 vs lib/spork/server.rb in spork-0.5.1
- old
+ new
@@ -1,16 +1,20 @@
require 'drb/drb'
require 'rbconfig'
require 'spork/forker.rb'
+require 'spork/custom_io_streams.rb'
+require 'spork/app_framework.rb'
# This is based off of spec_server.rb from rspec-rails (David Chelimsky), which was based on Florian Weber's TDDMate
class Spork::Server
@@supported_servers = []
LOAD_PREFERENCE = ['RSpec', 'Cucumber']
BOOTSTRAP_FILE = File.dirname(__FILE__) + "/../../assets/bootstrap.rb"
+ include Spork::CustomIOStreams
+
def self.port
raise NotImplemented
end
def self.helper_file
@@ -43,32 +47,28 @@
def self.load_preference_index
LOAD_PREFERENCE.index(server_name) || LOAD_PREFERENCE.length
end
- def self.using_rails?
- File.exist?("config/environment.rb")
- end
-
def self.bootstrapped?
File.read(helper_file).include?("Spork.prefork")
end
def self.bootstrap
if bootstrapped?
- puts "Already bootstrapped!"
+ stderr.puts "Already bootstrapped!"
return
end
- puts "Bootstrapping #{helper_file}."
+ stderr.puts "Bootstrapping #{helper_file}."
contents = File.read(helper_file)
bootstrap_code = File.read(BOOTSTRAP_FILE)
File.open(helper_file, "wb") do |f|
f.puts bootstrap_code
f.puts contents
end
- puts "Done. Edit #{helper_file} now with your favorite text editor and follow the instructions."
+ stderr.puts "Done. Edit #{helper_file} now with your favorite text editor and follow the instructions."
true
end
def self.run
return unless available?
@@ -78,11 +78,12 @@
def listen
trap("SIGINT") { sig_int_received }
trap("SIGTERM") { abort; exit!(0) }
trap("USR2") { abort; restart } if Signal.list.has_key?("USR2")
DRb.start_service("druby://127.0.0.1:#{port}", self)
- puts "Spork is ready and listening on #{port}!"
+ stderr.puts "Spork is ready and listening on #{port}!"
+ stderr.flush
DRb.thread.join
end
def port
self.class.port
@@ -92,47 +93,59 @@
self.class.helper_file
end
def run(argv, stderr, stdout)
return false if running?
+
@child = ::Spork::Forker.new do
$stdout, $stderr = stdout, stderr
- Spork.exec_each_run(helper_file)
+ Spork.exec_each_run { load helper_file }
run_tests(argv, stderr, stdout)
end
@child.result
end
def running?
@child && @child.running?
end
private
+ def self.framework
+ @framework ||= Spork::AppFramework.detect_framework
+ end
+
def self.preload
- if bootstrapped?
- puts "Loading Spork.prefork block..."
- Spork.exec_prefork(helper_file)
- else
- puts "#{helper_file} 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
+ Spork.exec_prefork do
+ unless bootstrapped?
+ stderr.puts "#{helper_file} has not been bootstrapped. Run spork --bootstrap to do so."
+ stderr.flush
+
+ if framework.bootstrap_required?
+ stderr.puts "I can't do anything for you by default for the framework your using: #{framework.short_name}.\nYou must bootstrap #{helper_file} to continue."
+ stderr.flush
+ return false
+ end
end
+
+ framework.preload do
+ if bootstrapped?
+ stderr.puts "Loading Spork.prefork block..."
+ stderr.flush
+ load(helper_file)
+ end
+ end
end
true
end
def run_tests(argv, input, output)
raise NotImplemented
end
def restart
- puts "restarting"
+ stderr.puts "restarting"
+ stderr.flush
config = ::Config::CONFIG
ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
command_line = [ruby, $0, ARGV].flatten.join(' ')
exec(command_line)
end
@@ -142,10 +155,11 @@
end
def sig_int_received
if running?
abort
- puts "Running tests stopped. Press CTRL-C again to stop the server."
+ stderr.puts "Running tests stopped. Press CTRL-C again to stop the server."
+ stderr.flush
else
exit!(0)
end
end
end
\ No newline at end of file