Add a new command to clean up generated files

This commit is contained in:
Chris Eppstein 2011-06-11 21:56:08 -07:00
parent 2be7567bb2
commit 39df74c55a
5 changed files with 130 additions and 3 deletions

View File

@ -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 <property>

View File

@ -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

View File

@ -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}"

View File

@ -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

View File

@ -5,6 +5,13 @@ module Compass
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