Skip to content

Commit b0e564f

Browse files
committed
Fixing builder to merge icons correctly based on settings
1 parent d81c450 commit b0e564f

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed

lib/sprite/builder.rb

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'fileutils'
12
module Sprite
23
class Builder
34
DEFAULT_CONFIG_PATH = 'config/sprite.yml'
@@ -6,13 +7,12 @@ class Builder
67
attr_reader :images
78
attr_reader :output
89

9-
def self.from_config(path)
10-
11-
# look up file
10+
def self.from_config(path = nil)
11+
1212
results = {}
13-
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
13+
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
1414
begin
15-
results = File.open(config_path) {|f| YAML::load(f)}
15+
results = File.open(config_path) {|f| YAML::load(f)} || {}
1616
rescue => e
1717
puts "Unable to read sprite config: #{Sprite.root+"/"+config_path}"
1818
puts e.to_s
@@ -38,34 +38,33 @@ def initialize(config = nil, images = nil)
3838

3939
def build
4040
@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
4547

46-
# create images
47-
images.each do |image|
48-
output_image(image)
48+
# write css
49+
output_file
4950
end
50-
51-
# write css
52-
output_file
5351
end
5452

5553
def output_image(image)
5654
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
6160

6261
combiner = ImageCombiner.new
6362

6463
dest_image = combiner.get_image(sources.shift)
6564
results << combiner.image_properties(dest_image).merge(:x => 0, :y => 0)
6665
sources.each do |source|
6766
source_image = combiner.get_image(source)
68-
if configuration['align'].to_s == 'horizontal'
67+
if image['align'].to_s == 'horizontal'
6968
x = dest_image.columns + spaced_by
7069
y = 0
7170
else
@@ -75,12 +74,23 @@ def output_image(image)
7574
results << combiner.image_properties(source_image).merge(:x => x, :y => y)
7675
dest_image = combiner.composite_images(dest_image, source_image, x, y)
7776
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)
8085
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|
8494
@output.each do |dest, results|
8595
results.each do |result|
8696
f.puts ".#{result[:name]}"
@@ -95,6 +105,14 @@ def output_file(configuration)
95105

96106
protected
97107

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+
98116
# sets all the default values on the config
99117
def set_config_defaults
100118
@config['style'] ||= 'css'
@@ -105,20 +123,14 @@ def set_config_defaults
105123
@config['class_separator'] ||= '_'
106124
end
107125

108-
# expands out sources
126+
# expands out sources, taking the Glob paths and turning them into separate entries in the array
109127
def expand_image_paths
110128
# cycle through image sources and expand out globs
111129
@images.each do |image|
112-
113130
# expand out all the globs
114131
image['sources'] = image['sources'].to_a.map{ |source|
115132
Dir.glob(File.join(Sprite.root, @config['source_path'], source))
116133
}.flatten.compact
117-
118-
# remove the prefixes on them
119-
image['sources'].each do |source|
120-
source.gsub!(Sprite.root, "")
121-
end
122134
end
123135
end
124136

lib/sprite/runner.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@ class Runner
33

44
attr_accessor :options
55
def initialize(args)
6-
self.args = args
7-
self.options = {}
8-
parse(args)
6+
self.options = set_options(args)
97
end
108

11-
def parse(args)
12-
# TODO: edit options with passed in args
9+
def set_options(args)
10+
opts = {}
11+
# TODO
12+
# edit options with passed in args
13+
opts
1314
end
1415

1516
# run sprite creator
1617
def run!
17-
# TODO
18+
begin
19+
Sprite::Builder.from_config(options[:path]).build
20+
# rescue Exception => e
21+
# # catch errors
22+
# puts "ERROR"
23+
# puts e
24+
# return 1
25+
end
26+
0
1827
end
1928

2029
end

0 commit comments

Comments
 (0)