force run feature

This commit is contained in:
John Bintz 2011-05-31 16:15:19 -04:00
parent 70e1f110f9
commit 628671d18a
2 changed files with 33 additions and 7 deletions

View File

@ -1,15 +1,17 @@
Want to restart your Rails dev server whilst you work? Now you can! Want to restart your Rails development server whilst you work? Now you can!
guard 'rails', :port => 5000 do guard 'rails', :port => 5000 do
watch('Gemfile.lock') watch('Gemfile.lock')
watch(%r{^(config|lib)/.*}) watch(%r{^(config|lib)/.*})
end end
Three fun options! Four fun options!
* `:port` is the port number to run on (default 3000) * `:port` is the port number to run on (default `3000`)
* `:environment` is the environment to use (default development) * `:environment` is the environment to use (default `development`)
* `:start_on_start` will start the server when starting Guard (default true) * `:start_on_start` will start the server when starting Guard (default `true`)
* `:force_run` kills any process that's holding open the listen port before attempting to (re)start Rails (default `false`).
This is super-alpha, but it works for me! This is super-alpha, but it works for me! Only really hand-tested in Mac OS X. Feel free to fork'n'fix for other
OSes, and to add some real tests.

View File

@ -1,5 +1,6 @@
require 'guard' require 'guard'
require 'guard/guard' require 'guard/guard'
require 'rbconfig'
module Guard module Guard
class Rails < ::Guard::Guard class Rails < ::Guard::Guard
@ -7,7 +8,12 @@ module Guard
def initialize(watchers = [], options = {}) def initialize(watchers = [], options = {})
super super
@options = { :port => 3000, :environment => 'development', :start_on_start => true }.merge(options) @options = {
:port => 3000,
:environment => 'development',
:start_on_start => true,
:force_run => false
}.merge(options)
end end
def start def start
@ -36,6 +42,7 @@ module Guard
end end
def start_rails def start_rails
kill_unmanaged_pid! if options[:force_run]
system %{sh -c 'cd #{Dir.pwd} && rails s -e #{options[:environment]} -p #{options[:port]} --pid #{pid_file} &'} system %{sh -c 'cd #{Dir.pwd} && rails s -e #{options[:environment]} -p #{options[:port]} --pid #{pid_file} &'}
while !File.file?(pid_file) while !File.file?(pid_file)
sleep 0.5 sleep 0.5
@ -48,6 +55,23 @@ module Guard
system %{kill -INT #{File.read(pid_file).strip}} system %{kill -INT #{File.read(pid_file).strip}}
end end
end end
def unmanaged_pid
if RbConfig::CONFIG['host_os'] =~ /darwin/
%x{lsof -P}.each_line { |line|
if line["*:#{options[:port]} "]
return line.split("\s")[1]
end
}
end
nil
end
def kill_unmanaged_pid!
if pid = unmanaged_pid
system %{kill -INT #{pid}}
end
end
end end
end end