diff --git a/lib/compass.rb b/lib/compass.rb index 02503443d2..121f55707c 100644 --- a/lib/compass.rb +++ b/lib/compass.rb @@ -1,21 +1,7 @@ -require File.join(File.dirname(__FILE__), 'compass', 'dependencies') - -def assert_sass_version(obj) - unless obj.respond_to?(:version) && obj.version[:major] == 2 && obj.version[:minor] >= 1 - raise LoadError.new("Compass requires Haml version 2.1 or greater.") - end -end - -begin - assert_sass_version(Sass) -rescue LoadError - require 'haml' - assert_sass_version(Haml) +module Compass end -require File.join(File.dirname(__FILE__), 'sass_extensions') - -['core_ext', 'version'].each do |file| +['dependencies', 'sass_extensions', 'core_ext', 'version'].each do |file| require File.join(File.dirname(__FILE__), 'compass', file) end @@ -32,7 +18,6 @@ def lib_directory require File.join(File.dirname(__FILE__), 'compass', 'configuration') require File.join(File.dirname(__FILE__), 'compass', 'frameworks') +require File.join(File.dirname(__FILE__), 'compass', 'app_integration') -# make sure we're running inside Merb -require File.join(File.dirname(__FILE__), 'compass', 'merb') if defined?(Merb::Plugins) diff --git a/lib/compass/app_integration.rb b/lib/compass/app_integration.rb new file mode 100644 index 0000000000..c6b00f7b8c --- /dev/null +++ b/lib/compass/app_integration.rb @@ -0,0 +1,3 @@ + +# If we're running inside Merb +require File.join(File.dirname(__FILE__), 'app_integration', 'merb') if defined?(Merb::Plugins) diff --git a/lib/compass/merb.rb b/lib/compass/app_integration/merb.rb similarity index 100% rename from lib/compass/merb.rb rename to lib/compass/app_integration/merb.rb diff --git a/lib/compass/sass_extensions.rb b/lib/compass/sass_extensions.rb new file mode 100644 index 0000000000..986be47f6b --- /dev/null +++ b/lib/compass/sass_extensions.rb @@ -0,0 +1,5 @@ +module Compass::SassExtensions +end + +require File.join(File.dirname(__FILE__), 'sass_extensions', 'functions') +require File.join(File.dirname(__FILE__), 'sass_extensions', 'monkey_patches') diff --git a/lib/compass/sass_extensions/functions.rb b/lib/compass/sass_extensions/functions.rb new file mode 100644 index 0000000000..f33513b6f3 --- /dev/null +++ b/lib/compass/sass_extensions/functions.rb @@ -0,0 +1,17 @@ +module Compass::SassExtensions::Functions +end + +['nest', 'enumerate', 'image_url'].each do |func| + require File.join(File.dirname(__FILE__), 'functions', func) +end + +module Sass::Script::Functions + include Compass::SassExtensions::Functions::Nest + include Compass::SassExtensions::Functions::Enumerate + include Compass::SassExtensions::Functions::ImageUrl +end + +# Wierd that this has to be re-included to pick up sub-modules. Ruby bug? +class Sass::Script::Functions::EvaluationContext + include Sass::Script::Functions +end diff --git a/lib/compass/sass_extensions/functions/enumerate.rb b/lib/compass/sass_extensions/functions/enumerate.rb new file mode 100644 index 0000000000..88d3240166 --- /dev/null +++ b/lib/compass/sass_extensions/functions/enumerate.rb @@ -0,0 +1,6 @@ +module Compass::SassExtensions::Functions::Enumerate + def enumerate(prefix, from, through) + selectors = (from.value..through.value).map{|i| "#{prefix.value}-#{i}"}.join(", ") + Sass::Script::String.new(selectors) + end +end \ No newline at end of file diff --git a/lib/compass/sass_extensions/functions/image_url.rb b/lib/compass/sass_extensions/functions/image_url.rb new file mode 100644 index 0000000000..f82ca026b3 --- /dev/null +++ b/lib/compass/sass_extensions/functions/image_url.rb @@ -0,0 +1,43 @@ +module Compass::SassExtensions::Functions::ImageUrl + def image_url(path) + path = path.value # get to the string value of the literal. + if absolute_path?(path) + return Sass::Script::String.new("url(#{path})") + end + http_images_path = if Compass.configuration.http_images_path == :relative + compute_relative_path + else + Compass.configuration.http_images_path + end + + real_path = if Compass.configuration.images_dir + File.join(Compass.configuration.project_path, Compass.configuration.images_dir, path) + end + + if http_images_path + http_images_path = "#{http_images_path}/" unless http_images_path[-1..-1] == "/" + path = "#{http_images_path}#{path}" + end + + if real_path && File.exists?(real_path) + path += "?#{File.mtime(real_path).strftime("%s")}" + elsif real_path + $stderr.puts "WARNING: '#{File.basename(path)}' was not found in #{File.dirname(real_path)}" + end + + Sass::Script::String.new("url(#{path})") + end + + private + + def absolute_path?(path) + path[0..0] == "/" || path[0..3] == "http" + end + + def compute_relative_path + if (target_css_file = options[:css_filename]) + images_path = File.join(Compass.configuration.project_path, Compass.configuration.images_dir) + Pathname.new(images_path).relative_path_from(Pathname.new(File.dirname(target_css_file))).to_s + end + end +end \ No newline at end of file diff --git a/lib/compass/sass_extensions/functions/nest.rb b/lib/compass/sass_extensions/functions/nest.rb new file mode 100644 index 0000000000..8981f79e8f --- /dev/null +++ b/lib/compass/sass_extensions/functions/nest.rb @@ -0,0 +1,12 @@ +module Compass::SassExtensions::Functions::Nest + COMMA_SEPARATOR = /\s*,\s*/ + + def nest(*arguments) + nested = arguments.map{|a| a.value}.inject do |memo,arg| + ancestors = memo.split(COMMA_SEPARATOR) + descendants = arg.split(COMMA_SEPARATOR) + ancestors.map{|a| descendants.map{|d| "#{a} #{d}"}.join(", ")}.join(", ") + end + Sass::Script::String.new(nested) + end +end \ No newline at end of file diff --git a/lib/compass/sass_extensions/monkey_patches.rb b/lib/compass/sass_extensions/monkey_patches.rb new file mode 100644 index 0000000000..de54a75c43 --- /dev/null +++ b/lib/compass/sass_extensions/monkey_patches.rb @@ -0,0 +1,3 @@ +['stylesheet_updating'].each do |patch| + require File.join(File.dirname(__FILE__), 'monkey_patches', patch) +end \ No newline at end of file diff --git a/lib/compass/sass_extensions/monkey_patches/stylesheet_updating.rb b/lib/compass/sass_extensions/monkey_patches/stylesheet_updating.rb new file mode 100644 index 0000000000..1cb091189f --- /dev/null +++ b/lib/compass/sass_extensions/monkey_patches/stylesheet_updating.rb @@ -0,0 +1,23 @@ +require 'sass/plugin' + +# XXX: We can remove this monkeypatch once Sass 2.2 is released. +module Sass::Plugin + class << self + unless method_defined?(:exact_stylesheet_needs_update?) + def stylesheet_needs_update?(name, template_path, css_path) + css_file = css_filename(name, css_path) + template_file = template_filename(name, template_path) + exact_stylesheet_needs_update?(css_file, template_file) + end + def exact_stylesheet_needs_update?(css_file, template_file) + if !File.exists?(css_file) + return true + else + css_mtime = File.mtime(css_file) + File.mtime(template_file) > css_mtime || + dependencies(template_file).any?(&dependency_updated?(css_mtime)) + end + end + end + end +end \ No newline at end of file diff --git a/lib/sass_extensions.rb b/lib/sass_extensions.rb deleted file mode 100644 index 053a90633e..0000000000 --- a/lib/sass_extensions.rb +++ /dev/null @@ -1,83 +0,0 @@ -require 'sass/plugin' - -module Sass::Script::Functions - COMMA_SEPARATOR = /\s*,\s*/ - - def nest(*arguments) - nested = arguments.map{|a| a.value}.inject do |memo,arg| - ancestors = memo.split(COMMA_SEPARATOR) - descendants = arg.split(COMMA_SEPARATOR) - ancestors.map{|a| descendants.map{|d| "#{a} #{d}"}.join(", ")}.join(", ") - end - Sass::Script::String.new(nested) - end - - def enumerate(prefix, from, through) - selectors = (from.value..through.value).map{|i| "#{prefix.value}-#{i}"}.join(", ") - Sass::Script::String.new(selectors) - end - - def image_url(path) - path = path.value # get to the string value of the literal. - if absolute_path?(path) - return Sass::Script::String.new("url(#{path})") - end - http_images_path = if Compass.configuration.http_images_path == :relative - compute_relative_path - else - Compass.configuration.http_images_path - end - - real_path = if Compass.configuration.images_dir - File.join(Compass.configuration.project_path, Compass.configuration.images_dir, path) - end - - if http_images_path - http_images_path = "#{http_images_path}/" unless http_images_path[-1..-1] == "/" - path = "#{http_images_path}#{path}" - end - - if real_path && File.exists?(real_path) - path += "?#{File.mtime(real_path).strftime("%s")}" - elsif real_path - $stderr.puts "WARNING: '#{File.basename(path)}' was not found in #{File.dirname(real_path)}" - end - - Sass::Script::String.new("url(#{path})") - end - - private - - def absolute_path?(path) - path[0..0] == "/" || path[0..3] == "http" - end - - def compute_relative_path - if (target_css_file = options[:css_filename]) - images_path = File.join(Compass.configuration.project_path, Compass.configuration.images_dir) - Pathname.new(images_path).relative_path_from(Pathname.new(File.dirname(target_css_file))).to_s - end - end -end - -# XXX: We can remove this check and monkeypatch once Sass 2.2 is released. -module Sass::Plugin - class << self - unless method_defined?(:exact_stylesheet_needs_update?) - def stylesheet_needs_update?(name, template_path, css_path) - css_file = css_filename(name, css_path) - template_file = template_filename(name, template_path) - exact_stylesheet_needs_update?(css_file, template_file) - end - def exact_stylesheet_needs_update?(css_file, template_file) - if !File.exists?(css_file) - return true - else - css_mtime = File.mtime(css_file) - File.mtime(template_file) > css_mtime || - dependencies(template_file).any?(&dependency_updated?(css_mtime)) - end - end - end - end -end \ No newline at end of file diff --git a/test/command_line_test.rb b/test/command_line_test.rb index a03027f232..a94ee22067 100644 --- a/test/command_line_test.rb +++ b/test/command_line_test.rb @@ -70,7 +70,7 @@ def test_rails_install within_tmp_directory do generate_rails_app("compass_rails") Dir.chdir "compass_rails" do - compass("--rails", ".") do |responder| + compass("--rails", '--trace', ".") do |responder| responder.respond_to "Is this OK? (Y/n) ", :with => "Y" responder.respond_to "Emit compiled stylesheets to public/stylesheets/compiled/? (Y/n) ", :with => "Y" end diff --git a/test/compass_test.rb b/test/compass_test.rb index f790b4dcd2..a700ace31d 100644 --- a/test/compass_test.rb +++ b/test/compass_test.rb @@ -18,9 +18,10 @@ def teardown_fixtures(*project_names) end end - def test_blueprint_generates_no_files + def test_empty_project + # With no sass files, we should have no css files. within_project(:empty) do |proj| - return unless File.exists?(proj.css_path) + return unless proj.css_path && File.exists?(proj.css_path) Dir.new(proj.css_path).each do |f| fail "This file should not have been generated: #{f}" unless f == "." || f == ".." end @@ -80,11 +81,13 @@ def assert_renders_correctly(*arguments) def within_project(project_name) @current_project = project_name - Compass.configuration.parse(configuration_file(project_name)) + Compass.configuration.parse(configuration_file(project_name)) if File.exists?(configuration_file(project_name)) Compass.configuration.project_path = project_path(project_name) args = Compass.configuration.to_compiler_arguments(:logger => Compass::NullLogger.new) - compiler = Compass::Compiler.new *args - compiler.run + if Compass.configuration.sass_path && File.exists?(Compass.configuration.sass_path) + compiler = Compass::Compiler.new *args + compiler.run + end yield Compass.configuration rescue save_output(project_name) diff --git a/test/fixtures/stylesheets/blueprint/images/grid.png b/test/fixtures/stylesheets/blueprint/images/grid.png new file mode 100644 index 0000000000..76aaa851a1 Binary files /dev/null and b/test/fixtures/stylesheets/blueprint/images/grid.png differ diff --git a/test/sass_extensions_test.rb b/test/sass_extensions_test.rb index d709464c50..98136c9a74 100644 --- a/test/sass_extensions_test.rb +++ b/test/sass_extensions_test.rb @@ -1,5 +1,4 @@ require File.dirname(__FILE__)+'/test_helper' -require 'compass' class SassExtensionsTest < Test::Unit::TestCase def test_simple diff --git a/test/test_case_helper.rb b/test/test_case_helper.rb new file mode 100644 index 0000000000..3c4f95e0fb --- /dev/null +++ b/test/test_case_helper.rb @@ -0,0 +1,13 @@ +module Compass + module TestCaseHelper + def absolutize(path) + if path.blank? + File.dirname(__FILE__) + elsif path[0] == ?/ + "#{File.dirname(__FILE__)}#{path}" + else + "#{File.dirname(__FILE__)}/#{path}" + end + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 2d77698655..6e0ed21d71 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,15 +1,4 @@ -# allows testing with edge Rails by creating a test/rails symlink -RAILS_ROOT = linked_rails = File.dirname(__FILE__) + '/rails' -RAILS_ENV = 'test' - need_gems = false -if File.exists?(linked_rails) && !$:.include?(linked_rails + '/activesupport/lib') - puts "[ using linked Rails ]" - $:.unshift linked_rails + '/activesupport/lib' - $:.unshift linked_rails + '/actionpack/lib' -else - need_gems = true -end # allows testing with edge Haml by creating a test/haml symlink linked_haml = File.dirname(__FILE__) + '/haml' @@ -24,22 +13,8 @@ require 'rubygems' if need_gems -require 'action_controller' -require 'action_view' - +require 'compass' require 'test/unit' -module Compass - module TestCaseHelper - def absolutize(path) - if path.blank? - File.dirname(__FILE__) - elsif path[0] == ?/ - "#{File.dirname(__FILE__)}#{path}" - else - "#{File.dirname(__FILE__)}/#{path}" - end - end - end -end +require File.join(File.dirname(__FILE__), 'test_case_helper')