Skip to content

Commit c5eff2a

Browse files
committed
test pass importer moved over
1 parent f74f7a3 commit c5eff2a

File tree

16 files changed

+166
-158
lines changed

16 files changed

+166
-158
lines changed

cli/lib/compass.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def lib_directory
3333
configuration/file_data
3434
app_integration
3535
compiler
36-
sprite_importer
3736
).each do |lib|
3837
require "compass/#{lib}"
3938
end
39+
#tem
40+
require 'compass/sprites/importer'

cli/lib/compass/commands/sprite.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ def initialize(working_path, options)
3939

4040
def perform
4141
relative_uri = options[:uri].gsub(/^#{Compass.configuration.images_dir}\//, '')
42-
name = Compass::SpriteImporter.sprite_name(relative_uri)
43-
sprites = Compass::SpriteImporter.new
42+
name = Compass::Sprites::Importer.sprite_name(relative_uri)
43+
sprites = Compass::Sprites::Importer.new
4444
options[:output_file] ||= File.join(Compass.configuration.sass_path, "sprites", "_#{name}.#{Compass.configuration.preferred_syntax}")
4545
options[:skip_overrides] ||= false
46-
contents = Compass::SpriteImporter.content_for_images(relative_uri, name, options[:skip_overrides])
46+
contents = Compass::Sprites::Importer.content_for_images(relative_uri, name, options[:skip_overrides])
4747
if options[:output_file][-4..-1] != "scss"
4848
contents = Sass::Engine.new(contents, Compass.sass_engine_options.merge(:syntax => :scss)).to_tree.to_sass
4949
end

cli/lib/compass/sass_extensions/sprites.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ module Sprites
1616
require 'compass/sass_extensions/sprites/image_methods'
1717
require 'compass/sass_extensions/sprites/sprite_map'
1818
require 'compass/sass_extensions/sprites/engines'
19-
require 'compass/sprite_importer'
19+
require 'compass/sprites/importer'
2020

cli/lib/compass/sass_extensions/sprites/sprite_map.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class SpriteMap < Sass::Script::Value::Base
1515
# the path is relative to the <tt>images_path</tt> confguration option
1616
def self.from_uri(uri, context, kwargs)
1717
uri = uri.value
18-
path, name = Compass::SpriteImporter.path_and_name(uri)
19-
files = Compass::SpriteImporter.files(uri)
18+
path, name = Compass::Sprites::Importer.path_and_name(uri)
19+
files = Compass::Sprites::Importer.files(uri)
2020
sprites = files.map do |sprite|
2121
relative_name(sprite)
2222
end

core/lib/compass/configuration/adapters.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def sass_load_paths
8686
next p if p.respond_to?(:find_relative)
8787
importer.new(p.to_s)
8888
end
89-
load_paths << Compass::SpriteImporter.new
89+
load_paths << Compass::Sprites::Importer.new
9090
load_paths
9191
end
9292

sprites/lib/compass/sprites/importer.rb

+110-107
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,133 @@
11
require 'erb'
22
require 'compass'
3-
require 'compass/sprite_importer/binding'
4-
5-
6-
3+
require 'compass/sprites/importer/binding'
74

85
module Compass
9-
class SpriteImporter < Sass::Importers::Base
10-
VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/
11-
SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)}
12-
13-
TEMPLATE_FOLDER = File.join(File.expand_path('../', __FILE__), 'sprite_importer')
14-
CONTENT_TEMPLATE_FILE = File.join(TEMPLATE_FOLDER, 'content.erb')
15-
CONTENT_TEMPLATE = ERB.new(File.read(CONTENT_TEMPLATE_FILE))
16-
17-
18-
19-
# finds all sprite files
20-
def self.find_all_sprite_map_files(path)
21-
hex = "[0-9a-f]"
22-
glob = "*-s#{hex*10}{#{valid_extensions.join(",")}}"
23-
Sass::Util.glob(File.join(path, "**", glob))
24-
end
25-
26-
def find(uri, options)
27-
if uri =~ SPRITE_IMPORTER_REGEX
28-
return self.class.sass_engine(uri, self.class.sprite_name(uri), self, options)
6+
module Sprites
7+
class Importer < Sass::Importers::Base
8+
VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/
9+
SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)}
10+
11+
TEMPLATE_FOLDER = File.join(File.expand_path('../', __FILE__), 'importer')
12+
CONTENT_TEMPLATE_FILE = File.join(TEMPLATE_FOLDER, 'content.erb')
13+
CONTENT_TEMPLATE = ERB.new(File.read(CONTENT_TEMPLATE_FILE))
14+
15+
16+
17+
# finds all sprite files
18+
def self.find_all_sprite_map_files(path)
19+
hex = "[0-9a-f]"
20+
glob = "*-s#{hex*10}{#{valid_extensions.join(",")}}"
21+
Sass::Util.glob(File.join(path, "**", glob))
2922
end
30-
nil
31-
end
32-
33-
def find_relative(uri, base, options)
34-
nil
35-
end
36-
37-
def to_s
38-
self.class.name
39-
end
40-
41-
def hash
42-
self.class.name.hash
43-
end
44-
45-
def eql?(other)
46-
other.class == self.class
47-
end
48-
49-
def mtime(uri, options)
50-
self.class.files(uri).sort.inject(Time.at(0)) do |max_time, file|
51-
(t = File.mtime(file)) > max_time ? t : max_time
23+
24+
def find(uri, options)
25+
if uri =~ SPRITE_IMPORTER_REGEX
26+
return self.class.sass_engine(uri, self.class.sprite_name(uri), self, options)
27+
end
28+
nil
29+
end
30+
31+
def find_relative(uri, base, options)
32+
nil
33+
end
34+
35+
def to_s
36+
self.class.name
37+
end
38+
39+
def hash
40+
self.class.name.hash
5241
end
53-
end
5442

55-
def key(uri, options={})
56-
[self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)]
57-
end
43+
def key(uri, options={})
44+
[self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)]
45+
end
5846

59-
def public_url(*args)
60-
nil
61-
end
47+
def public_url(*args)
48+
nil
49+
end
6250

6351

64-
def self.path_and_name(uri)
65-
if uri =~ sprite_importer_regex_with_ext
66-
[$1, $3]
67-
else
68-
raise Compass::Error, "invalid sprite path"
52+
def eql?(other)
53+
other.class == self.class
54+
end
55+
56+
def mtime(uri, options)
57+
self.class.files(uri).sort.inject(Time.at(0)) do |max_time, file|
58+
(t = File.mtime(file)) > max_time ? t : max_time
59+
end
60+
end
61+
62+
def key(uri, options={})
63+
[self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)]
64+
end
65+
66+
def self.path_and_name(uri)
67+
if uri =~ sprite_importer_regex_with_ext
68+
[$1, $3]
69+
else
70+
raise Compass::Error, "invalid sprite path"
71+
end
6972
end
70-
end
71-
72-
# Name of this spite
73-
def self.sprite_name(uri)
74-
_, name = path_and_name(uri)
75-
name
76-
end
7773

78-
# The on-disk location of this sprite
79-
def self.path(uri)
80-
path, _ = path_and_name(uri)
81-
path
82-
end
83-
84-
# Returns the Glob of image files for the uri
85-
def self.files(uri)
86-
Compass.configuration.sprite_load_path.compact.each do |folder|
87-
files = Sass::Util.glob(File.join(folder, uri)).sort
88-
next if files.empty?
89-
return files
74+
# Name of this spite
75+
def self.sprite_name(uri)
76+
_, name = path_and_name(uri)
77+
name
9078
end
9179

92-
path = Compass.configuration.sprite_load_path.to_a.join(', ')
93-
raise Compass::SpriteException, %Q{No files were found in the load path matching "#{uri}". Your current load paths are: #{path}}
94-
end
80+
# The on-disk location of this sprite
81+
def self.path(uri)
82+
path, _ = path_and_name(uri)
83+
path
84+
end
85+
86+
# Returns the Glob of image files for the uri
87+
def self.files(uri)
88+
Compass.configuration.sprite_load_path.compact.each do |folder|
89+
files = Sass::Util.glob(File.join(folder, uri)).sort
90+
next if files.empty?
91+
return files
92+
end
93+
94+
path = Compass.configuration.sprite_load_path.to_a.join(', ')
95+
raise Compass::SpriteException, %Q{No files were found in the load path matching "#{uri}". Your current load paths are: #{path}}
96+
end
9597

96-
# Returns an Array of image names without the file extension
97-
def self.sprite_names(uri)
98-
files(uri).collect do |file|
99-
File.basename(file, '.png')
98+
# Returns an Array of image names without the file extension
99+
def self.sprite_names(uri)
100+
files(uri).collect do |file|
101+
File.basename(file, '.png')
102+
end
103+
end
104+
105+
# Returns the sass_options for this sprite
106+
def self.sass_options(uri, importer, options)
107+
options.merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer)
108+
end
109+
110+
# Returns a Sass::Engine for this sprite object
111+
def self.sass_engine(uri, name, importer, options)
112+
content = content_for_images(uri, name, options[:skip_overrides])
113+
Sass::Engine.new(content, sass_options(uri, importer, options))
100114
end
101-
end
102-
103-
# Returns the sass_options for this sprite
104-
def self.sass_options(uri, importer, options)
105-
options.merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer)
106-
end
107-
108-
# Returns a Sass::Engine for this sprite object
109-
def self.sass_engine(uri, name, importer, options)
110-
content = content_for_images(uri, name, options[:skip_overrides])
111-
Sass::Engine.new(content, sass_options(uri, importer, options))
112-
end
113115

114-
# Generates the Sass for this sprite file
115-
def self.content_for_images(uri, name, skip_overrides = false)
116-
binder = Compass::Sprites::Binding.new(:name => name, :uri => uri, :skip_overrides => skip_overrides, :sprite_names => sprite_names(uri), :files => files(uri))
117-
CONTENT_TEMPLATE.result(binder.get_binding)
118-
end
116+
# Generates the Sass for this sprite file
117+
def self.content_for_images(uri, name, skip_overrides = false)
118+
binder = Compass::Sprites::Binding.new(:name => name, :uri => uri, :skip_overrides => skip_overrides, :sprite_names => sprite_names(uri), :files => files(uri))
119+
CONTENT_TEMPLATE.result(binder.get_binding)
120+
end
119121

120-
private
122+
private
121123

122-
def self.valid_extensions
123-
@valid_extensions ||= SassExtensions::Sprites::SpriteMap.sprite_engine_class::VALID_EXTENSIONS
124-
end
124+
def self.valid_extensions
125+
@valid_extensions ||= SassExtensions::Sprites::SpriteMap.sprite_engine_class::VALID_EXTENSIONS
126+
end
125127

126-
def self.sprite_importer_regex_with_ext
127-
@importer_regex ||= %r{#{SPRITE_IMPORTER_REGEX}(#{valid_extensions.join('|')})}
128+
def self.sprite_importer_regex_with_ext
129+
@importer_regex ||= %r{#{SPRITE_IMPORTER_REGEX}(#{valid_extensions.join('|')})}
130+
end
128131
end
129132
end
130133
end

sprites/test/helpers/sprite_helper.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
module Compass::Sprite::Test::SpriteHelper
1+
module Compass::Sprites::Test::SpriteHelper
22
URI = "selectors/*.png"
33

44
def init_sprite_helper
5-
@images_proj_path = File.join(fixture_path, 'sprites', 'public')
5+
@images_proj_path = File.join(fixture_path, 'public')
66
@images_src_dir = 'images'
77
@images_src_path = File.join(@images_proj_path, @images_src_dir)
88
@images_tmp_dir = 'images-tmp'
99
@images_tmp_path = File.join(@images_proj_path, @images_tmp_dir)
1010
end
1111

1212
def sprite_map_test(options, uri = URI)
13-
importer = Compass::SpriteImporter.new
14-
path, name = Compass::SpriteImporter.path_and_name(uri)
15-
sprite_names = Compass::SpriteImporter.sprite_names(uri)
16-
sass_engine = Compass::SpriteImporter.sass_engine(uri, name, importer, options)
13+
importer = Compass::Sprites::Importer.new
14+
path, name = Compass::Sprites::Importer.path_and_name(uri)
15+
sprite_names = Compass::Sprites::Importer.sprite_names(uri)
16+
sass_engine = Compass::Sprites::Importer.sass_engine(uri, name, importer, options)
1717
map = Compass::SassExtensions::Sprites::SpriteMap.new(sprite_names.map{|n| uri.gsub('*', n)}, path, name, sass_engine, options)
1818
map.options = {:compass => {:logger => Compass::NullLogger.new}}
1919
map

sprites/test/integrations/sprites_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SpritesTest < Test::Unit::TestCase
88

99
def setup
1010
Compass.reset_configuration!
11-
@images_project_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'sprites', 'public'))
11+
@images_project_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'public'))
1212
@images_src_dir = 'images'
1313
@images_src_path = File.join(@images_project_path, @images_src_dir)
1414
@images_tmp_dir = 'images-tmp'

sprites/test/test_helper.rb

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
test_dir = File.dirname(__FILE__)
3-
$:.unshift(test_dir) unless $:.include?(test_dir)
4-
cli_dir = File.join(test_dir, '..', '..', 'cli', 'lib')
2+
::TEST_DIR = File.dirname(__FILE__)
3+
$:.unshift(::TEST_DIR) unless $:.include?(::TEST_DIR)
4+
cli_dir = File.join(::TEST_DIR, '..', '..', 'cli', 'lib')
55
$:.unshift(cli_dir) unless $:.include?(cli_dir)
6-
core_dir = File.join(test_dir, '..', '..', 'core', 'lib')
6+
core_dir = File.join(::TEST_DIR, '..', '..', 'core', 'lib')
77
$:.unshift(core_dir) unless $:.include?(core_dir)
88
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
99
$:.unshift(lib_dir) unless $:.include?(lib_dir)
@@ -13,9 +13,14 @@
1313
require 'compass/sprites'
1414
require 'test/unit'
1515
require "mocha/setup"
16-
17-
require File.join(test_dir, '..', '..', 'test', 'common', 'helpers')
18-
require File.join(test_dir, 'helpers', 'sprite_helper')
16+
module Compass
17+
module Sprites
18+
module Test
19+
end
20+
end
21+
end
22+
require File.join(::TEST_DIR, '..', '..', 'test', 'common', 'helpers')
23+
require File.join(::TEST_DIR, 'helpers', 'sprite_helper')
1924

2025
class Test::Unit::TestCase
2126
include Compass::Test::Diff
@@ -24,7 +29,7 @@ class Test::Unit::TestCase
2429
extend Compass::Test::TestCaseHelper::ClassMethods
2530

2631
def fixture_path
27-
File.join(test_dir, 'fixtures')
32+
File.join(::TEST_DIR, 'fixtures')
2833
end
2934

3035
end
@@ -33,18 +38,18 @@ module SpriteHelper
3338
URI = "selectors/*.png"
3439

3540
def init_sprite_helper
36-
@images_proj_path = File.join(fixture_path, 'sprites', 'public')
41+
@images_proj_path = File.join(fixture_path, 'public')
3742
@images_src_dir = 'images'
3843
@images_src_path = File.join(@images_proj_path, @images_src_dir)
3944
@images_tmp_dir = 'images-tmp'
4045
@images_tmp_path = File.join(@images_proj_path, @images_tmp_dir)
4146
end
4247

4348
def sprite_map_test(options, uri = URI)
44-
importer = Compass::SpriteImporter.new
45-
path, name = Compass::SpriteImporter.path_and_name(uri)
46-
sprite_names = Compass::SpriteImporter.sprite_names(uri)
47-
sass_engine = Compass::SpriteImporter.sass_engine(uri, name, importer, options)
49+
importer = Compass::Sprites::Importer.new
50+
path, name = Compass::Sprites::Importer.path_and_name(uri)
51+
sprite_names = Compass::Sprites::Importer.sprite_names(uri)
52+
sass_engine = Compass::Sprites::Importer.sass_engine(uri, name, importer, options)
4853
map = Compass::SassExtensions::Sprites::SpriteMap.new(sprite_names.map{|n| uri.gsub('*', n)}, path, name, sass_engine, options)
4954
map.options = {:compass => {:logger => Compass::NullLogger.new}}
5055
map

0 commit comments

Comments
 (0)