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
2011-05-27 19:59:48 +00:00
Notifier . notify ( " Rails restarting on port #{ options [ :port ] } in #{ options [ :environment ] } environment... " , :title = > " Restarting Rails... " , :image = > :pending )
2011-05-27 18:58:20 +00:00
stop_rails ; start_rails
2011-05-27 19:59:48 +00:00
Notifier . notify ( " Rails restarted on port #{ options [ :port ] } . " , :title = > " Rails restarted! " , :image = > :success )
2011-05-27 18:58:20 +00:00
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-28 22:35:01 +00:00
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 ) } "
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