guard-rails-assets/lib/guard/rails-assets.rb

59 lines
1.4 KiB
Ruby
Raw Normal View History

2011-06-17 10:32:50 +00:00
require 'guard'
require 'guard/guard'
module Guard
class RailsAssets < Guard
def initialize(watchers=[], options={})
super
2011-06-17 12:14:17 +00:00
@options = options || {}
2011-08-01 09:47:58 +00:00
@run_on = @options[:run_on] || [:start, :change]
@run_on = [@run_on] unless @run_on.respond_to?(:include?)
end
2011-06-17 10:32:50 +00:00
def start
runner.start if runner.respond_to? :start
2011-06-17 12:14:17 +00:00
compile_assets if run_for? :start
2011-06-17 10:32:50 +00:00
end
def reload
runner.reload if runner.respond_to? :reload
2011-06-17 12:14:17 +00:00
compile_assets if run_for? :reload
2011-06-17 10:32:50 +00:00
end
def run_all
2011-06-17 12:14:17 +00:00
compile_assets if run_for? :all
2011-06-17 10:32:50 +00:00
end
2011-06-17 12:14:17 +00:00
def run_on_change(paths=[])
compile_assets if run_for? :change
2011-06-17 10:32:50 +00:00
end
def compile_assets
puts "Compiling rails assets with #{runner.class.name}."
result = runner.compile_assets
2011-06-17 12:14:17 +00:00
if result
Notifier::notify 'Assets compiled'
puts 'Assets compiled.'
2011-06-17 12:14:17 +00:00
else
Notifier::notify 'see the details in the terminal', :title => "Can't compile assets", :image => :failed
puts 'Failed to compile assets.'
2011-06-17 12:14:17 +00:00
end
2011-06-17 10:32:50 +00:00
end
def runner
@runner ||= begin
runner_name = (@options[:runner] || :rails).to_s
require "guard/rails-assets/#{runner_name}_runner"
::Guard::RailsAssets.const_get(runner_name.capitalize + 'Runner').new(@options)
end
end
2011-06-17 12:14:17 +00:00
def run_for? command
2011-08-01 09:47:58 +00:00
@run_on.include?(command)
2011-06-17 12:14:17 +00:00
end
2011-06-17 10:32:50 +00:00
end
end