Move the compile action from the Actions module to the Compiler class.

Refactor the compass project compiler to be easier to read.
This commit is contained in:
Chris Eppstein 2010-01-02 08:54:19 -08:00
parent 42d5295e52
commit 5707a3627f
2 changed files with 69 additions and 39 deletions

View File

@ -63,26 +63,6 @@ module Compass
ERB.new(contents).result(ctx)
end
# Compile one Sass file
def compile(sass_filename, css_filename, options)
if options[:force] || Sass::Plugin.exact_stylesheet_needs_update?(css_filename, sass_filename)
logger.record :compile, basename(sass_filename) unless options[:quiet]
engine = ::Sass::Engine.new(open(sass_filename).read,
:filename => sass_filename,
:line_comments => options[:line_comments],
:style => options[:style],
:css_filename => css_filename,
:load_paths => options[:load_paths],
:cache_location => options[:cache_location])
css_content = logger.red do
engine.render
end
write_file(css_filename, css_content, options.merge(:force => true))
else
logger.record :unchanged, basename(sass_filename) unless options[:quiet]
end
end
def remove(file_name)
if File.exists?(file_name)
File.unlink file_name

View File

@ -10,7 +10,11 @@ module Compass
self.from, self.to = from, to
self.logger = options.delete(:logger)
self.options = options
self.options[:cache_location] ||= File.join(from, ".sass-cache")
self.options[:cache_location] ||= determine_cache_location
end
def determine_cache_location
Compass.configuration.cache_path || Sass::Plugin.options[:cache_location] || File.join(working_path, ".sass-cache")
end
def sass_files(options = {})
@ -34,6 +38,7 @@ module Compass
css_files.map{|css_file| File.dirname(css_file)}.uniq.sort.sort_by{|d| d.length }
end
# Returns the sass file that needs to be compiled, if any.
def out_of_date?
Compass.configure_sass_plugin! unless Compass.sass_plugin_configured?
sass_files.zip(css_files).each do |sass_filename, css_filename|
@ -42,6 +47,7 @@ module Compass
false
end
# Determines if the configuration file is newer than any css file
def new_config?
config_file = Compass.detect_configuration_file
return false unless config_file
@ -52,34 +58,78 @@ module Compass
nil
end
def cache_location
Compass.configuration.cache_path || Sass::Plugin.options[:cache_location] || "./.sass-cache"
end
def run
if new_config?
FileUtils.rm_rf cache_location
# Wipe out the cache and force compilation if the configuration has changed.
FileUtils.rm_rf options[:cache_location]
options[:force] = true
end
# We use the Sass::Plugin to check dependencies so we have configure it.
Compass.configure_sass_plugin! unless Compass.sass_plugin_configured?
target_directories.each do |dir|
directory dir
end
# Make sure the target directories exist
target_directories.each {|dir| directory dir}
# Compile each sass file.
sass_files.zip(css_files).each do |sass_filename, css_filename|
begin
compile sass_filename, css_filename, options
compile_if_required sass_filename, css_filename
rescue Sass::SyntaxError => e
full_exception = Compass.configuration.environment == :development
logger.record :error, basename(sass_filename), "(Line #{e.sass_line}: #{e.message})"
contents = if Sass::SyntaxError.respond_to?(:exception_to_css)
Sass::SyntaxError.exception_to_css(e, :full_exception => full_exception)
else
Sass::Plugin.options[:full_exception] ||= Compass.configuration.environment == :development
Sass::Plugin.send(:exception_string, e)
end
write_file css_filename, contents, options.merge(:force => true)
handle_exception(sass_filename, css_filename, e)
end
end
end
def compile_if_required(sass_filename, css_filename)
if should_compile?(sass_filename, css_filename)
compile sass_filename, css_filename
else
logger.record :unchanged, basename(sass_filename) unless options[:quiet]
end
end
# Compile one Sass file
def compile(sass_filename, css_filename)
logger.record :compile, basename(sass_filename) unless options[:quiet]
css_content = logger.red do
engine(sass_filename, css_filename).render
end
write_file(css_filename, css_content, options.merge(:force => true))
end
def should_compile?(sass_filename, css_filename)
options[:force] || Sass::Plugin.exact_stylesheet_needs_update?(css_filename, sass_filename)
end
# A sass engine for compiling a single file.
def engine(sass_filename, css_filename)
opts = options.merge :filename => sass_filename, :css_filename => css_filename
Sass::Engine.new(open(sass_filename).read, opts)
end
# Place the syntax error into the target css file,
# formatted to display in the browser (in development mode)
# if there's an error.
def handle_exception(sass_filename, css_filename, e)
logger.record :error, basename(sass_filename), "(Line #{e.sass_line}: #{e.message})"
write_file css_filename, error_contents(e), options.merge(:force => true)
end
# Haml refactored this logic in 2.3, this is backwards compatibility for either one
def error_contents(e)
if Sass::SyntaxError.respond_to?(:exception_to_css)
Sass::SyntaxError.exception_to_css(e, :full_exception => show_full_exception?)
else
Sass::Plugin.options[:full_exception] ||= show_full_exception?
Sass::Plugin.send(:exception_string, e)
end
end
# We don't want to show the full exception in production environments.
def show_full_exception?
Compass.configuration.environment == :development
end
end
end