[Examples] Refactor the module and file loading for Sass Extensions and application integration. Fixed broken unit tests.

This commit is contained in:
Chris Eppstein 2009-05-19 11:27:29 -07:00
parent f1832d07ab
commit 2b3b781c33
17 changed files with 139 additions and 135 deletions

View File

@ -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)

View File

@ -0,0 +1,3 @@
# If we're running inside Merb
require File.join(File.dirname(__FILE__), 'app_integration', 'merb') if defined?(Merb::Plugins)

View File

@ -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')

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,3 @@
['stylesheet_updating'].each do |patch|
require File.join(File.dirname(__FILE__), 'monkey_patches', patch)
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

View File

@ -1,5 +1,4 @@
require File.dirname(__FILE__)+'/test_helper'
require 'compass'
class SassExtensionsTest < Test::Unit::TestCase
def test_simple

13
test/test_case_helper.rb Normal file
View File

@ -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

View File

@ -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 @@ end
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')