Grid image subcommand.

This commit is contained in:
Chris Eppstein 2009-10-05 00:56:49 -07:00
parent 12dcccad5d
commit 8b5868ca00
3 changed files with 71 additions and 10 deletions

View File

@ -47,7 +47,7 @@ misc commands
------------- -------------
# Generate a background image that can be used to verify grid alignment # Generate a background image that can be used to verify grid alignment
compass grid-background W+GxH [path/to/image.png] compass grid-img W+GxH [path/to/grid.png]
# Emit the version of compass # Emit the version of compass
compass version compass version

View File

@ -4,8 +4,65 @@ require 'compass/grid_builder'
module Compass module Compass
module Commands module Commands
module GridBackgroundOptionsParser
def set_options(opts)
banner = %Q{Usage: compass grid-img W+GxH [path/to/grid.png]
Description:
Generates a background image that can be used to check grid alignment.
Height is optional and defaults to 20px
By default, the image generated will be named "grid.png"
and be found in the images directory.
This command requires that you have both ImageMagick and RMagick installed.
Examples:
compass grid-img 40+10 # 40px column, 10px gutter, 20px height
compass grid-img 40+20x28 # 40px column, 20px gutter, 28px height
compass grid-img 60+20x28 images/wide_grid.png
Options:
}
opts.banner = banner
super
end
end
class GenerateGridBackground < ProjectBase class GenerateGridBackground < ProjectBase
include Actions include Actions
register :"grid-img"
class << self
def option_parser(arguments)
parser = Compass::Exec::CommandOptionParser.new(arguments)
parser.extend(Compass::Exec::GlobalOptionsParser)
parser.extend(GridBackgroundOptionsParser)
end
def usage
option_parser([]).to_s
end
def description(command)
"Generates a grid background image."
end
def parse!(arguments)
parser = option_parser(arguments)
parser.parse!
if arguments.size == 0
raise OptionParser::ParseError, "Please specify the grid dimensions."
end
parser.options[:grid_dimensions] = arguments.shift
parser.options[:grid_filename] = arguments.shift
parser.options
end
end
def initialize(working_path, options) def initialize(working_path, options)
super super
assert_project_directory_exists! assert_project_directory_exists!
@ -22,7 +79,8 @@ module Compass
column_width = $1.to_i column_width = $1.to_i
gutter_width = $2.to_i gutter_width = $2.to_i
height = $3.to_i if $3 height = $3.to_i if $3
unless GridBuilder.new(options.merge(:column_width => column_width, :gutter_width => gutter_width, :height => height, :output_path => projectize(project_images_subdirectory), :working_path => self.working_path)).generate! filename = options[:grid_filename] || projectize("#{project_images_subdirectory}/grid.png")
unless GridBuilder.new(options.merge(:column_width => column_width, :gutter_width => gutter_width, :height => height, :filename => filename, :working_path => self.working_path)).generate!
puts "ERROR: Some library dependencies appear to be missing." puts "ERROR: Some library dependencies appear to be missing."
puts "Have you installed rmagick? If not, please run:" puts "Have you installed rmagick? If not, please run:"
puts "sudo gem install rmagick" puts "sudo gem install rmagick"

View File

@ -16,21 +16,21 @@ module Compass
rescue Exception => e rescue Exception => e
end end
attr_reader :column_width, :gutter_width, :output_path, :able_to_generate, :options attr_reader :column_width, :gutter_width, :filename, :able_to_generate, :options
# ==== Options # ==== Options
# * <tt>options</tt> # * <tt>options</tt>
# * <tt>:column_width</tt> -- Width (in pixels) of current grid column # * <tt>:column_width</tt> -- Width (in pixels) of current grid column
# * <tt>:gutter_width</tt> -- Width (in pixels) of current grid gutter # * <tt>:gutter_width</tt> -- Width (in pixels) of current grid gutter
# * <tt>:height</tt> -- Height (in pixels) of a row # * <tt>:height</tt> -- Height (in pixels) of a row
# * <tt>:output_path</tt> -- Output path of grid.png file # * <tt>:filename</tt> -- Output path of grid.png file
def initialize(options={}) def initialize(options={})
@able_to_generate = Magick::Long_version rescue false @able_to_generate = Magick::Long_version rescue false
return unless @able_to_generate return unless @able_to_generate
@column_width = options[:column_width] @column_width = options[:column_width]
@gutter_width = options[:gutter_width] @gutter_width = options[:gutter_width]
@height = options[:height] || 20 @height = options[:height] || 20
@output_path = options[:output_path] @filename = options[:filename]
@options = options @options = options
end end
@ -56,7 +56,6 @@ module Compass
end end
end end
filename = File.join(self.output_path, "grid.png")
if File.exists?(filename) if File.exists?(filename)
if options[:force] if options[:force]
overwrite = true overwrite = true
@ -65,9 +64,13 @@ module Compass
raise Compass::FilesystemConflict.new(msg) raise Compass::FilesystemConflict.new(msg)
end end
end end
directory self.output_path directory File.dirname(filename)
logger.record((overwrite ? :overwrite : :create), basename(filename)) logger.record((overwrite ? :overwrite : :create), basename(filename))
unless options[:dry_run]
rvg.draw.write(filename) rvg.draw.write(filename)
else
true
end
end end
end end
end end