Refactored the Configuration Singleton and the UpdateProject command so that it now relies more heavily on the configuration object.

This commit is contained in:
Chris Eppstein 2009-02-20 17:11:22 -08:00
parent a8f6648a64
commit 95fd94428a
4 changed files with 109 additions and 61 deletions

View File

@ -42,6 +42,14 @@ module Compass
end end
end end
def assert_project_directory_exists!
if File.exists?(project_directory) && !File.directory?(project_directory)
raise Compass::FilesystemConflict.new("#{project_directory} is not a directory.")
elsif !File.directory?(project_directory)
raise Compass::Error.new("#{project_directory} does not exist.")
end
end
private private
def determine_project_name(working_path, options) def determine_project_name(working_path, options)

View File

@ -12,48 +12,12 @@ module Compass
def perform def perform
read_project_configuration read_project_configuration
default_options = { :style => default_output_style } Compass.configuration.set_maybe(options)
compilation_options = default_options.merge(options).merge(:load_paths => sass_load_paths) Compass.configuration.set_defaults!
Compass::Compiler.new(working_path, Compass::Compiler.new(working_path,
projectize(project_src_subdirectory), projectize(Compass.configuration.sass_dir),
projectize(project_css_subdirectory), projectize(Compass.configuration.css_dir),
compilation_options).run Compass.sass_engine_options).run
end
def default_output_style
if options[:environment] == :development
:expanded
else
:compact
end
end
# where to load sass files from
def sass_load_paths
[project_src_directory] + Compass::Frameworks::ALL.map{|f| f.stylesheets_directory}
end
# The subdirectory where the sass source is kept.
def project_src_subdirectory
Compass.configuration.sass_dir ||= options[:sass_dir] || "src"
end
# The subdirectory where the css output is kept.
def project_css_subdirectory
Compass.configuration.css_dir ||= options[:css_dir] || "stylesheets"
end
# The directory where the project source files are located.
def project_src_directory
@project_src_directory ||= separate("#{project_directory}/#{project_src_subdirectory}")
end
def assert_project_directory_exists!
if File.exists?(project_directory) && !File.directory?(project_directory)
raise Compass::FilesystemConflict.new("#{project_directory} is not a directory.")
elsif !File.directory?(project_directory)
raise Compass::Error.new("#{project_directory} does not exist.")
end
end end
end end

View File

@ -3,7 +3,20 @@ require 'singleton'
module Compass module Compass
class Configuration class Configuration
include Singleton include Singleton
attr_accessor :project_path, :css_dir, :sass_dir, :images_dir, :javascripts_dir, :required_libraries
ATTRIBUTES = [
:project_path,
:css_dir,
:sass_dir,
:images_dir,
:javascripts_dir,
:output_style,
:environment
]
attr_accessor *ATTRIBUTES
attr_accessor :required_libraries
def initialize def initialize
self.required_libraries = [] self.required_libraries = []
@ -19,28 +32,103 @@ module Compass
def parse_string(contents, filename) def parse_string(contents, filename)
eval(contents, binding, filename) eval(contents, binding, filename)
[:css_dir, :sass_dir, :images_dir, :javascripts_dir].each do |prop| ATTRIBUTES.each do |prop|
value = eval(prop.to_s, binding) rescue nil value = eval(prop.to_s, binding) rescue nil
self.send("#{prop}=", value) if value self.send("#{prop}=", value) if value
end end
end end
def set_all(options)
ATTRIBUTES.each do |a|
self.send("#{a}=", options[a]) if options.has_key?(a)
end
end
def set_maybe(options)
ATTRIBUTES.each do |a|
self.send("#{a}=", options[a]) if options[a]
end
end
def default_all(options)
ATTRIBUTES.each do |a|
self.send("#{a}=", options[a]) unless self.send(a)
end
end
def set_defaults!
default_all(ATTRIBUTES.inject({}){|m, a| m[a] = default_for(a); m})
end
def default_for(attribute)
method = "default_#{attribute}".to_sym
self.send(method) if respond_to?(method)
end
def default_sass_dir
"src"
end
def default_css_dir
"stylesheets"
end
def default_output_style
if environment == :development
:expanded
else
:compact
end
end
def serialize def serialize
contents = "" contents = ""
required_libraries.each do |lib| required_libraries.each do |lib|
contents << %Q{require '#{lib}'\n} contents << %Q{require '#{lib}'\n}
end end
contents << "# Require any additional compass plugins here.\n"
contents << "\n" if required_libraries.any? contents << "\n" if required_libraries.any?
[:css_dir, :sass_dir, :images_dir, :javascripts_dir].each do |prop| ATTRIBUTES.each do |prop|
value = send(prop) value = send(prop)
contents << %Q(#{prop} = "#{value}"\n) if value unless value.nil?
contents << %Q(#{prop} = #{value.inspect}\n)
end
end end
contents contents
end end
def to_sass_plugin_options
if project_path && sass_dir && css_dir
proj_sass_path = File.join(project_path, sass_dir)
proj_css_path = File.join(project_path, css_dir)
locations = {proj_sass_path => proj_css_path}
else
locations = {}
end
Compass::Frameworks::ALL.each do |framework|
locations[framework.stylesheets_directory] = proj_css_path || css_dir || "."
end
plugin_opts = {:template_location => locations}
plugin_opts[:style] = output_style if output_style
plugin_opts
end
def to_sass_engine_options
load_paths = []
if project_path && sass_dir
load_paths << File.join(project_path, sass_dir)
end
Compass::Frameworks::ALL.each do |framework|
load_paths << framework.stylesheets_directory
end
engine_opts = {:load_paths => load_paths}
engine_opts[:style] = output_style if output_style
engine_opts
end
# Support for testing. # Support for testing.
def reset! def reset!
[:project_path, :css_dir, :sass_dir, :images_dir, :javascripts_dir].each do |attr| ATTRIBUTES.each do |attr|
send("#{attr}=", nil) send("#{attr}=", nil)
end end
self.required_libraries = [] self.required_libraries = []
@ -62,13 +150,7 @@ module Compass
end end
def sass_plugin_configuration def sass_plugin_configuration
proj_sass_path = File.join(configuration.project_path, configuration.sass_dir) configuration.to_sass_plugin_options
proj_css_path = File.join(configuration.project_path, configuration.css_dir)
locations = {proj_sass_path => proj_css_path}
Compass::Frameworks::ALL.each do |framework|
locations[framework.stylesheets_directory] = proj_css_path
end
{:template_location => locations}
end end
def configure_sass_plugin! def configure_sass_plugin!
@ -76,14 +158,7 @@ module Compass
end end
def sass_engine_options def sass_engine_options
load_paths = [] configuration.to_sass_engine_options
if configuration.project_path && configuration.sass_dir
load_paths << File.join(configuration.project_path, configuration.sass_dir)
end
Compass::Frameworks::ALL.each do |framework|
load_paths << framework.stylesheets_directory
end
{:load_paths => load_paths}
end end
end end

View File

@ -6,6 +6,7 @@ class ConfigurationTest < Test::Unit::TestCase
contents = <<-CONFIG contents = <<-CONFIG
require 'compass' require 'compass'
require 'sass' require 'sass'
# Require any additional compass plugins here.
css_dir = "css" css_dir = "css"
sass_dir = "sass" sass_dir = "sass"