From 19048c7bc5927526e649376649679fa76d53d826 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Tue, 8 Dec 2009 16:26:15 -0500 Subject: [PATCH] Refactoring --- .gitignore | 1 + lib/sprite.rb | 3 + lib/sprite/builder.rb | 99 ++++++-------------- lib/sprite/config.rb | 46 +++++++++ lib/sprite/image_config.rb | 32 +++++++ lib/sprite/image_writer.rb | 25 +++++ lib/sprite/styles.rb | 2 +- lib/sprite/styles/templated_css_generator.rb | 2 +- 8 files changed, 136 insertions(+), 74 deletions(-) create mode 100644 lib/sprite/config.rb create mode 100644 lib/sprite/image_config.rb create mode 100644 lib/sprite/image_writer.rb diff --git a/.gitignore b/.gitignore index 943d3e3..d6ca10f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store spec/output pkg +rspec_autotest.rb diff --git a/lib/sprite.rb b/lib/sprite.rb index f006e92..719f69a 100644 --- a/lib/sprite.rb +++ b/lib/sprite.rb @@ -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' diff --git a/lib/sprite/builder.rb b/lib/sprite/builder.rb index 4717e3c..fe46584 100644 --- a/lib/sprite/builder.rb +++ b/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 @@ -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 @@ -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 @@ -120,10 +104,9 @@ 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| @@ -131,7 +114,7 @@ def set_image_defaults 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"), @@ -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 @@ -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 \ No newline at end of file diff --git a/lib/sprite/config.rb b/lib/sprite/config.rb new file mode 100644 index 0000000..e329359 --- /dev/null +++ b/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 diff --git a/lib/sprite/image_config.rb b/lib/sprite/image_config.rb new file mode 100644 index 0000000..353d420 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/sprite/image_writer.rb b/lib/sprite/image_writer.rb new file mode 100644 index 0000000..6dacfe1 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/sprite/styles.rb b/lib/sprite/styles.rb index d3ba338..d405894 100644 --- a/lib/sprite/styles.rb +++ b/lib/sprite/styles.rb @@ -12,7 +12,7 @@ module Sprite::Styles "sass_mixin" => "SassMixinGenerator", "sass_yml" => "SassYmlGenerator" } - + def self.get(config) const_get(GENERATORS[config]) rescue diff --git a/lib/sprite/styles/templated_css_generator.rb b/lib/sprite/styles/templated_css_generator.rb index 8c29b37..87d77d3 100644 --- a/lib/sprite/styles/templated_css_generator.rb +++ b/lib/sprite/styles/templated_css_generator.rb @@ -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