guard-jasmine-headless-webkit/lib/guard/jasmine-headless-webkit.rb

103 lines
2.6 KiB
Ruby
Raw Normal View History

2011-05-23 22:37:19 +00:00
require 'guard'
require 'guard/guard'
2011-05-24 01:28:01 +00:00
require 'guard/jasmine-headless-webkit/runner'
2011-06-26 19:51:52 +00:00
require 'coffee-script'
2011-05-23 22:37:19 +00:00
module Guard
class JasmineHeadlessWebkit < Guard
DEFAULT_EXTENSIONS = %w{js coffee}
2011-05-24 00:44:18 +00:00
def initialize(watchers = [], options = {})
super
@options = {
:all_on_start => true,
:run_before => false,
:valid_extensions => DEFAULT_EXTENSIONS
2011-05-24 00:44:18 +00:00
}.merge(options)
end
2011-05-23 22:37:19 +00:00
def start
UI.info "Guard::JasmineHeadlessWebkit is running."
2011-05-24 00:44:18 +00:00
run_all if @options[:all_on_start]
2011-05-23 22:37:19 +00:00
end
def run_all
2011-06-10 15:50:25 +00:00
UI.info "Guard::JasmineHeadlessWebkit running all specs..."
2011-06-16 14:48:34 +00:00
JasmineHeadlessWebkitRunner.run if run_all_things_before
@ran_before = false
2011-06-26 20:11:33 +00:00
rescue ::CoffeeScript::CompilationError
2011-06-17 13:37:08 +00:00
rescue StandardError => e
puts e.message
puts e.backtrace.join("\n")
puts
2011-05-23 22:37:19 +00:00
end
def run_on_change(paths)
paths = filter_paths(paths)
@ran_before = false
2011-06-16 14:48:34 +00:00
if run_all_things_before
@ran_before = true
2011-06-03 20:25:00 +00:00
if !paths.empty?
2011-06-10 15:50:25 +00:00
UI.info "Guard::JasmineHeadlessWebkit running the following: #{paths.join(' ')}"
JasmineHeadlessWebkitRunner.run(paths)
else
run_all
2011-06-03 20:25:00 +00:00
end
end
2011-06-26 20:11:33 +00:00
rescue ::CoffeeScript::CompilationError
2011-06-17 13:37:08 +00:00
rescue StandardError => e
puts e.message
puts e.backtrace.join("\n")
puts
end
private
def filter_paths(paths)
paths.find_all { |path| File.extname(path)[valid_extensions] }
end
def valid_extensions
%r{\.(#{@options[:valid_extensions].join('|')})$}
end
def run_before
run_a_thing_before(:run_before, @options[:run_before])
2011-05-23 22:37:19 +00:00
end
def run_jammit
2011-06-15 21:07:50 +00:00
$stderr.puts "Jammit support is deprecated and will be removed in the future. Use guard-jammit instead." if @options[:jammit]
run_a_thing_before(:jammit, "Jammit", %{jammit -f 2>/dev/null})
end
def run_rails_assets
run_a_thing_before(:rails_assets, "Rails Assets", %{rake assets:precompile:for_testing})
end
def run_a_thing_before(option, *args)
if @options[option] && !@ran_before
run_program(*args)
else
true
end
end
2011-06-16 14:48:34 +00:00
def run_all_things_before
run_before and run_rails_assets and run_jammit
end
def run_program(name, command = nil)
command ||= name
UI.info "Guard::JasmineHeadlessWebkit running #{name}..."
system command
$?.exitstatus == 0
end
2011-05-23 22:37:19 +00:00
end
2011-05-24 00:59:20 +00:00
class Dsl
def newest_js_file(path)
2011-05-23 22:37:19 +00:00
Dir[path + '*.{js,coffee}'].sort { |left, right| File.mtime(right) <=> File.mtime(left) }.first
end
end
end