From f318e93764a25c067c3dde533bc53ce3e8a8842d Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sat, 18 Dec 2010 11:26:42 -0800 Subject: [PATCH] Expose CLI config parameters at configuration parse time. --- doc-src/content/CHANGELOG.markdown | 4 ++++ lib/compass/commands/project_base.rb | 2 +- lib/compass/configuration/helpers.rb | 8 ++++---- lib/compass/configuration/inheritance.rb | 10 ++++++++++ lib/compass/configuration/serialization.rb | 12 ++++++++---- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/doc-src/content/CHANGELOG.markdown b/doc-src/content/CHANGELOG.markdown index 4c429f37..349b9f14 100644 --- a/doc-src/content/CHANGELOG.markdown +++ b/doc-src/content/CHANGELOG.markdown @@ -22,6 +22,10 @@ The Documentation for the [latest preview release](http://beta.compass-style.org optional based on Compass's legacy support settings. * Added the ability to piggy back on compass's watcher within your configuration file. See the [configuration reference](/help/tutorials/configuration-reference/) for details. +* The options passed to the CLI can now be inspected within the compass configuration file. + The CLI options will still override the values set within the config file, but they might + inform other values. For instance `compass compile -e production` will have the environment + parameter preset to `:production` so that you can set other values in the project accordingly. 0.11.alpha.4 (12/08/2010) diff --git a/lib/compass/commands/project_base.rb b/lib/compass/commands/project_base.rb index 75310861..df1259d1 100644 --- a/lib/compass/commands/project_base.rb +++ b/lib/compass/commands/project_base.rb @@ -28,7 +28,7 @@ module Compass end def add_project_configuration - Compass.add_project_configuration(options[:configuration_file]) do + Compass.add_project_configuration(options[:configuration_file], :defaults => Compass.configuration_for(options, "cli_defaults")) do options[:project_type] end end diff --git a/lib/compass/configuration/helpers.rb b/lib/compass/configuration/helpers.rb index 0dd06de5..2f037d8a 100644 --- a/lib/compass/configuration/helpers.rb +++ b/lib/compass/configuration/helpers.rb @@ -27,18 +27,18 @@ module Compass @configuration = data end - def configuration_for(config, filename = nil) + def configuration_for(config, filename = nil, defaults = nil) if config.nil? nil elsif config.is_a?(Compass::Configuration::Data) config elsif config.respond_to?(:read) filename ||= config.to_s if config.is_a?(Pathname) - Compass::Configuration::Data.new_from_string(config.read, filename) + Compass::Configuration::Data.new_from_string(config.read, filename, defaults) elsif config.is_a?(Hash) Compass::Configuration::Data.new(filename, config) elsif config.is_a?(String) - Compass::Configuration::Data.new_from_file(config) + Compass::Configuration::Data.new_from_file(config, defaults) elsif config.is_a?(Symbol) Compass::AppIntegration.lookup(config).configuration else @@ -76,7 +76,7 @@ module Compass options = args.last.is_a?(Hash) ? args.pop : {} configuration_file_path = args.shift || detect_configuration_file raise ArgumentError, "Too many arguments" if args.any? - if data = configuration_for(configuration_file_path) + if data = configuration_for(configuration_file_path, nil, configuration_for(options[:defaults])) if data.raw_project_type add_configuration(data.raw_project_type.to_sym) elsif options[:project_type] diff --git a/lib/compass/configuration/inheritance.rb b/lib/compass/configuration/inheritance.rb index e792923b..b44954ca 100644 --- a/lib/compass/configuration/inheritance.rb +++ b/lib/compass/configuration/inheritance.rb @@ -87,6 +87,16 @@ module Compass self end + def reset_inheritance! + self.inherited_data = nil + end + + def with_defaults(data) + inherit_from!(data) + yield + reset_inheritance! + end + def unset!(attribute) @set_attributes ||= {} send("#{attribute}=", nil) diff --git a/lib/compass/configuration/serialization.rb b/lib/compass/configuration/serialization.rb index 1301c379..3e3606f5 100644 --- a/lib/compass/configuration/serialization.rb +++ b/lib/compass/configuration/serialization.rb @@ -8,15 +8,19 @@ module Compass end module ClassMethods - def new_from_file(config_file) + def new_from_file(config_file, defaults) data = Data.new(config_file) - data._parse(config_file) + data.with_defaults(defaults) do + data._parse(config_file) + end data end - def new_from_string(contents, filename) + def new_from_string(contents, filename, defaults) data = Data.new(filename) - data.parse_string(contents, filename) + data.with_defaults(defaults) do + data.parse_string(contents, filename) + end data end end