1
+ require 'fileutils'
1
2
module Sprite
2
3
class Builder
3
4
DEFAULT_CONFIG_PATH = 'config/sprite.yml'
@@ -6,13 +7,12 @@ class Builder
6
7
attr_reader :images
7
8
attr_reader :output
8
9
9
- def self . from_config ( path )
10
-
11
- # look up file
10
+ def self . from_config ( path = nil )
11
+
12
12
results = { }
13
- config_path = File . join ( Sprite . root , path || DEFAULT_CONFIG_PATH )
13
+ config_path = File . join ( Sprite . root , path || DEFAULT_CONFIG_PATH )
14
14
begin
15
- results = File . open ( config_path ) { |f | YAML ::load ( f ) }
15
+ results = File . open ( config_path ) { |f | YAML ::load ( f ) } || { }
16
16
rescue => e
17
17
puts "Unable to read sprite config: #{ Sprite . root +"/" +config_path } "
18
18
puts e . to_s
@@ -38,34 +38,33 @@ def initialize(config = nil, images = nil)
38
38
39
39
def build
40
40
@output = { }
41
-
42
- # build up config
43
- # @image_path = sprite_config['base_image_path'] ? Sprite.root+"/"+sprite_config['config']['base_image_path']+"/" : DEFAULT_IMAGE_PATH
44
- # @file_path = sprite_config['config']['output_file'] ? Sprite.root+"/"+sprite_config['config']['output_file'] : DEFAULT_FILE_PATH
41
+
42
+ if images . size > 0
43
+ # create images
44
+ images . each do |image |
45
+ output_image ( image )
46
+ end
45
47
46
- # create images
47
- images . each do |image |
48
- output_image ( image )
48
+ # write css
49
+ output_file
49
50
end
50
-
51
- # write css
52
- output_file
53
51
end
54
52
55
53
def output_image ( image )
56
54
results = [ ]
57
- sources = configuration [ 'sources' ] . to_a
58
-
59
- dest = configuration [ 'target' ] || sources [ 0 ] . gsub ( /\. / , "_sprite." )
60
- spaced_by = configuration [ 'spaced_by' ] || 0
55
+ sources = image [ 'sources' ] . to_a
56
+ return unless sources . length > 0
57
+
58
+ name = image [ 'name' ]
59
+ spaced_by = image [ 'spaced_by' ] || 0
61
60
62
61
combiner = ImageCombiner . new
63
62
64
63
dest_image = combiner . get_image ( sources . shift )
65
64
results << combiner . image_properties ( dest_image ) . merge ( :x => 0 , :y => 0 )
66
65
sources . each do |source |
67
66
source_image = combiner . get_image ( source )
68
- if configuration [ 'align' ] . to_s == 'horizontal'
67
+ if image [ 'align' ] . to_s == 'horizontal'
69
68
x = dest_image . columns + spaced_by
70
69
y = 0
71
70
else
@@ -75,12 +74,23 @@ def output_image(image)
75
74
results << combiner . image_properties ( source_image ) . merge ( :x => x , :y => y )
76
75
dest_image = combiner . composite_images ( dest_image , source_image , x , y )
77
76
end
78
- @output [ dest ] = results
79
- dest_image . write ( @image_path + dest )
77
+ @output [ name ] = results
78
+
79
+ # set up path
80
+ path = image_path ( name , image [ 'format' ] )
81
+ FileUtils . mkdir_p ( File . dirname ( path ) )
82
+
83
+ # write sprite image file to disk
84
+ dest_image . write ( path )
80
85
end
81
-
82
- def output_file ( configuration )
83
- File . open ( @file_path , 'w' ) do |f |
86
+
87
+ def output_file
88
+ # set up path
89
+ path = output_path ( "css" )
90
+ FileUtils . mkdir_p ( File . dirname ( path ) )
91
+
92
+ # write stylesheet file to disk
93
+ File . open ( path , 'w' ) do |f |
84
94
@output . each do |dest , results |
85
95
results . each do |result |
86
96
f . puts ".#{ result [ :name ] } "
@@ -95,6 +105,14 @@ def output_file(configuration)
95
105
96
106
protected
97
107
108
+ def output_path ( file_ext )
109
+ "#{ Sprite . root } /#{ config [ 'output_path' ] } .#{ file_ext } "
110
+ end
111
+
112
+ def image_path ( name , format )
113
+ "#{ Sprite . root } /#{ config [ 'image_output_path' ] } #{ name } .#{ format } "
114
+ end
115
+
98
116
# sets all the default values on the config
99
117
def set_config_defaults
100
118
@config [ 'style' ] ||= 'css'
@@ -105,20 +123,14 @@ def set_config_defaults
105
123
@config [ 'class_separator' ] ||= '_'
106
124
end
107
125
108
- # expands out sources
126
+ # expands out sources, taking the Glob paths and turning them into separate entries in the array
109
127
def expand_image_paths
110
128
# cycle through image sources and expand out globs
111
129
@images . each do |image |
112
-
113
130
# expand out all the globs
114
131
image [ 'sources' ] = image [ 'sources' ] . to_a . map { |source |
115
132
Dir . glob ( File . join ( Sprite . root , @config [ 'source_path' ] , source ) )
116
133
} . flatten . compact
117
-
118
- # remove the prefixes on them
119
- image [ 'sources' ] . each do |source |
120
- source . gsub! ( Sprite . root , "" )
121
- end
122
134
end
123
135
end
124
136
0 commit comments