Refactored the Configuration Singleton and the UpdateProject command so that it now relies more heavily on the configuration object.
This commit is contained in:
parent
a8f6648a64
commit
95fd94428a
@ -42,6 +42,14 @@ module Compass
|
||||
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
|
||||
|
||||
def determine_project_name(working_path, options)
|
||||
|
@ -12,48 +12,12 @@ module Compass
|
||||
|
||||
def perform
|
||||
read_project_configuration
|
||||
default_options = { :style => default_output_style }
|
||||
compilation_options = default_options.merge(options).merge(:load_paths => sass_load_paths)
|
||||
Compass.configuration.set_maybe(options)
|
||||
Compass.configuration.set_defaults!
|
||||
Compass::Compiler.new(working_path,
|
||||
projectize(project_src_subdirectory),
|
||||
projectize(project_css_subdirectory),
|
||||
compilation_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
|
||||
projectize(Compass.configuration.sass_dir),
|
||||
projectize(Compass.configuration.css_dir),
|
||||
Compass.sass_engine_options).run
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -3,7 +3,20 @@ require 'singleton'
|
||||
module Compass
|
||||
class Configuration
|
||||
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
|
||||
self.required_libraries = []
|
||||
@ -19,28 +32,103 @@ module Compass
|
||||
|
||||
def parse_string(contents, 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
|
||||
self.send("#{prop}=", value) if value
|
||||
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
|
||||
contents = ""
|
||||
required_libraries.each do |lib|
|
||||
contents << %Q{require '#{lib}'\n}
|
||||
end
|
||||
contents << "# Require any additional compass plugins here.\n"
|
||||
contents << "\n" if required_libraries.any?
|
||||
[:css_dir, :sass_dir, :images_dir, :javascripts_dir].each do |prop|
|
||||
ATTRIBUTES.each do |prop|
|
||||
value = send(prop)
|
||||
contents << %Q(#{prop} = "#{value}"\n) if value
|
||||
unless value.nil?
|
||||
contents << %Q(#{prop} = #{value.inspect}\n)
|
||||
end
|
||||
end
|
||||
contents
|
||||
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.
|
||||
def reset!
|
||||
[:project_path, :css_dir, :sass_dir, :images_dir, :javascripts_dir].each do |attr|
|
||||
ATTRIBUTES.each do |attr|
|
||||
send("#{attr}=", nil)
|
||||
end
|
||||
self.required_libraries = []
|
||||
@ -62,13 +150,7 @@ module Compass
|
||||
end
|
||||
|
||||
def sass_plugin_configuration
|
||||
proj_sass_path = File.join(configuration.project_path, configuration.sass_dir)
|
||||
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}
|
||||
configuration.to_sass_plugin_options
|
||||
end
|
||||
|
||||
def configure_sass_plugin!
|
||||
@ -76,14 +158,7 @@ module Compass
|
||||
end
|
||||
|
||||
def sass_engine_options
|
||||
load_paths = []
|
||||
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}
|
||||
configuration.to_sass_engine_options
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -6,6 +6,7 @@ class ConfigurationTest < Test::Unit::TestCase
|
||||
contents = <<-CONFIG
|
||||
require 'compass'
|
||||
require 'sass'
|
||||
# Require any additional compass plugins here.
|
||||
|
||||
css_dir = "css"
|
||||
sass_dir = "sass"
|
||||
|
Loading…
Reference in New Issue
Block a user