Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
weplay committed Dec 8, 2009
1 parent a1f8c2c commit 19048c7
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.DS_Store
spec/output
pkg
rspec_autotest.rb
3 changes: 3 additions & 0 deletions lib/sprite.rb
Expand Up @@ -19,7 +19,10 @@ def self.root
end

require 'sprite/builder'
require 'sprite/config'
require 'sprite/image_combiner'
require 'sprite/image_config'
require 'sprite/image_reader'
require 'sprite/image_resizer'
require 'sprite/image_writer'
require 'sprite/styles'
99 changes: 27 additions & 72 deletions lib/sprite/builder.rb
@@ -1,25 +1,11 @@
require 'fileutils'
module Sprite
class Builder
DEFAULT_CONFIG_PATH = 'config/sprite.yml'

attr_reader :config
attr_reader :images

def self.from_config(path = nil)
results = {}
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)

# read configuration
if File.exists?(config_path)
begin
results = File.open(config_path) {|f| YAML::load(f)} || {}
rescue => e
puts "Error reading sprite config: #{config_path}"
puts e.to_s
end
end

results = Config.read_config(path)
new(results["config"], results["images"])
end

Expand All @@ -28,7 +14,9 @@ def initialize(config = nil, images = nil)
set_config_defaults

@images = images || []
set_image_defaults
if @images.empty?
@images = default_images
end
expand_image_paths

# initialize sprite files
Expand All @@ -52,47 +40,43 @@ def build
end

protected
def write_image(image)
def write_image(image_info)
results = []
sources = image['sources'].to_a
image_config = ImageConfig.new(image_info, config)
sources = image_config.sources
return unless sources.length > 0

name = image['name']
format = image['format'] || config["default_format"]
spaced_by = image['spaced_by'] || config["default_spacing"] || 0
resize_to = image['resize_to'] || config['resize_to']
resizer = ImageResizer.new(resize_to)

name = image_config.name
resizer = ImageResizer.new(image_config.resize_to)
combiner = ImageCombiner.new

# Let's get the sprite started with the first image
first_image = ImageReader.read(sources.shift)
resizer.resize(first_image)

dest_image = first_image
results << combiner.image_properties(dest_image).merge(:x => 0, :y => 0, :group => name)

# Now let's add the rest of the images in turn
sources.each do |source|
source_image = ImageReader.read(source)
resizer.resize(source_image)
if image['align'].to_s == 'horizontal'
x = dest_image.columns + spaced_by
if image_config.horizontal_layout?
x = dest_image.columns + image_config.spaced_by
y = 0
align = "horizontal"
else
x = 0
y = dest_image.rows + spaced_by
y = dest_image.rows + image_config.spaced_by
align = "vertical"
end
results << combiner.image_properties(source_image).merge(:x => -x, :y => -y, :group => name, :align => align)
dest_image = combiner.composite_images(dest_image, source_image, x, y)
end

ImageWriter.new(config).write(dest_image, name, image_config.format)

# set up path
path = image_output_path(name, format)
FileUtils.mkdir_p(File.dirname(path))

# write sprite image file to disk
dest_image.write(path)
@sprite_files["#{name}.#{format}"] = results
@sprite_files["#{name}.#{image_config.format}"] = results
end

def write_styles
Expand Down Expand Up @@ -120,18 +104,17 @@ def set_config_defaults
end

# if no image configs are detected, set some intelligent defaults
def set_image_defaults
return unless @images.size == 0

def default_images
sprites_path = image_source_path("sprites")
collection = []

if File.exists?(sprites_path)
Dir.glob(File.join(sprites_path, "*")) do |dir|
next unless File.directory?(dir)
source_name = File.basename(dir)

# default to finding all png, gif, jpg, and jpegs within the directory
images << {
collection << {
"name" => source_name,
"sources" => [
File.join("sprites", source_name, "*.png"),
Expand All @@ -142,6 +125,7 @@ def set_image_defaults
}
end
end
collection
end

# expands out sources, taking the Glob paths and turning them into separate entries in the array
Expand All @@ -161,51 +145,22 @@ def style_output_path(file_ext, relative = false)
unless path.include?(".#{file_ext}")
path = "#{path}.#{file_ext}"
end
public_path(path, relative)
end

# get the disk path for a location within the image output folder
def image_output_path(name, format, relative = false)
path_parts = []
path_parts << chop_trailing_slash(config['image_output_path']) if path_present?(config['image_output_path'])
path_parts << "#{name}.#{format}"
public_path(File.join(*path_parts), relative)
Config.new(config).public_path(path, relative)
end

# get the disk path for an image source file
def image_source_path(location, relative = false)
path_parts = []
path_parts << chop_trailing_slash(config["image_source_path"]) if path_present?(config['image_source_path'])
path_parts << Config.chop_trailing_slash(config["image_source_path"]) if Config.path_present?(config['image_source_path'])
path_parts << location
public_path(File.join(*path_parts), relative)
Config.new(config).public_path(File.join(*path_parts), relative)
end

def style_template_source_path(image, relative = false)
location = image["style_output_template"]
path_parts = []
path_parts << location
public_path(File.join(*path_parts), relative)
end

# get the disk path for a location within the public folder (if set)
def public_path(location, relative = false)
path_parts = []
path_parts << Sprite.root unless relative
path_parts << chop_trailing_slash(config['public_path']) if path_present?(config['public_path'])
path_parts << location

File.join(*path_parts)
end

# chop off the trailing slash on a directory path (if it exists)
def chop_trailing_slash(path)
path = path[0...-1] if path[-1] == File::SEPARATOR
path
end

# check if the path is set
def path_present?(path)
path.to_s.strip != ""
Config.new(config).public_path(File.join(*path_parts), relative)
end
end
end
46 changes: 46 additions & 0 deletions lib/sprite/config.rb
@@ -0,0 +1,46 @@
module Sprite
class Config
DEFAULT_CONFIG_PATH = 'config/sprite.yml'

def self.read_config(path = nil)
config_path = File.join(Sprite.root, path || DEFAULT_CONFIG_PATH)

# read configuration
if File.exists?(config_path)
begin
File.open(config_path) {|f| YAML::load(f)} || {}
rescue => e
puts "Error reading sprite config: #{config_path}"
puts e.to_s
{}
end
end
end

# chop off the trailing slash on a directory path (if it exists)
def self.chop_trailing_slash(path)
path = path[0...-1] if path[-1] == File::SEPARATOR
path
end

# check if the path is set
def self.path_present?(path)
path.to_s.strip != ""
end

def initialize(settings_hash)
@settings = settings_hash
end

# get the disk path for a location within the public folder (if set)
def public_path(location, relative = false)
path_parts = []
path_parts << Sprite.root unless relative
path_parts << Config.chop_trailing_slash(@settings['public_path']) if Config.path_present?(@settings['public_path'])
path_parts << location

File.join(*path_parts)
end

end
end
32 changes: 32 additions & 0 deletions lib/sprite/image_config.rb
@@ -0,0 +1,32 @@
module Sprite
class ImageConfig
def initialize(image_info, global_config_info)
@image_info = image_info
@global_config_info = global_config_info
end

def sources
@image_info['sources'].to_a
end

def name
@image_info['name']
end

def format
@image_info['format'] || @global_config_info["default_format"]
end

def spaced_by
@image_info['spaced_by'] || @global_config_info["default_spacing"] || 0
end

def resize_to
@image_info['resize_to'] || @global_config_info['resize_to']
end

def horizontal_layout?
@image_info['align'].to_s == 'horizontal'
end
end
end
25 changes: 25 additions & 0 deletions lib/sprite/image_writer.rb
@@ -0,0 +1,25 @@
module Sprite
class ImageWriter
def initialize(config)
@config = config
end

def write(image, name, format)
# set up path
path = image_output_path(name, format)
FileUtils.mkdir_p(File.dirname(path))

# write sprite image file to disk
image.write(path)
end

# get the disk path for a location within the image output folder
def image_output_path(name, format, relative = false)
path_parts = []
path_parts << Config.chop_trailing_slash(@config['image_output_path']) if Config.path_present?(@config['image_output_path'])
path_parts << "#{name}.#{format}"
Config.new(@config).public_path(File.join(*path_parts), relative)
end

end
end
2 changes: 1 addition & 1 deletion lib/sprite/styles.rb
Expand Up @@ -12,7 +12,7 @@ module Sprite::Styles
"sass_mixin" => "SassMixinGenerator",
"sass_yml" => "SassYmlGenerator"
}

def self.get(config)
const_get(GENERATORS[config])
rescue
Expand Down
2 changes: 1 addition & 1 deletion lib/sprite/styles/templated_css_generator.rb
Expand Up @@ -23,7 +23,7 @@ def write(path, sprite_files)
height = sprite[:height]
left = sprite[:x]
top = sprite[:y]
image_path = @builder.send :image_output_path, image['name'], image['format']
image_path = ImageWriter.new(@builder.config).image_output_path(image['name'], image['format'])
f.puts erb_template.result(binding)
end
end
Expand Down

0 comments on commit 19048c7

Please sign in to comment.