Skip to content

Commit

Permalink
Add a new command to clean up generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseppstein committed Jun 12, 2011
1 parent 2be7567 commit 39df74c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
23 changes: 22 additions & 1 deletion features/command_line.feature
Expand Up @@ -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 |
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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>
Expand Down
20 changes: 20 additions & 0 deletions features/step_definitions/command_line_steps.rb
Expand Up @@ -116,10 +116,30 @@
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
Expand Down
2 changes: 1 addition & 1 deletion lib/compass/commands.rb
Expand Up @@ -4,7 +4,7 @@ module Compass::Commands
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}"
Expand Down
79 changes: 79 additions & 0 deletions 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
9 changes: 8 additions & 1 deletion lib/compass/sprite_importer.rb
Expand Up @@ -4,7 +4,14 @@ class SpriteImporter < Sass::Importers::Base
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
Expand Down

0 comments on commit 39df74c

Please sign in to comment.