Class: Guard::Rake

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/rake.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Rake

Returns a new instance of Rake.



12
13
14
15
16
17
18
19
20
# File 'lib/guard/rake.rb', line 12

def initialize(watchers=[], options={})
  super
  @options = {
    :run_on_start => true,
    :run_on_all => true,
    :task_args => []
  }.update(options)
  @task = @options[:task]
end

Class Attribute Details

.rakefile_loadedObject

Returns the value of attribute rakefile_loaded.



9
10
11
# File 'lib/guard/rake.rb', line 9

def rakefile_loaded
  @rakefile_loaded
end

Instance Method Details

#load_rakefileObject



72
73
74
75
76
77
# File 'lib/guard/rake.rb', line 72

def load_rakefile
  ARGV.clear
  ::Rake.application.init
  ::Rake.application.load_rakefile
  self.class.rakefile_loaded = true      
end

#reloadObject



34
35
36
37
# File 'lib/guard/rake.rb', line 34

def reload
  stop
  start
end

#run_allObject



39
40
41
# File 'lib/guard/rake.rb', line 39

def run_all
  run_rake_task if @options[:run_on_all]
end

#run_on_change(paths) ⇒ Object



44
45
46
# File 'lib/guard/rake.rb', line 44

def run_on_change(paths)
  run_rake_task(paths)
end

#run_on_changes(paths) ⇒ Object



48
49
50
# File 'lib/guard/rake.rb', line 48

def run_on_changes(paths)
  run_rake_task(paths)
end

#run_rake_task(paths = []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/guard/rake.rb', line 53

def run_rake_task(paths=[])
  UI.info "running #{@task}"
  ::Rake::Task.tasks.each { |t| t.reenable }
  ::Rake::Task[@task].invoke(*@options[:task_args], paths)

  Notifier.notify(
    "watched files: #{paths}", 
    :title => "running rake task: #{@task}",
    :image => :success
  )
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")}"

  Notifier.notify(
    " #{e.class}: #{e.message}", :title => "fail to run rake task: #{@task}", :image => :failed)
  throw :task_has_failed
end

#startObject



22
23
24
25
26
27
# File 'lib/guard/rake.rb', line 22

def start
  UI.info "Starting guard-rake #{@task}"
  load_rakefile unless self.class.rakefile_loaded
  run_rake_task if @options[:run_on_start]
  true
end

#stopObject



29
30
31
32
# File 'lib/guard/rake.rb', line 29

def stop
  UI.info "Stopping guard-rake #{@task}"
  true
end