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-05-23 22:37:19 +00:00
|
|
|
|
|
|
|
module Guard
|
|
|
|
class JasmineHeadlessWebkit < Guard
|
2011-06-03 20:45:19 +00:00
|
|
|
DEFAULT_EXTENSIONS = %w{js coffee}
|
|
|
|
|
2011-05-24 00:44:18 +00:00
|
|
|
def initialize(watchers = [], options = {})
|
|
|
|
super
|
|
|
|
@options = {
|
2011-05-29 12:53:33 +00:00
|
|
|
:all_on_start => true,
|
2011-06-03 20:45:19 +00:00
|
|
|
:run_before => false,
|
|
|
|
:valid_extensions => DEFAULT_EXTENSIONS
|
2011-05-24 00:44:18 +00:00
|
|
|
}.merge(options)
|
|
|
|
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
|
|
|
|
|
|
|
|
def run_all
|
2011-06-01 21:14:23 +00:00
|
|
|
JasmineHeadlessWebkitRunner.run if run_before and run_jammit
|
|
|
|
@ran_jammit = false
|
2011-05-23 22:37:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_on_change(paths)
|
2011-06-03 20:45:19 +00:00
|
|
|
paths = filter_paths(paths)
|
2011-06-01 21:14:23 +00:00
|
|
|
@ran_jammit = false
|
|
|
|
if run_before and run_jammit
|
|
|
|
@ran_jammit = true
|
2011-06-03 20:25:00 +00:00
|
|
|
do_run_all = true
|
|
|
|
if !paths.empty?
|
|
|
|
do_run_all = (JasmineHeadlessWebkitRunner.run(paths) == 0)
|
|
|
|
end
|
|
|
|
run_all if do_run_all
|
2011-05-29 12:53:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2011-06-03 20:45:19 +00:00
|
|
|
def filter_paths(paths)
|
|
|
|
paths.find_all { |path| File.extname(path)[valid_extensions] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_extensions
|
|
|
|
%r{\.(#{@options[:valid_extensions].join('|')})$}
|
|
|
|
end
|
|
|
|
|
2011-05-29 12:53:33 +00:00
|
|
|
def run_before
|
|
|
|
if @options[:run_before]
|
2011-06-01 21:14:23 +00:00
|
|
|
run_program(@options[:run_before])
|
2011-05-29 12:53:33 +00:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2011-05-23 22:37:19 +00:00
|
|
|
end
|
2011-06-01 21:14:23 +00:00
|
|
|
|
|
|
|
def run_jammit
|
|
|
|
if @options[:jammit] && !@ran_jammit
|
|
|
|
run_program("Jammit", %{jammit -f 2>/dev/null})
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|