diff --git a/lib/guard/compass.rb b/lib/guard/compass.rb index 62b9666..e3d6885 100644 --- a/lib/guard/compass.rb +++ b/lib/guard/compass.rb @@ -1,5 +1,6 @@ require 'guard' require 'guard/guard' +require 'guard/watcher' require 'guard/reporter' require 'compass' @@ -23,8 +24,9 @@ def initialize(watchers = [], options = {}) # Compile all the sass|scss stylesheets def start - create_updater UI.info "Guard::Compass is watching at your stylesheets." + load_compass_configuration + create_updater true end @@ -35,6 +37,7 @@ def stop # Reload the configuration def reload + load_compass_configuration create_updater true end @@ -59,7 +62,8 @@ def perform end end - def create_updater + def load_compass_configuration + ::Compass.default_configuration if(options[:configuration_file]) filepath = Pathname.new(options[:configuration_file]) if(filepath.relative?) @@ -72,6 +76,18 @@ def create_updater reporter.failure "Compass configuration file not found: " + filepath + "\nPlease check Guard configuration." end end + + ::Compass.configuration.sass_dir ||= "#{options[:workdir]}/src" + watchers.clear + watchers.push Watcher.new("^#{ File.expand_path(::Compass.configuration.sass_dir, options[:workdir]) }/.*") + if(options[:configuration_file]) + watchers.push Watcher.new("^#{options[:configuration_file]}$") + elsif conf_file = ::Compass.detect_configuration_file(options[:workdir]) + watchers.push Watcher.new("^#{conf_file}$") + end + end + + def create_updater @updater = ::Compass::Commands::UpdateProject.new(@options[:workdir] , @options) valid_sass_path? end diff --git a/spec/guard/compass_spec.rb b/spec/guard/compass_spec.rb index 992874e..1cbbb5a 100644 --- a/spec/guard/compass_spec.rb +++ b/spec/guard/compass_spec.rb @@ -2,20 +2,35 @@ require 'spec_helper' require 'guard/compass' +def create_fixture(name) + FileUtils.mkdir(TMP_PATH) if ! File.exists? TMP_PATH + @project_path = "#{TMP_PATH}/#{name}" + FileUtils.cp_r "#{FIXTURES_PATH}/#{name}", TMP_PATH + subject.options.merge!(:workdir => @project_path) +end + +def remove_fixtures + FileUtils.rm_rf(TMP_PATH) +end + describe Guard::Compass do - subject { Guard::Compass.new } - - it "has a reporter" do - subject.reporter.should_not be_nil - end - it "might be initialized with options" do - g = Guard::Compass.new([], :workdir => 'test', :configuration_file => 'test_also') - g.options[:workdir].should == 'test' - g.options[:configuration_file].should == 'test_also' + after :each do + ::Compass.reset_configuration! end describe "In a standard project" do + subject { Guard::Compass.new } + + it "has a reporter" do + subject.reporter.should_not be_nil + end + + it "might be initialized with options" do + g = Guard::Compass.new([], :workdir => 'test', :configuration_file => 'test_also') + g.options[:workdir].should == 'test' + g.options[:configuration_file].should == 'test_also' + end before :each do create_fixture(:compass_prj) @@ -23,6 +38,7 @@ after :each do remove_fixtures + ::Compass.reset_configuration! end describe "start" do @@ -52,6 +68,7 @@ after :each do subject.stop + ::Compass.reset_configuration! end describe "the updater" do @@ -91,6 +108,8 @@ end describe "with custom configuration and locations" do + subject { Guard::Compass.new } + before :each do create_fixture(:custom_config_file) end @@ -98,6 +117,7 @@ after :each do remove_fixtures subject.stop + ::Compass.reset_configuration! end it "configure Compass correctly with an absolute path" do @@ -123,6 +143,8 @@ end describe "without config file" do + subject { Guard::Compass.new } + before :each do create_fixture(:no_config_file) subject.start @@ -131,6 +153,7 @@ after :each do remove_fixtures subject.stop + ::Compass.reset_configuration! end describe "run_on_change" do @@ -144,6 +167,8 @@ end describe "with a bad directory configuration" do + subject { Guard::Compass.new } + before :each do create_fixture(:bad_src_directory) subject.reporter.stub!(:failure).with("Sass files src directory not found: #{@project_path}/src\nPlease check your Compass configuration.") @@ -153,6 +178,7 @@ after :each do remove_fixtures subject.stop + ::Compass.reset_configuration! end it "rebuilds failed to build sass" do @@ -173,6 +199,7 @@ after :each do subject.stop remove_fixtures + ::Compass.reset_configuration! end it "reports an error" do @@ -184,15 +211,55 @@ end -private - def create_fixture(name) - FileUtils.mkdir(TMP_PATH) if ! File.exists? TMP_PATH - @project_path = "#{TMP_PATH}/#{name}" - FileUtils.cp_r "#{FIXTURES_PATH}/#{name}", TMP_PATH - subject.options.merge!(:workdir => @project_path) - end - - def remove_fixtures - FileUtils.rm_rf(TMP_PATH) + describe "Watchers creation" do + + after :each do + remove_fixtures + ::Compass.reset_configuration! + end + + describe "in standard project" do + subject { + Guard::Compass.new([], :workdir => "#{TMP_PATH}/compass_prj") + } + + before :each do + create_fixture :compass_prj + end + + it "should have some watchers" do + subject.options.should eql :workdir => "#{TMP_PATH}/compass_prj" + subject.start + subject.watchers.size.should(eql(2), subject.watchers.inspect) + subject.watchers.first.pattern.should == "^#{@project_path}/src/.*" + end + end + + describe "in customized project" do + subject { + Guard::Compass.new([], :workdir => "#{TMP_PATH}/custom_config_file", :configuration_file => 'another_config_location/config.rb') + } + + before :each do + create_fixture :custom_config_file + end + + it "should have some watchers" do + subject.start + subject.watchers.size.should(eql(2), subject.watchers.inspect) + subject.watchers.first.pattern.should == "^#{@project_path}/another_src_location/.*" + end + end end end + +def create_fixture(name) + FileUtils.mkdir(TMP_PATH) if ! File.exists? TMP_PATH + @project_path = "#{TMP_PATH}/#{name}" + FileUtils.cp_r "#{FIXTURES_PATH}/#{name}", TMP_PATH + subject.options.merge!(:workdir => @project_path) +end + +def remove_fixtures + FileUtils.rm_rf(TMP_PATH) +end