[Configuration] Allow additional import paths to be declared within the compass configuration.

This commit is contained in:
Chris Eppstein 2009-06-27 12:15:28 -07:00
parent ec6c12092e
commit 047be06a0a
2 changed files with 60 additions and 1 deletions

View File

@ -13,7 +13,8 @@ module Compass
:javascripts_dir, :javascripts_dir,
:output_style, :output_style,
:environment, :environment,
:http_images_path :http_images_path,
:additional_import_paths
] ]
attr_accessor *ATTRIBUTES attr_accessor *ATTRIBUTES
@ -38,6 +39,10 @@ module Compass
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
if @added_import_paths
self.additional_import_paths ||= []
self.additional_import_paths += @added_import_paths
end
end end
def set_all(options) def set_all(options)
@ -117,6 +122,15 @@ module Compass
end end
end end
def add_import_path(*paths)
# The @added_import_paths variable works around an issue where
# the additional_import_paths gets overwritten during parse
@added_import_paths ||= []
@added_import_paths += paths
self.additional_import_paths ||= []
self.additional_import_paths += paths
end
# When called with a block, defines the asset host url to be used. # When called with a block, defines the asset host url to be used.
# The block must return a string that starts with a protocol (E.g. http). # The block must return a string that starts with a protocol (E.g. http).
# The block will be passed the root-relative url of the asset. # The block will be passed the root-relative url of the asset.
@ -186,12 +200,25 @@ module Compass
Compass::Frameworks::ALL.each do |framework| Compass::Frameworks::ALL.each do |framework|
locations[framework.stylesheets_directory] = css_path || css_dir || "." locations[framework.stylesheets_directory] = css_path || css_dir || "."
end end
resolve_additional_import_paths.each do |additional_path|
locations[additional_path] = File.join(css_path || css_dir || ".", File.basename(additional_path))
end
plugin_opts = {:template_location => locations} plugin_opts = {:template_location => locations}
plugin_opts[:style] = output_style if output_style plugin_opts[:style] = output_style if output_style
plugin_opts[:line_comments] = default_line_comments if environment plugin_opts[:line_comments] = default_line_comments if environment
plugin_opts plugin_opts
end end
def resolve_additional_import_paths
(additional_import_paths || []).map do |path|
if project_path && !absolute_path?(path)
File.join(project_path, path)
else
path
end
end
end
def to_sass_engine_options def to_sass_engine_options
engine_opts = {:load_paths => sass_load_paths} engine_opts = {:load_paths => sass_load_paths}
engine_opts[:style] = output_style if output_style engine_opts[:style] = output_style if output_style
@ -205,6 +232,7 @@ module Compass
Compass::Frameworks::ALL.each do |framework| Compass::Frameworks::ALL.each do |framework|
load_paths << framework.stylesheets_directory load_paths << framework.stylesheets_directory
end end
load_paths += resolve_additional_import_paths
load_paths load_paths
end end
@ -215,6 +243,7 @@ module Compass
end end
@asset_cache_buster = nil @asset_cache_buster = nil
@asset_host = nil @asset_host = nil
@added_import_paths = nil
self.required_libraries = [] self.required_libraries = []
end end
@ -223,6 +252,10 @@ module Compass
super super
end end
def absolute_path?(path)
# This is only going to work on unix, gonna need a better implementation.
path.index(File::SEPARATOR) == 0
end
end end
module ConfigHelpers module ConfigHelpers

View File

@ -63,4 +63,30 @@ class ConfigurationTest < Test::Unit::TestCase
end end
end end
def test_additional_import_paths
contents = <<-CONFIG
project_path = "/home/chris/my_compass_project"
css_dir = "css"
additional_import_paths = ["../foo"]
add_import_path "/path/to/my/framework"
CONFIG
Compass.configuration.parse_string(contents, "test_additional_import_paths")
assert Compass.configuration.to_sass_engine_options[:load_paths].include?("/home/chris/my_compass_project/../foo")
assert Compass.configuration.to_sass_engine_options[:load_paths].include?("/path/to/my/framework"), Compass.configuration.to_sass_engine_options[:load_paths].inspect
assert_equal "/home/chris/my_compass_project/css/framework", Compass.configuration.to_sass_plugin_options[:template_location]["/path/to/my/framework"]
assert_equal "/home/chris/my_compass_project/css/foo", Compass.configuration.to_sass_plugin_options[:template_location]["/home/chris/my_compass_project/../foo"]
expected_serialization = <<EXPECTED
# Require any additional compass plugins here.
project_path = "/home/chris/my_compass_project"
css_dir = "css"
# To enable relative image paths using the images_url() function:
# http_images_path = :relative
additional_import_paths = ["../foo", "/path/to/my/framework"]
EXPECTED
assert_equal expected_serialization, Compass.configuration.serialize
end
end end