Skip to content

Commit

Permalink
Updating Builder to handle proper configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
merbjedi committed Nov 14, 2009
1 parent 0f92b54 commit 5c7e5bc
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 36 deletions.
3 changes: 2 additions & 1 deletion lib/sprite.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# set up Sprite module
module Sprite

# provides the root directory to use when reading and writing files
def self.root
@root ||= nil

Expand Down
96 changes: 74 additions & 22 deletions lib/sprite/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,43 @@ class Builder
DEFAULT_IMAGE_PATH = 'public/images/'
DEFAULT_FILE_PATH = 'tmp/sprite.css'

def initialize(settings = nil)
@image_path = DEFAULT_IMAGE_PATH

if settings.is_a?(Hash)
@sprite_config = settings
attr_reader :config
attr_reader :images

def initialize(config = nil, images = nil)
results = config_results

# use the override
if config.is_a?(Hash)
@config = config
else
@config = config["config"] || {}
end

# set defaults
set_config_defaults

# default image list
if images.is_a?(Array)
@images = images
else
@sprite_config = read_sprite_config(settings)
@images = config["images"] || []
end

# process images
process_image_settings
end

def build
@output = {}

# build up settings
# build up config
@image_path = sprite_config['config']['base_image_path'] ? Sprite.root+"/"+sprite_config['config']['base_image_path']+"/" : DEFAULT_IMAGE_PATH
@file_path = sprite_config['config']['output_file'] ? Sprite.root+"/"+sprite_config['config']['output_file'] : DEFAULT_FILE_PATH

# create images
sprite_config['images'].each do |configuration|
output_image(configuration)
output_image(configuration)
end

# write css
Expand All @@ -32,24 +49,26 @@ def build

def output_image(configuration)
results = []
sources = configuration['sources'].collect {|source| Dir.glob(@image_path+source)}.flatten
sources = configuration['sources'].to_a

dest = configuration['target'] || sources[0].gsub(/\./,"_sprite.")
spaced_by = configuration['spaced_by'] || 0
dest_image = ImageUtil.get_image(sources.shift)
results << ImageUtil.image_properties(dest_image).merge(:x => 0, :y => 0)

combiner = ImageCombiner.new

dest_image = combiner.get_image(sources.shift)
results << combiner.image_properties(dest_image).merge(:x => 0, :y => 0)
sources.each do |source|
source_image = ImageUtil.get_image(source)
if configuration['align'] == 'horizontal'
gravity = Magick::EastGravity
source_image = combiner.get_image(source)
if configuration['align'].to_s == 'horizontal'
x = dest_image.columns + spaced_by
y = 0
else
gravity = Magick::SouthGravity
x = 0
y = dest_image.rows + spaced_by
end
results << ImageUtil.image_properties(source_image).merge(:x => x, :y => y)
dest_image = ImageUtil.composite_images(dest_image, source_image, x, y)
results << combiner.image_properties(source_image).merge(:x => x, :y => y)
dest_image = combiner.composite_images(dest_image, source_image, x, y)
end
@output[dest] = results
dest_image.write(@image_path + dest)
Expand All @@ -71,17 +90,50 @@ def output_file(configuration)

protected

# reads config settings from the given path
def read_sprite_config(path = nil)
# reads config config from the given path
def read_config(path = nil)
config_results = {}
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)
begin
config = File.open(config_path) {|f| YAML::load(f)}
config_results = File.open(config_path) {|f| YAML::load(f)}
rescue => e
puts "Unable to read sprite config: #{Sprite.root+"/"+config_path}"
puts e.to_s
end
config
rescue
config_results
end

# sets all the default values on the config
def set_config_defaults
@config['style'] ||= ''
@config['output_path'] ||= 'public/sass/mixins/sprites'
@config['image_output_path'] ||= 'public/images/sprite/'
@config['source_path'] ||= ''
@config['default_format'] ||= 'png'
@config['class_separator'] ||= '_'
end

# expands out sources
def process_image_config
# cycle through image sources and expand out globs
@images.each do |image|
# find all the files
image['sources'] = image['sources'].to_a.map do |source|
Dir.glob(File.join(Sprite.root, @config['source_path'], source))
end

# remove the prefix on them
new_sources = new_sources.flatten.map do |source|
source.gsub!(Sprite.root, "")
end

image_config['sources'] = new_sources
end

rescue => e
puts "Invalid sprite configuration syntax:"
puts e.to_s
end

end
end
24 changes: 24 additions & 0 deletions spec/resources/configs/full_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
config:
style: css
output_path: output/stylesheets/sprites.css
image_output_path: output/images/sprites/
source_path: resources/images/
class_separator: '_'
default_format: png

# defines what sprite collections get created
images:
- name: android
format: png
align: vertical
spaced_by: 50
sources:
- android/*.png

- name: topics
format: gif
align: vertical
spaced_by: 50
sources:
- topics/good_topic.gif
- topics/mid_topic.gif
20 changes: 20 additions & 0 deletions spec/sprite/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'

describe Sprite::Builder do

context "Configuration Parsing" do
before(:all) do
@sprite = Sprite::Builder.new("resources/configs/full_config.yml")
end

it "should load the settings keys from file" do
@sprite.config.keys.size.should == 2
end

it "should glob the files together" do
@sprite.config["images"].first["sources"].size.should == 30
end

end

end
2 changes: 1 addition & 1 deletion spec/sprite/image_combiner_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Sprite do
describe Sprite::ImageCombiner do
before(:all) do
# build a sprite object with empty config
@combiner = Sprite::ImageCombiner.new
Expand Down
12 changes: 0 additions & 12 deletions spec/sprite/sprite_config_spec.rb

This file was deleted.

0 comments on commit 5c7e5bc

Please sign in to comment.