Skip to content

Commit

Permalink
add output, no default backend, will load any available backend in th…
Browse files Browse the repository at this point in the history
…is order, rmagick > oily_png > chunky_png > mini_magick.
  • Loading branch information
shwoodard committed Jan 29, 2011
1 parent fb6d0a8 commit 39903df
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 17 deletions.
1 change: 0 additions & 1 deletion lib/active_assets/active_sprites/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module Configurable

included do
config_accessor :sprite_backend
self.sprite_backend = :rmagick if sprite_backend.nil?
end
end
end
Expand Down
48 changes: 42 additions & 6 deletions lib/active_assets/active_sprites/runners/abstract_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,63 @@ def initialize(railtie, sprites)
end
end

def generate!(debug = ENV['DEBUG'])
def generate!
verbose = ENV['VERBOSE'] != 'false' || ENV['DEBUG']

if verbose
t = Time.now
$stdout << "#{t}: Active Sprites: \"I'm string my run using #{runner_name}.\"\n"
$stdout << "\nSprites to create:\n\"#{@sprites.map(&:path).join('", "')}\"\n"
end

@sprites.each do |sprite|
next if sprite.sprite_pieces.empty?

if verbose
t_sprite = Time.now
$stdout << "\n=================================================\n"
$stdout << "Starting Sprite, #{sprite.path}\n"
end

sprite_path = sanitize_asset_path(context.image_path(sprite.path))
p "Sprite Path: #{sprite_path}" if debug
sprite_stylesheet_path = sanitize_asset_path(context.stylesheet_path(sprite.stylesheet_path))
p "Sprite Stylesheet Path: #{sprite_stylesheet_path}" if debug

orientation = sprite.orientation.to_s
sprite_pieces = sprite.sprite_pieces

begin
$stdout << "Gathering sprite details..." if verbose
image_list, width, height = set_sprite_details_and_return_image_list(sprite, sprite_path, sprite_pieces, orientation)
$stdout << "done.\n" if verbose

if ENV['DEBUG']
$stdout << "|\tpath\t|\tselectors\t|\tx\t|\ty\t|\twidth\t|\theight\t|\n"
$stdout << "#{sprite_pieces.map(&:to_s).join("\n")}\n"
end

stylesheet = SpriteStylesheet.new(sprite_pieces)
stylesheet.write File.join(@railtie.config.paths.public.to_a.first, sprite_stylesheet_path)
create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height, orientation)
write File.join(@railtie.config.paths.public.to_a.first, sprite_path), sprite.quality
stylesheet_file_path = File.join(@railtie.config.paths.public.to_a.first, sprite_stylesheet_path)
$stdout << "Writing stylesheet to #{stylesheet_file_path} ... " if verbose
stylesheet.write stylesheet_file_path
$stdout << "done.\n" if verbose

$stdout << "Beginning sprite generation using #{runner_name.humanize}.\n" if verbose
create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height, orientation, verbose)
$stdout << "Success!\n" if verbose

sprite_file_path = File.join(@railtie.config.paths.public.to_a.first, sprite_path)
$stdout << "Writing sprite to #{sprite_file_path} ... " if verbose
write sprite_file_path, sprite.quality
$stdout << "done.\n" if verbose

$stdout << "Finished #{sprite.path} in #{Time.now - t_sprite} seconds.\n" if verbose
$stdout << "=================================================\n\n" if verbose
ensure
finish
end
end

$stdout << "#{Time.now}: ActiveSprites \"I finished my run in #{Time.now - t} seconds.\"\n" if verbose
end

private
Expand Down
13 changes: 12 additions & 1 deletion lib/active_assets/active_sprites/runners/chunky_png_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ def set_sprite_details_and_return_image_list(sprite, sprite_path, sprite_pieces,
[image_list, width, height]
end

def create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height, orientation)
def create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height, orientation, verbose)
@sprite = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)

image_list.each_with_index do |image, i|
@sprite.replace(image, sprite_pieces[i].details.x, sprite_pieces[i].details.y)
$stdout << '.' if verbose
end
$stdout << "\n" if verbose
end

def write(path, quality = nil)
Expand All @@ -46,6 +48,15 @@ def write(path, quality = nil)
def finish
@sprite = nil
end

def runner_name
begin
require 'oily_png'
'oily_png'
rescue LoadError
'chunky_png'
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def set_sprite_details_and_return_image_list(sprite, sprite_path, sprite_pieces,
[image_list]
end

def create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height, orientation)
def create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height, orientation, verbose)
begin
tempfile = ImageTempfile.new(File.extname(sprite_path)[1..-1])
tempfile.binmode
Expand All @@ -50,6 +50,8 @@ def create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height,
args << tempfile.path

MiniMagick::Image.new(image_list.first.path).run_command('montage', *args)
image_list.size.times { $stdout << '.' } if verbose
$stdout << "\n" if verbose
@sprite = MiniMagick::Image.open(tempfile.path)
end

Expand All @@ -63,6 +65,10 @@ def finish
@sprite = nil
end

def runner_name
'mini_magick'
end

end
end
end
10 changes: 9 additions & 1 deletion lib/active_assets/active_sprites/runners/rmagick_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ def set_sprite_details_and_return_image_list(sprite, sprite_path, sprite_pieces,
[image_list]
end

def create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height, orientation)
def create_sprite(sprite, sprite_path, sprite_pieces, image_list, width, height, orientation, verbose)
@sprite = image_list.montage do
self.tile = orientation == Sprite::Orientation::VERTICAL ? "1x#{sprite_pieces.size}" : "#{sprite_pieces.size}x1"
self.geometry = "+0+0"
self.background_color = 'transparent'
self.matte_color = sprite.matte_color || '#bdbdbd'
end

image_list.size.times { $stdout << '.' } if verbose
$stdout << "\n" if verbose
end

def write(path, quality = nil)
Expand All @@ -53,6 +56,11 @@ def finish
end
@sprite = DEFAULT_SPRITE
end

def runner_name
'rmagick'
end

end
end
end
6 changes: 5 additions & 1 deletion lib/active_assets/active_sprites/sprite_piece.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def configure(mapping, options = {}, &blk)
self
end

def to_s
def to_css
return '' if details.nil?
<<-CSS
#{css_selector}
Expand All @@ -45,6 +45,10 @@ def to_s
CSS
end

def to_s
"|\t#{path}\t|\t#{css_selector}\t|\t#{details.x}\t|\t#{details.y}\t|\t#{details.width}\t|\t#{details.height}\t|\n"
end

GEOMETRY_PROPS.each do |prop|
eval <<-METH
def #{prop}(*args)
Expand Down
7 changes: 3 additions & 4 deletions lib/active_assets/active_sprites/sprite_stylesheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ def initialize(sprite_pieces)
end

def write(path)
to_s!
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w+') do |f|
f.write @as_string
f.write to_s
end
end

private
def to_s!
@as_string ||= @sprite_pieces.map(&:to_s).join("\n")
def to_s
@as_string ||= @sprite_pieces.map(&:to_css).join("\n")
end
end
end
Expand Down
26 changes: 24 additions & 2 deletions lib/active_assets/active_sprites/sprites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,31 @@ def generate!(railtie = Rails.application)
require 'chunky_png'
ChunkyPngRunner.new(railtie @sprites).generate!
end
else
begin
require 'rmagick'
RmagickRunner.new(railtie, @sprites).generate!
rescue LoadError
begin
begin
require 'oily_png'
ChunkyPngRunner.new(railtie, @sprites).generate!
rescue LoadError
require 'chunky_png'
ChunkyPngRunner.new(railtie, @sprites).generate!
end
rescue LoadError
require 'mini_magick'
MiniMagickRunner.new(railtie, @sprites).generate!
end
end
end
rescue LoadError
p "#{sprite_backend}, sprite backend was not found. Either install #{sprite_backend}, or configure config.active_sprites.sprite_backend to your installed backend in application.rb. Options are :rmagick, :chunky_png, and :mini_magick"
rescue LoadError => e
msg = ''
msg << "You have not specified any sprite runtime in your configuration. We tried to load all supported runtimes, but failed to load any." if !sprite_backend
msg << "Your requested backend, #{sprite_backend}, was not found." if sprite_backend
msg << " Supported sprite backends include :rmagick, :chunky_png, and :mini_magick. To use oily_png, install oily_png and configure with :chunky_png.\n"
$stdout << msg
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ENV['RAILS_ENV'] ||= 'test'
ENV['VERBOSE'] = 'false'

require 'rubygems'
require 'test/unit'
Expand Down

0 comments on commit 39903df

Please sign in to comment.