smarter filtering of files by extension

This commit is contained in:
John Bintz 2011-06-03 16:45:19 -04:00
parent 8497977cac
commit 761de5a80a
2 changed files with 26 additions and 4 deletions

View File

@ -4,11 +4,14 @@ require 'guard/jasmine-headless-webkit/runner'
module Guard module Guard
class JasmineHeadlessWebkit < Guard class JasmineHeadlessWebkit < Guard
DEFAULT_EXTENSIONS = %w{js coffee}
def initialize(watchers = [], options = {}) def initialize(watchers = [], options = {})
super super
@options = { @options = {
:all_on_start => true, :all_on_start => true,
:run_before => false :run_before => false,
:valid_extensions => DEFAULT_EXTENSIONS
}.merge(options) }.merge(options)
end end
@ -23,6 +26,7 @@ module Guard
end end
def run_on_change(paths) def run_on_change(paths)
paths = filter_paths(paths)
@ran_jammit = false @ran_jammit = false
if run_before and run_jammit if run_before and run_jammit
@ran_jammit = true @ran_jammit = true
@ -35,6 +39,14 @@ module Guard
end end
private 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 def run_before
if @options[:run_before] if @options[:run_before]
run_program(@options[:run_before]) run_program(@options[:run_before])
@ -61,6 +73,7 @@ module Guard
class Dsl class Dsl
def newest_js_file(path) def newest_js_file(path)
p binding
Dir[path + '*.{js,coffee}'].sort { |left, right| File.mtime(right) <=> File.mtime(left) }.first Dir[path + '*.{js,coffee}'].sort { |left, right| File.mtime(right) <=> File.mtime(left) }.first
end end
end end

View File

@ -32,7 +32,7 @@ describe Guard::JasmineHeadlessWebkit do
Guard::JasmineHeadlessWebkitRunner.expects(:run).returns(1) Guard::JasmineHeadlessWebkitRunner.expects(:run).returns(1)
guard.expects(:run_all).never guard.expects(:run_all).never
guard.run_on_change(%w{test}) guard.run_on_change(%w{test.js})
end end
end end
@ -41,7 +41,7 @@ describe Guard::JasmineHeadlessWebkit do
Guard::JasmineHeadlessWebkitRunner.expects(:run).returns(0) Guard::JasmineHeadlessWebkitRunner.expects(:run).returns(0)
guard.expects(:run_all).once guard.expects(:run_all).once
guard.run_on_change(%w{test}) guard.run_on_change(%w{test.js})
end end
end end
@ -53,6 +53,15 @@ describe Guard::JasmineHeadlessWebkit do
guard.run_on_change([]) guard.run_on_change([])
end end
end end
context "Files I don't care about given, ignore" do
it 'should run all but not run once' do
Guard::JasmineHeadlessWebkitRunner.expects(:run).never
guard.expects(:run_all).once
guard.run_on_change(%w{test.jst})
end
end
end end
context 'with run_before' do context 'with run_before' do
@ -106,7 +115,7 @@ describe Guard::JasmineHeadlessWebkit do
let(:options) { { :jammit => true } } let(:options) { { :jammit => true } }
it "should run jammit only once" do it "should run jammit only once" do
guard.run_on_change(%w{path.txt}) guard.run_on_change(%w{path.js})
end end
end end
end end