diff --git a/features/command_line.feature b/features/command_line.feature index f0be79b1..06e0060c 100644 --- a/features/command_line.feature +++ b/features/command_line.feature @@ -145,6 +145,7 @@ Feature: Command Line Scenario: Basic help When I run: compass help Then I should see the following "primary" commands: + | clean | | compile | | create | | init | @@ -179,6 +180,27 @@ Feature: Command Line And I run: compass compile And a css file tmp/layout.css is reported overwritten + Scenario: Cleaning a project + Given I am using the existing project in test/fixtures/stylesheets/compass + When I run: compass compile + And I run: compass clean + Then the following files are reported removed: + | .sass-cache/ | + | tmp/border_radius.css | + | tmp/box.css | + | tmp/box_shadow.css | + | tmp/columns.css | + | tmp/fonts.css | + | images/flag-s03c3b29b35.png | + And the following files are removed: + | .sass-cache/ | + | tmp/border_radius.css | + | tmp/box.css | + | tmp/box_shadow.css | + | tmp/columns.css | + | tmp/fonts.css | + | images/flag-s03c3b29b35.png | + Scenario: Watching a project for changes Given ruby supports fork Given I am using the existing project in test/fixtures/stylesheets/compass @@ -218,7 +240,6 @@ Feature: Command Line | sass_dir | sass | | css_dir | assets/css | - @now Scenario Outline: Print out a configuration value Given I am using the existing project in test/fixtures/stylesheets/compass When I run: compass config -p diff --git a/features/step_definitions/command_line_steps.rb b/features/step_definitions/command_line_steps.rb index 8811693d..5bddd7a7 100644 --- a/features/step_definitions/command_line_steps.rb +++ b/features/step_definitions/command_line_steps.rb @@ -116,10 +116,30 @@ Then /^a directory ([^ ]+) is (not )?created$/ do |directory, negated| File.directory?(directory).should == !negated end +Then /an? \w+ file ([^ ]+) is (not )?removed/ do |filename, negated| + File.exists?(filename).should == !!negated +end + Then /an? \w+ file ([^ ]+) is (not )?created/ do |filename, negated| File.exists?(filename).should == !negated end +Then "the following files are reported removed:" do |table| + table.rows.each do |css_file| + Then %Q{a css file #{css_file.first} is reported removed} + end +end + +Then "the following files are removed:" do |table| + table.rows.each do |css_file| + Then %Q{a css file #{css_file.first} is removed} + end +end + +Then /an? \w+ file ([^ ]+) is reported removed/ do |filename| + @last_result.should =~ /remove.*#{Regexp.escape(filename)}/ +end + Then /an? \w+ file ([^ ]+) is reported created/ do |filename| @last_result.should =~ /create.*#{Regexp.escape(filename)}/ end diff --git a/lib/compass/commands.rb b/lib/compass/commands.rb index f4230ef4..42ca37ab 100644 --- a/lib/compass/commands.rb +++ b/lib/compass/commands.rb @@ -4,7 +4,7 @@ end require 'compass/commands/registry' %w(base generate_grid_background default help list_frameworks project_base - update_project watch_project create_project imports installer_command + update_project watch_project create_project clean_project imports installer_command print_version project_stats stamp_pattern sprite validate_project write_configuration interactive unpack_extension).each do |lib| require "compass/commands/#{lib}" diff --git a/lib/compass/commands/clean_project.rb b/lib/compass/commands/clean_project.rb new file mode 100644 index 00000000..e6792326 --- /dev/null +++ b/lib/compass/commands/clean_project.rb @@ -0,0 +1,79 @@ +require 'compass/commands/project_base' +require 'compass/compiler' + +module Compass + module Commands + module CleanProjectOptionsParser + def set_options(opts) + opts.banner = %Q{ + Usage: compass clean [path/to/project] [options] + + Description: + Remove generated files and the sass cache. + + Options: + }.split("\n").map{|l| l.gsub(/^ */,'')}.join("\n") + + super + end + end + + class CleanProject < UpdateProject + + register :clean + + def initialize(working_path, options) + super + assert_project_directory_exists! + end + + def perform + compiler = new_compiler_instance + compiler.clean! + Compass::SpriteImporter.find_all_sprite_map_files(Compass.configuration.images_path).each do |sprite| + remove sprite + end + end + + def determine_cache_location + Compass.configuration.cache_path || Sass::Plugin.options[:cache_location] || File.join(working_path, ".sass-cache") + end + + class << self + def option_parser(arguments) + parser = Compass::Exec::CommandOptionParser.new(arguments) + parser.extend(Compass::Exec::GlobalOptionsParser) + parser.extend(Compass::Exec::ProjectOptionsParser) + parser.extend(CleanProjectOptionsParser) + end + + def usage + option_parser([]).to_s + end + + def primary; true; end + + def description(command) + "Remove generated files and the sass cache" + end + + def parse!(arguments) + parser = option_parser(arguments) + parser.parse! + parse_arguments!(parser, arguments) + parser.options + end + + def parse_arguments!(parser, arguments) + if arguments.size > 0 + parser.options[:project_name] = arguments.shift if File.directory?(arguments.first) + unless arguments.empty? + parser.options[:sass_files] = arguments.dup + parser.options[:force] = true + end + end + end + end + end + end +end diff --git a/lib/compass/sprite_importer.rb b/lib/compass/sprite_importer.rb index 459c20e6..3dd1bd3e 100644 --- a/lib/compass/sprite_importer.rb +++ b/lib/compass/sprite_importer.rb @@ -4,7 +4,14 @@ module Compass VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png} VALID_EXTENSIONS = ['.png'] - + + # finds all sprite files + def self.find_all_sprite_map_files(path) + hex = "[0-9a-f]" + glob = "*-{,s}#{hex*10}{#{VALID_EXTENSIONS.join(",")}}" + Dir.glob(File.join(path, "**", glob)) + end + def self.load(uri, options) klass = Compass::SpriteImporter.new klass.uri, klass.options = uri, options