Move project configuration reading out of the command infrastructure and into the configuration helpers.

This commit is contained in:
Chris Eppstein 2009-09-02 10:47:21 -07:00
parent 54a459f28e
commit 60ad1e2425
5 changed files with 37 additions and 28 deletions

View File

@ -7,7 +7,7 @@ module Compass
def configure!
Compass.add_configuration(installer.default_configuration)
read_project_configuration
Compass.add_project_configuration
Compass.add_configuration(options)
Compass.add_configuration(installer.completed_configuration)
if File.exists?(Compass.configuration.extensions_path)

View File

@ -22,14 +22,18 @@ module Compass
protected
def configure!
read_project_configuration
add_project_configuration
if File.exists?(Compass.configuration.extensions_path)
Compass::Frameworks.discover(Compass.configuration.extensions_path)
end
end
def add_project_configuration
Compass.add_project_configuration(options[:configuration_file])
end
def projectize(path)
File.join(project_directory, separate(path))
Compass.projectize(path)
end
def project_directory
@ -48,31 +52,6 @@ module Compass
Compass.configuration.images_dir
end
# Read the configuration file for this project
def read_project_configuration
if (file = detect_configuration_file) && File.readable?(file)
Compass.add_configuration(file)
end
end
def explicit_config_file_must_be_readable?
true
end
# TODO: Deprecate the src/config.rb location.
KNOWN_CONFIG_LOCATIONS = [".compass/config.rb", "config/compass.config", "config.rb", "src/config.rb"]
# Finds the configuration file, if it exists in a known location.
def detect_configuration_file
if options[:configuration_file]
if explicit_config_file_must_be_readable? && !File.readable?(options[:configuration_file])
raise Compass::Error, "Configuration file, #{file}, not found or not readable."
end
return options[:configuration_file]
end
KNOWN_CONFIG_LOCATIONS.map{|f| projectize(f)}.detect{|f| File.exists?(f)}
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.")

View File

@ -11,6 +11,10 @@ module Compass
assert_project_directory_exists!
end
def add_project_configuration
Compass.add_project_configuration
end
def perform
installer.write_configuration_files(options[:configuration_file])
end

View File

@ -53,6 +53,28 @@ module Compass
def sass_engine_options
configuration.to_sass_engine_options
end
# Read the configuration file for this project
def add_project_configuration(configuration_file_path = nil)
configuration_file_path ||= detect_configuration_file
Compass.add_configuration(configuration_file_path) if configuration_file_path
end
# Returns a full path to the relative path to the project directory
def projectize(path, project_path = nil)
project_path ||= configuration.project_path
File.join(project_path, *path.split('/'))
end
# TODO: Deprecate the src/config.rb location.
KNOWN_CONFIG_LOCATIONS = [".compass/config.rb", "config/compass.config", "config.rb", "src/config.rb"]
# Finds the configuration file, if it exists in a known location.
def detect_configuration_file(project_path = nil)
possible_files = KNOWN_CONFIG_LOCATIONS.map{|f| projectize(f, project_path) }
possible_files.detect{|f| File.exists?(f)}
end
end
end

View File

@ -13,6 +13,7 @@ module Compass
data.parse(config_file)
data
end
def new_from_string(contents, filename)
data = Data.new
data.parse_string(contents, filename)
@ -23,6 +24,9 @@ module Compass
module InstanceMethods
# parses a configuration file which is a ruby script
def parse(config_file)
unless File.readable?(config_file)
raise Compass::Error, "Configuration file, #{config_file}, not found or not readable."
end
open(config_file) do |f|
parse_string(f.read, config_file)
end