From 2b3b781c33e0afde18fa4ce67dbb4187e74cc07a Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Tue, 19 May 2009 11:27:29 -0700 Subject: [PATCH] [Examples] Refactor the module and file loading for Sass Extensions and application integration. Fixed broken unit tests. --- lib/compass.rb | 21 +---- lib/compass/app_integration.rb | 3 + lib/compass/{ => app_integration}/merb.rb | 0 lib/compass/sass_extensions.rb | 5 ++ lib/compass/sass_extensions/functions.rb | 17 ++++ .../sass_extensions/functions/enumerate.rb | 6 ++ .../sass_extensions/functions/image_url.rb | 43 +++++++++ lib/compass/sass_extensions/functions/nest.rb | 12 +++ lib/compass/sass_extensions/monkey_patches.rb | 3 + .../monkey_patches/stylesheet_updating.rb | 23 +++++ lib/sass_extensions.rb | 83 ------------------ test/command_line_test.rb | 2 +- test/compass_test.rb | 13 +-- .../stylesheets/blueprint/images/grid.png | Bin 0 -> 199 bytes test/sass_extensions_test.rb | 1 - test/test_case_helper.rb | 13 +++ test/test_helper.rb | 29 +----- 17 files changed, 139 insertions(+), 135 deletions(-) create mode 100644 lib/compass/app_integration.rb rename lib/compass/{ => app_integration}/merb.rb (100%) create mode 100644 lib/compass/sass_extensions.rb create mode 100644 lib/compass/sass_extensions/functions.rb create mode 100644 lib/compass/sass_extensions/functions/enumerate.rb create mode 100644 lib/compass/sass_extensions/functions/image_url.rb create mode 100644 lib/compass/sass_extensions/functions/nest.rb create mode 100644 lib/compass/sass_extensions/monkey_patches.rb create mode 100644 lib/compass/sass_extensions/monkey_patches/stylesheet_updating.rb delete mode 100644 lib/sass_extensions.rb create mode 100644 test/fixtures/stylesheets/blueprint/images/grid.png create mode 100644 test/test_case_helper.rb diff --git a/lib/compass.rb b/lib/compass.rb index 02503443..121f5570 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 +module Compass end -begin - assert_sass_version(Sass) -rescue LoadError - require 'haml' - assert_sass_version(Haml) -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 @@ end 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 00000000..c6b00f7b --- /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 00000000..986be47f --- /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 00000000..f33513b6 --- /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 00000000..88d32401 --- /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 00000000..f82ca026 --- /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 00000000..8981f79e --- /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 00000000..de54a75c --- /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 00000000..1cb09118 --- /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 053a9063..00000000 --- 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 a03027f2..a94ee220 100644 --- a/test/command_line_test.rb +++ b/test/command_line_test.rb @@ -70,7 +70,7 @@ FRAMEWORKS 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 f790b4dc..a700ace3 100644 --- a/test/compass_test.rb +++ b/test/compass_test.rb @@ -18,9 +18,10 @@ class CompassTest < Test::Unit::TestCase 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 @@ private 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 0000000000000000000000000000000000000000..76aaa851a17551b07c608797105bf6e4ba204148 GIT binary patch literal 199 zcmeAS@N?(olHy`uVBq!ia0vp^8bB-}zy>7OUt9bGNUZ}$lIVGz#3>Ma-jF$ZsY0(YncrlhC+|`Fx8c*$A-Tz zURV0ZXYJA_XUfmr?XQ{3sL1TZ*22RfDPW|K;&7s20;a&iIhQ2XzO{Q?D=6sy=8i=j W|D0$1qSt{|GI+ZBxvX