lib/guard/rails.rb in guard-rails-0.0.1 vs lib/guard/rails.rb in guard-rails-0.0.2
- old
+ new
@@ -1,77 +1,52 @@
require 'guard'
require 'guard/guard'
+require 'guard/rails/runner'
require 'rbconfig'
module Guard
class Rails < ::Guard::Guard
- attr_reader :options
+ attr_reader :options, :runner
- def initialize(watchers = [], options = {})
- super
- @options = {
+ DEFAULT_OPTIONS = {
:port => 3000,
:environment => 'development',
:start_on_start => true,
- :force_run => false
- }.merge(options)
+ :force_run => false,
+ :timeout => 20
+ }
+
+ def initialize(watchers = [], options = {})
+ super
+ @options = DEFAULT_OPTIONS.merge(options)
+
+ @runner = RailsRunner.new(@options)
end
def start
- UI.info "Guard::Rails restarting app on port #{options[:port]} using #{options[:environment]} environment."
+ UI.info "Guard::Rails will now restart your app on port #{options[:port]} using #{options[:environment]} environment."
run_all if options[:start_on_start]
end
def run_all
+ UI.info "Restarting Rails..."
Notifier.notify("Rails restarting on port #{options[:port]} in #{options[:environment]} environment...", :title => "Restarting Rails...", :image => :pending)
- stop_rails ; start_rails
- Notifier.notify("Rails restarted on port #{options[:port]}.", :title => "Rails restarted!", :image => :success)
+ if runner.restart
+ UI.info "Rails restarted, pid #{runner.pid}"
+ Notifier.notify("Rails restarted on port #{options[:port]}.", :title => "Rails restarted!", :image => :success)
+ else
+ UI.info "Rails NOT restarted, check your log files."
+ Notifier.notify("Rails NOT restarted, check your log files.", :title => "Rails NOT restarted!", :image => :failure)
+ end
end
def stop
Notifier.notify("Until next time...", :title => "Rails shutting down.", :image => :pending)
- stop_rails
+ runner.stop
end
def run_on_change(paths)
run_all
- end
-
- private
- def pid_file
- File.expand_path("tmp/pids/#{options[:environment]}.pid")
- end
-
- 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} &'}
- while !File.file?(pid_file)
- sleep 0.5
- end
- UI.info "Rails restarted, pid #{File.read(pid_file)}"
- end
-
- def stop_rails
- if File.file?(pid_file)
- system %{kill -INT #{File.read(pid_file).strip}}
- 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