Integrated Guard::Ego inside Guard, oh yeah!
This commit is contained in:
parent
59b1ea2c96
commit
ba0b84838d
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
## New features
|
## New features
|
||||||
|
|
||||||
|
- Guard::Ego is now part of Guard, so Guardfile is automagically re-evaluated when modified. ([@thibaudgg][])
|
||||||
- Pull request [#91](https://github.com/guard/guard/pull/91): Show Guards in Guardfile with the `guard -T`. ([@johnbintz][])
|
- Pull request [#91](https://github.com/guard/guard/pull/91): Show Guards in Guardfile with the `guard -T`. ([@johnbintz][])
|
||||||
|
|
||||||
## Improvements
|
## Improvements
|
||||||
|
26
Guardfile
26
Guardfile
@ -4,16 +4,16 @@ guard :rspec, :version => 2 do
|
|||||||
watch('spec/spec_helper.rb') { "spec" }
|
watch('spec/spec_helper.rb') { "spec" }
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'guard/guard'
|
# require 'guard/guard'
|
||||||
|
#
|
||||||
module ::Guard
|
# module ::Guard
|
||||||
class Breaking < ::Guard::Guard
|
# class Breaking < ::Guard::Guard
|
||||||
def run_all
|
# def run_all
|
||||||
raise "Fool !"
|
# raise "Fool !"
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
|
#
|
||||||
group "exceptional" do
|
# group "exceptional" do
|
||||||
guard :breaking
|
# guard :breaking
|
||||||
end
|
# end
|
24
lib/guard.rb
24
lib/guard.rb
@ -29,17 +29,15 @@ module Guard
|
|||||||
Interactor.init_signal_traps
|
Interactor.init_signal_traps
|
||||||
Dsl.evaluate_guardfile(options)
|
Dsl.evaluate_guardfile(options)
|
||||||
|
|
||||||
if guards.empty?
|
listener.on_change do |files|
|
||||||
UI.error "No guards found in Guardfile, please add at least one."
|
Dsl.revaluate_guardfile if Watcher.match_guardfile?(files)
|
||||||
else
|
|
||||||
listener.on_change do |files|
|
|
||||||
run { run_on_change_for_all_guards(files) } if Watcher.match_files?(guards, files)
|
|
||||||
end
|
|
||||||
|
|
||||||
UI.info "Guard is now watching at '#{Dir.pwd}'"
|
run { run_on_change_for_all_guards(files) } if Watcher.match_files?(guards, files)
|
||||||
guards.each { |guard| supervised_task(guard, :start) }
|
|
||||||
listener.start
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
UI.info "Guard is now watching at '#{Dir.pwd}'"
|
||||||
|
guards.each { |guard| supervised_task(guard, :start) }
|
||||||
|
listener.start
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_on_change_for_all_guards(files)
|
def run_on_change_for_all_guards(files)
|
||||||
@ -79,8 +77,12 @@ module Guard
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_guard(name, watchers = [], options = {})
|
def add_guard(name, watchers = [], options = {})
|
||||||
guard_class = get_guard_class(name)
|
if name.downcase == 'ego'
|
||||||
@guards << guard_class.new(watchers, options)
|
UI.deprecation("Guard::Ego is now part of Guard you can removed it from your Guardfile.")
|
||||||
|
else
|
||||||
|
guard_class = get_guard_class(name)
|
||||||
|
@guards << guard_class.new(watchers, options)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_guard_class(name)
|
def get_guard_class(name)
|
||||||
|
@ -2,12 +2,22 @@ module Guard
|
|||||||
class Dsl
|
class Dsl
|
||||||
class << self
|
class << self
|
||||||
@@options = nil
|
@@options = nil
|
||||||
|
|
||||||
def evaluate_guardfile(options = {})
|
def evaluate_guardfile(options = {})
|
||||||
options.is_a?(Hash) or raise ArgumentError.new("evaluate_guardfile not passed a Hash!")
|
options.is_a?(Hash) or raise ArgumentError.new("evaluate_guardfile not passed a Hash!")
|
||||||
|
|
||||||
@@options = options.dup
|
@@options = options.dup
|
||||||
instance_eval_guardfile(fetch_guardfile_contents)
|
instance_eval_guardfile(fetch_guardfile_contents)
|
||||||
|
|
||||||
|
UI.error "No guards found in Guardfile, please add at least one." if ::Guard.guards.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def revaluate_guardfile
|
||||||
|
::Guard.guards.clear
|
||||||
|
Dsl.evaluate_guardfile(@@options)
|
||||||
|
msg = "Guardfile has been re-evaluated."
|
||||||
|
UI.info(msg)
|
||||||
|
Notifier.notify(msg)
|
||||||
end
|
end
|
||||||
|
|
||||||
def instance_eval_guardfile(contents)
|
def instance_eval_guardfile(contents)
|
||||||
@ -69,6 +79,10 @@ module Guard
|
|||||||
@@options ? @@options[:guardfile_contents] : ""
|
@@options ? @@options[:guardfile_contents] : ""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def guardfile_path
|
||||||
|
@@options ? @@options[:guardfile_path] : ""
|
||||||
|
end
|
||||||
|
|
||||||
def guardfile_contents_usable?
|
def guardfile_contents_usable?
|
||||||
guardfile_contents && guardfile_contents.size >= 'guard :a'.size # smallest guard-definition
|
guardfile_contents && guardfile_contents.size >= 'guard :a'.size # smallest guard-definition
|
||||||
end
|
end
|
||||||
|
@ -16,6 +16,13 @@ module Guard
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def deprecation(message, options = {})
|
||||||
|
unless ENV["GUARD_ENV"] == "test"
|
||||||
|
reset_line if options[:reset]
|
||||||
|
puts "#{color('DEPRECATION:', ';31')} #{message}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def debug(message, options = {})
|
def debug(message, options = {})
|
||||||
unless ENV["GUARD_ENV"] == "test"
|
unless ENV["GUARD_ENV"] == "test"
|
||||||
reset_line if options[:reset]
|
reset_line if options[:reset]
|
||||||
@ -48,7 +55,7 @@ module Guard
|
|||||||
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
||||||
require 'Win32/Console/ANSI'
|
require 'Win32/Console/ANSI'
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
@color_enabled = false
|
@color_enabled = false
|
||||||
info "You must 'gem install win32console' to use color on Windows"
|
info "You must 'gem install win32console' to use color on Windows"
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
module Guard
|
module Guard
|
||||||
class Watcher
|
class Watcher
|
||||||
attr_accessor :pattern, :action
|
attr_accessor :pattern, :action
|
||||||
|
|
||||||
def initialize(pattern, action = nil)
|
def initialize(pattern, action = nil)
|
||||||
@pattern, @action = pattern, action
|
@pattern, @action = pattern, action
|
||||||
@@warning_printed ||= false
|
@@warning_printed ||= false
|
||||||
|
|
||||||
# deprecation warning
|
# deprecation warning
|
||||||
if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/
|
if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/
|
||||||
unless @@warning_printed
|
unless @@warning_printed
|
||||||
@ -17,7 +17,7 @@ module Guard
|
|||||||
@pattern = Regexp.new(@pattern)
|
@pattern = Regexp.new(@pattern)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.match_files(guard, files)
|
def self.match_files(guard, files)
|
||||||
guard.watchers.inject([]) do |paths, watcher|
|
guard.watchers.inject([]) do |paths, watcher|
|
||||||
files.each do |file|
|
files.each do |file|
|
||||||
@ -33,7 +33,7 @@ module Guard
|
|||||||
paths.flatten.map { |p| p.to_s }
|
paths.flatten.map { |p| p.to_s }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.match_files?(guards, files)
|
def self.match_files?(guards, files)
|
||||||
guards.any? do |guard|
|
guards.any? do |guard|
|
||||||
guard.watchers.any? do |watcher|
|
guard.watchers.any? do |watcher|
|
||||||
@ -41,7 +41,7 @@ module Guard
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def match_file?(file)
|
def match_file?(file)
|
||||||
if @pattern.is_a?(Regexp)
|
if @pattern.is_a?(Regexp)
|
||||||
file.match(@pattern)
|
file.match(@pattern)
|
||||||
@ -49,7 +49,11 @@ module Guard
|
|||||||
file == @pattern ? [file] : nil
|
file == @pattern ? [file] : nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.match_guardfile?(files)
|
||||||
|
files.any? { |file| "#{Dir.pwd}/#{file}" == Dsl.guardfile_path }
|
||||||
|
end
|
||||||
|
|
||||||
def call_action(matches)
|
def call_action(matches)
|
||||||
begin
|
begin
|
||||||
@action.arity > 0 ? @action.call(matches) : @action.call
|
@action.arity > 0 ? @action.call(matches) : @action.call
|
||||||
@ -57,6 +61,6 @@ module Guard
|
|||||||
UI.error "Problem with watch action!\n#{e.message}\n\n#{e.backtrace.join("\n")}"
|
UI.error "Problem with watch action!\n#{e.message}\n\n#{e.backtrace.join("\n")}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Guard::DslDescriber do
|
describe Guard::DslDescriber do
|
||||||
|
before(:each) { ::Guard.stub!(:guards).and_return([mock('Guard')]) }
|
||||||
subject { described_class }
|
subject { described_class }
|
||||||
|
|
||||||
|
|
||||||
it 'should evaluate a Guardfile and create the right structure' do
|
it 'should evaluate a Guardfile and create the right structure' do
|
||||||
mixed_guardfile_string = <<-GUARD
|
mixed_guardfile_string = <<-GUARD
|
||||||
guard 'test', :a => :b do
|
guard 'test', :a => :b do
|
||||||
|
@ -6,13 +6,11 @@ describe Guard::Dsl do
|
|||||||
@local_guardfile_path = File.join(Dir.pwd, 'Guardfile')
|
@local_guardfile_path = File.join(Dir.pwd, 'Guardfile')
|
||||||
@home_guardfile_path = File.expand_path(File.join("~", ".Guardfile"))
|
@home_guardfile_path = File.expand_path(File.join("~", ".Guardfile"))
|
||||||
::Guard.stub!(:options).and_return(:debug => true)
|
::Guard.stub!(:options).and_return(:debug => true)
|
||||||
|
::Guard.stub!(:guards).and_return([mock('Guard')])
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "it should select the correct data source for Guardfile" do
|
describe "it should select the correct data source for Guardfile" do
|
||||||
|
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
|
||||||
before(:each) do
|
|
||||||
::Guard::Dsl.stub!(:instance_eval_guardfile)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should use a string for initializing" do
|
it "should use a string for initializing" do
|
||||||
Guard::UI.should_not_receive(:error)
|
Guard::UI.should_not_receive(:error)
|
||||||
@ -60,10 +58,15 @@ describe Guard::Dsl do
|
|||||||
lambda { subject.evaluate_guardfile }.should raise_error
|
lambda { subject.evaluate_guardfile }.should raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "displays an error message when no guard are defined in Guardfile" do
|
||||||
|
::Guard::Dsl.stub!(:instance_eval_guardfile)
|
||||||
|
::Guard.stub!(:guards).and_return([])
|
||||||
|
Guard::UI.should_receive(:error)
|
||||||
|
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
|
||||||
|
end
|
||||||
|
|
||||||
describe "it should correctly read data from its valid data source" do
|
describe "it should correctly read data from its valid data source" do
|
||||||
before(:each) do
|
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
|
||||||
::Guard::Dsl.stub!(:instance_eval_guardfile)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should read correctly from a string" do
|
it "should read correctly from a string" do
|
||||||
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
|
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
|
||||||
@ -86,9 +89,7 @@ describe Guard::Dsl do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "It should correctly throw errors when initializing with invalid data" do
|
describe "It should correctly throw errors when initializing with invalid data" do
|
||||||
before(:each) do
|
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
|
||||||
::Guard::Dsl.stub!(:instance_eval_guardfile)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should raise error when there's a problem reading a file" do
|
it "should raise error when there's a problem reading a file" do
|
||||||
File.stub!(:exist?).with('/def/Guardfile') { true }
|
File.stub!(:exist?).with('/def/Guardfile') { true }
|
||||||
@ -118,7 +119,6 @@ describe Guard::Dsl do
|
|||||||
lambda { subject.evaluate_guardfile(:guardfile_contents => "") }.should raise_error
|
lambda { subject.evaluate_guardfile(:guardfile_contents => "") }.should raise_error
|
||||||
lambda { subject.evaluate_guardfile(:guardfile_contents => nil) }.should raise_error
|
lambda { subject.evaluate_guardfile(:guardfile_contents => nil) }.should raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "displays an error message when Guardfile is not valid" do
|
it "displays an error message when Guardfile is not valid" do
|
||||||
@ -127,13 +127,22 @@ describe Guard::Dsl do
|
|||||||
lambda { subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string ) }.should raise_error
|
lambda { subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string ) }.should raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".revaluate_guardfile" do
|
||||||
|
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
|
||||||
|
|
||||||
|
it "resets already definded guards before calling evaluate_guardfile" do
|
||||||
|
subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string)
|
||||||
|
::Guard.guards.should_not be_empty
|
||||||
|
::Guard::Dsl.should_receive(:evaluate_guardfile)
|
||||||
|
subject.revaluate_guardfile
|
||||||
|
::Guard.guards.should be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe ".guardfile_default_path" do
|
describe ".guardfile_default_path" do
|
||||||
let(:local_path) { File.join(Dir.pwd, 'Guardfile') }
|
let(:local_path) { File.join(Dir.pwd, 'Guardfile') }
|
||||||
let(:user_path) { File.expand_path(File.join("~", '.Guardfile')) }
|
let(:user_path) { File.expand_path(File.join("~", '.Guardfile')) }
|
||||||
|
before(:each) { File.stub(:exist? => false) }
|
||||||
before do
|
|
||||||
File.stub(:exist? => false)
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when there is a local Guardfile" do
|
context "when there is a local Guardfile" do
|
||||||
it "returns the path to the local Guardfile" do
|
it "returns the path to the local Guardfile" do
|
||||||
@ -156,7 +165,6 @@ describe Guard::Dsl do
|
|||||||
subject.guardfile_default_path.should == local_path
|
subject.guardfile_default_path.should == local_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".guardfile_include?" do
|
describe ".guardfile_include?" do
|
||||||
@ -226,7 +234,6 @@ describe Guard::Dsl do
|
|||||||
|
|
||||||
subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'")
|
subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'")
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#watch" do
|
describe "#watch" do
|
||||||
|
@ -210,4 +210,16 @@ describe Guard::Watcher do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".match_guardfile?" do
|
||||||
|
before(:all) { Guard::Dsl.stub(:guardfile_path) { Dir.pwd + '/Guardfile' } }
|
||||||
|
|
||||||
|
context "with files that match the Guardfile" do
|
||||||
|
specify { Guard::Watcher.match_guardfile?(['Guardfile', 'guard_rocks_spec.rb']).should be_true }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with no files that match the Guardfile" do
|
||||||
|
specify { Guard::Watcher.match_guardfile?(['guard_rocks.rb', 'guard_rocks_spec.rb']).should be_false }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user