Skip to content

Commit

Permalink
[Configuration] Allow additional import paths to be declared within t…
Browse files Browse the repository at this point in the history
…he compass configuration.
  • Loading branch information
chriseppstein committed Jun 27, 2009
1 parent ec6c120 commit 047be06
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
35 changes: 34 additions & 1 deletion lib/compass/configuration.rb
Expand Up @@ -13,7 +13,8 @@ class Configuration
:javascripts_dir,
:output_style,
:environment,
:http_images_path
:http_images_path,
:additional_import_paths
]

attr_accessor *ATTRIBUTES
Expand All @@ -38,6 +39,10 @@ def parse_string(contents, filename)
value = eval(prop.to_s, binding) rescue nil
self.send("#{prop}=", value) if value
end
if @added_import_paths
self.additional_import_paths ||= []
self.additional_import_paths += @added_import_paths
end
end

def set_all(options)
Expand Down Expand Up @@ -117,6 +122,15 @@ def css_path
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.
# 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.
Expand Down Expand Up @@ -186,12 +200,25 @@ def to_sass_plugin_options
Compass::Frameworks::ALL.each do |framework|
locations[framework.stylesheets_directory] = css_path || css_dir || "."
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[:style] = output_style if output_style
plugin_opts[:line_comments] = default_line_comments if environment
plugin_opts
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
engine_opts = {:load_paths => sass_load_paths}
engine_opts[:style] = output_style if output_style
Expand All @@ -205,6 +232,7 @@ def sass_load_paths
Compass::Frameworks::ALL.each do |framework|
load_paths << framework.stylesheets_directory
end
load_paths += resolve_additional_import_paths
load_paths
end

Expand All @@ -215,6 +243,7 @@ def reset!
end
@asset_cache_buster = nil
@asset_host = nil
@added_import_paths = nil
self.required_libraries = []
end

Expand All @@ -223,6 +252,10 @@ def require(lib)
super
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

module ConfigHelpers
Expand Down
26 changes: 26 additions & 0 deletions test/configuration_test.rb
Expand Up @@ -63,4 +63,30 @@ def test_serialization_fails_with_asset_cache_buster_set
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

0 comments on commit 047be06

Please sign in to comment.