2011-05-27 18:41:07 +00:00
|
|
|
require 'guard'
|
|
|
|
require 'guard/guard'
|
|
|
|
|
|
|
|
module Guard
|
|
|
|
class Rails < ::Guard::Guard
|
|
|
|
attr_reader :options
|
|
|
|
|
|
|
|
def initialize(watchers = [], options = {})
|
2011-05-27 18:51:49 +00:00
|
|
|
super
|
2011-05-27 18:41:07 +00:00
|
|
|
@options = { :port => 3000, :environment => 'development', :start_on_start => true }.merge(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
UI.info "Guard::Rails restarting app on port #{options[:port]} using #{options[:environment]} environment."
|
|
|
|
run_all if options[:start_on_start]
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_all
|
|
|
|
Notifier.notify("Rails restarting on port #{options[:port]} in #{options[:environment]}", :title => "Restarting Rails...", :image => :pending)
|
2011-05-27 18:58:20 +00:00
|
|
|
stop_rails ; start_rails
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
Notifier.notify("Until next time...", :title => "Rails shutting down.", :image => :pending)
|
|
|
|
stop_rails
|
2011-05-27 18:41:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_on_change(paths)
|
|
|
|
run_all
|
|
|
|
end
|
2011-05-27 18:58:20 +00:00
|
|
|
|
|
|
|
private
|
2011-05-27 19:28:28 +00:00
|
|
|
def pid_file
|
|
|
|
File.expand_path("tmp/pids/#{options[:environment]}.pid")
|
|
|
|
end
|
|
|
|
|
2011-05-27 18:58:20 +00:00
|
|
|
def start_rails
|
2011-05-27 19:28:28 +00:00
|
|
|
system %{sh -c 'rails s -d -e #{options[:environment]} -p #{options[:port]} --pid #{pid_file}'}
|
2011-05-27 18:58:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop_rails
|
2011-05-27 19:28:28 +00:00
|
|
|
if File.file?(pid_file)
|
|
|
|
system %{kill -INT #{File.read(pid_file).strip}}
|
|
|
|
end
|
2011-05-27 18:58:20 +00:00
|
|
|
end
|
2011-05-27 18:41:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|