Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 3 commits
  • 1 file changed
  • 0 commit comments
  • 2 contributors
Showing with 47 additions and 10 deletions.
  1. +47 −10 bin/spin
View
57 bin/spin
@@ -47,19 +47,20 @@ def disconnect(connection)
connection.close
end
-def rails_root
+def rails_root(preload)
path = Pathname.pwd
- until path.join('config/application.rb').file?
+ until path.join(preload).file?
return if path.root?
path = path.parent
end
path
end
# ## spin serve
-def serve(force_rspec, force_testunit, time, push_results)
- root_path = rails_root and Dir.chdir(root_path)
+def serve(force_rspec, force_testunit, time, push_results, preload)
+ root_path = rails_root(preload) and Dir.chdir(root_path)
file = socket_file
+ Spin.parse_hook_file(root_path)
# We delete the tmp file for the Unix socket if it already exists. The file
# is scoped to the `pwd`, so if it already exists then it must be from an
@@ -91,7 +92,9 @@ def serve(force_rspec, force_testunit, time, push_results)
# But you can't initialize the application because any non-trivial app will
# involve it's models/controllers, etc. in its initialization, which you
# definitely don't want to preload.
- require File.expand_path 'config/application'
+ Spin.execute_hook(:before_preload)
+ require File.expand_path preload.sub('.rb','')
+ Spin.execute_hook(:after_preload)
# Determine the test framework to use using the passed-in 'force' options
# or else default to checking for defined constants.
@@ -114,7 +117,7 @@ def serve(force_rspec, force_testunit, time, push_results)
# This is the amount of time that you'll save on each subsequent test run.
puts "Preloaded Rails env in #{sec}s..."
else
- warn "Could not find config/application.rb. Are you running this from the root of a Rails project?"
+ warn "Could not find #{preload}. Are you running this from the root of a Rails project?"
end
puts "Pushing test results back to push processes" if push_results
@@ -172,6 +175,7 @@ ensure
end
def fork_and_run(files, push_results, test_framework, conn)
+ Spin.execute_hook(:before_fork)
# We fork(2) before loading the file so that our pristine preloaded
# environment is untouched. The child process will load whatever code it
# needs to, then it exits and we're back to the baseline preloaded app.
@@ -190,6 +194,8 @@ def fork_and_run(files, push_results, test_framework, conn)
$stderr.reopen(conn)
end
+ Spin.execute_hook(:after_fork)
+
puts
puts "Loading #{files.inspect}"
@@ -210,7 +216,7 @@ def fork_and_run(files, push_results, test_framework, conn)
end
# ## spin push
-def push
+def push(preload)
# The filenames that we will spin up to `spin serve` are passed in as
# arguments.
files_to_load = ARGV
@@ -244,7 +250,7 @@ def push
end
end.compact.uniq
- if root_path = rails_root
+ if root_path = rails_root(preload)
files_to_load.map! do |file|
Pathname.new(file).expand_path.relative_path_from(root_path).to_s
end
@@ -271,10 +277,37 @@ rescue Errno::ECONNREFUSED, Errno::ENOENT
abort "Connection was refused. Have you started up `spin serve` yet?"
end
+module Spin
+ HOOKS = [:before_fork, :after_fork, :before_preload, :after_preload]
+
+ def self.hook(name, &block)
+ raise unless HOOKS.include?(name)
+ _hooks(name) << block
+ end
+
+ def self.execute_hook(name)
+ raise unless HOOKS.include?(name)
+ _hooks(name).each(&:call)
+ end
+
+ def self.parse_hook_file(root)
+ load(root.join(".spin.rb"))
+ end
+
+ private
+
+ def self._hooks(name)
+ @hooks ||= {}
+ @hooks[name] ||= []
+ @hooks[name]
+ end
+end
+
force_rspec = false
force_testunit = false
time = false
push_results = false
+preload = "config/application.rb"
options = OptionParser.new do |opts|
opts.banner = usage
opts.separator ""
@@ -300,6 +333,10 @@ options = OptionParser.new do |opts|
push_results = v
end
+ opts.on('--preload FILE', "Preload this file instead of #{preload}") do |v|
+ preload = v
+ end
+
opts.separator "General Options:"
opts.on('-e', 'Stub to keep kicker happy')
opts.on('-h', '--help') do
@@ -311,8 +348,8 @@ options.parse!
subcommand = ARGV.shift
case subcommand
-when 'serve' then serve(force_rspec, force_testunit, time, push_results)
-when 'push' then push
+when 'serve' then serve(force_rspec, force_testunit, time, push_results, preload)
+when 'push' then push(preload)
else
$stderr.puts options
exit 1

No commit comments for this range

Something went wrong with that request. Please try again.