lib/guard/rake.rb in guard-rake-0.0.7 vs lib/guard/rake.rb in guard-rake-0.0.8

- old
+ new

@@ -3,24 +3,28 @@ require 'guard/version' require 'rake' module Guard class Rake < Guard + class << self + attr_accessor :rakefile_loaded + end + def initialize(watchers=[], options={}) super @options = { :run_on_start => true, - :run_on_all => true + :run_on_all => true, + :task_args => [] }.update(options) @task = @options[:task] end def start UI.info "Starting guard-rake #{@task}" - ::Rake.application.init - ::Rake.application.load_rakefile - run_all if @options[:run_on_start] + load_rakefile unless self.class.rakefile_loaded + run_rake_task if @options[:run_on_start] true end def stop UI.info "Stopping guard-rake #{@task}" @@ -36,21 +40,31 @@ run_rake_task if @options[:run_on_all] end if ::Guard::VERSION < "1.1" def run_on_change(paths) - run_rake_task + run_rake_task(paths) end else def run_on_changes(paths) - run_rake_task + run_rake_task(paths) end end - - def run_rake_task + def run_rake_task(paths=[]) UI.info "running #{@task}" ::Rake::Task.tasks.each { |t| t.reenable } - ::Rake::Task[@task].invoke + ::Rake::Task[@task].invoke(*@options[:task_args], paths) + rescue Exception => e + UI.error "#{self.class.name} failed to run rake task <#{@task}>, exception was:\n\t#{e.class}: #{e.message}" + UI.debug "\n#{e.backtrace.join("\n")}" + + throw :task_has_failed + end + + def load_rakefile + ::Rake.application.init + ::Rake.application.load_rakefile + self.class.rakefile_loaded = true end end end