2011-05-23 22:37:19 +00:00
|
|
|
require 'guard'
|
|
|
|
require 'guard/guard'
|
2011-06-26 19:51:52 +00:00
|
|
|
require 'coffee-script'
|
2011-05-23 22:37:19 +00:00
|
|
|
|
|
|
|
module Guard
|
|
|
|
class JasmineHeadlessWebkit < Guard
|
2011-10-11 19:30:39 +00:00
|
|
|
autoload :Runner, 'guard/jasmine-headless-webkit/runner'
|
|
|
|
|
2011-06-03 20:45:19 +00:00
|
|
|
DEFAULT_EXTENSIONS = %w{js coffee}
|
|
|
|
|
2011-09-07 13:53:33 +00:00
|
|
|
ALL_SPECS_MESSAGE = "Guard::JasmineHeadlessWebkit running all specs..."
|
|
|
|
SOME_SPECS_MESSAGE = "Guard::JasmineHeadlessWebkit running the following: %s"
|
|
|
|
|
2011-09-06 20:00:27 +00:00
|
|
|
attr_reader :files_to_rerun
|
|
|
|
|
2011-09-14 14:06:55 +00:00
|
|
|
DEFAULT_OPTIONS = {
|
|
|
|
:all_on_start => true,
|
|
|
|
:run_before => false,
|
|
|
|
:valid_extensions => DEFAULT_EXTENSIONS
|
|
|
|
}
|
|
|
|
|
2011-05-24 00:44:18 +00:00
|
|
|
def initialize(watchers = [], options = {})
|
|
|
|
super
|
2011-09-14 14:06:55 +00:00
|
|
|
|
|
|
|
@options = DEFAULT_OPTIONS.merge(options)
|
|
|
|
@filtered_options = options
|
|
|
|
DEFAULT_OPTIONS.keys.each { |key| @filtered_options.delete(key) }
|
2011-09-06 20:00:27 +00:00
|
|
|
|
|
|
|
@files_to_rerun = []
|
2011-05-24 00:44:18 +00:00
|
|
|
end
|
2011-05-29 12:53:33 +00:00
|
|
|
|
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
|
|
|
|
|
2011-09-06 20:00:27 +00:00
|
|
|
def reload
|
|
|
|
@files_to_rerun = []
|
|
|
|
UI.info "Resetting Guard::JasmineHeadlessWebkit failed files..."
|
|
|
|
end
|
|
|
|
|
2011-05-23 22:37:19 +00:00
|
|
|
def run_all
|
2011-06-26 20:13:33 +00:00
|
|
|
run_something_and_rescue do
|
2011-11-23 22:57:19 +00:00
|
|
|
run_for_failed_files
|
2011-06-26 20:13:33 +00:00
|
|
|
end
|
2011-05-23 22:37:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_on_change(paths)
|
2011-06-26 20:13:33 +00:00
|
|
|
run_something_and_rescue do
|
2011-11-23 22:57:19 +00:00
|
|
|
if !(paths = filter_paths(paths)).empty?
|
|
|
|
paths = (paths + @files_to_rerun).uniq
|
|
|
|
|
|
|
|
run_for_failed_files(paths)
|
|
|
|
else
|
|
|
|
run_all
|
2011-06-03 20:25:00 +00:00
|
|
|
end
|
2011-05-29 12:53:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2011-09-07 13:53:33 +00:00
|
|
|
def run_for_failed_files(paths = [])
|
|
|
|
if paths.empty?
|
|
|
|
UI.info(ALL_SPECS_MESSAGE)
|
|
|
|
else
|
|
|
|
UI.info(SOME_SPECS_MESSAGE % paths.join(' '))
|
|
|
|
end
|
2011-10-11 19:30:39 +00:00
|
|
|
failed_files = Runner.run(paths, @filtered_options)
|
2011-09-26 15:48:14 +00:00
|
|
|
@files_to_rerun = failed_files || paths
|
2011-11-23 22:57:19 +00:00
|
|
|
|
2011-09-26 15:48:14 +00:00
|
|
|
failed_files && @files_to_rerun.empty?
|
2011-09-07 13:53:33 +00:00
|
|
|
end
|
|
|
|
|
2011-06-03 20:45:19 +00:00
|
|
|
def filter_paths(paths)
|
2011-10-17 19:15:57 +00:00
|
|
|
paths.collect { |path| Dir[path] }.flatten.find_all { |path| File.extname(path)[valid_extensions] }.collect { |path| File.expand_path(path) }.uniq
|
2011-06-03 20:45:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_extensions
|
|
|
|
%r{\.(#{@options[:valid_extensions].join('|')})$}
|
|
|
|
end
|
|
|
|
|
2011-06-01 21:14:23 +00:00
|
|
|
def run_program(name, command = nil)
|
|
|
|
command ||= name
|
|
|
|
UI.info "Guard::JasmineHeadlessWebkit running #{name}..."
|
|
|
|
system command
|
|
|
|
$?.exitstatus == 0
|
|
|
|
end
|
2011-06-26 20:13:33 +00:00
|
|
|
|
|
|
|
def run_something_and_rescue
|
|
|
|
yield
|
|
|
|
rescue ::CoffeeScript::CompilationError
|
|
|
|
rescue StandardError => e
|
2011-09-07 13:53:33 +00:00
|
|
|
if ENV['GUARD_ENV'] == 'test'
|
|
|
|
raise e
|
|
|
|
else
|
|
|
|
puts e.message
|
|
|
|
puts e.backtrace.join("\n")
|
|
|
|
puts
|
|
|
|
end
|
2011-06-26 20:13:33 +00:00
|
|
|
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
|
|
|
|
|