Skip to content

Commit

Permalink
[Examples] Refactor the module and file loading for Sass Extensions a…
Browse files Browse the repository at this point in the history
…nd application integration. Fixed broken unit tests.
  • Loading branch information
chriseppstein committed Jun 20, 2009
1 parent f1832d0 commit 2b3b781
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 135 deletions.
21 changes: 3 additions & 18 deletions 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

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

3 changes: 3 additions & 0 deletions 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)
File renamed without changes.
5 changes: 5 additions & 0 deletions 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')
17 changes: 17 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions 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
43 changes: 43 additions & 0 deletions 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
12 changes: 12 additions & 0 deletions 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
3 changes: 3 additions & 0 deletions 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
23 changes: 23 additions & 0 deletions 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
83 changes: 0 additions & 83 deletions lib/sass_extensions.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/command_line_test.rb
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions test/compass_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Binary file added test/fixtures/stylesheets/blueprint/images/grid.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion 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
Expand Down
13 changes: 13 additions & 0 deletions 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
29 changes: 2 additions & 27 deletions 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'
Expand All @@ -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')

0 comments on commit 2b3b781

Please sign in to comment.