From 95fd94428a1c868e2f285583fc17d1d7503ff9c1 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Fri, 20 Feb 2009 17:11:22 -0800 Subject: [PATCH] Refactored the Configuration Singleton and the UpdateProject command so that it now relies more heavily on the configuration object. --- lib/compass/commands/project_base.rb | 8 ++ lib/compass/commands/update_project.rb | 46 ++-------- lib/compass/configuration.rb | 115 ++++++++++++++++++++----- test/configuration_test.rb | 1 + 4 files changed, 109 insertions(+), 61 deletions(-) diff --git a/lib/compass/commands/project_base.rb b/lib/compass/commands/project_base.rb index 887559a8..e5ba5b58 100644 --- a/lib/compass/commands/project_base.rb +++ b/lib/compass/commands/project_base.rb @@ -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) diff --git a/lib/compass/commands/update_project.rb b/lib/compass/commands/update_project.rb index adddf2bf..7c989573 100644 --- a/lib/compass/commands/update_project.rb +++ b/lib/compass/commands/update_project.rb @@ -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 diff --git a/lib/compass/configuration.rb b/lib/compass/configuration.rb index ecfd8681..72b6fbfe 100644 --- a/lib/compass/configuration.rb +++ b/lib/compass/configuration.rb @@ -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 diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 00fd98dc..9cd1195c 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -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"