Generate a grid background image with the --grid-img command line option.

This commit is contained in:
Chris Eppstein 2009-04-02 20:20:29 -07:00
parent 63de13debd
commit 43fc3be14b
5 changed files with 71 additions and 8 deletions

View File

@ -0,0 +1,27 @@
require File.join(File.dirname(__FILE__), 'project_base')
require File.join(File.dirname(__FILE__), 'update_project')
require File.join(File.dirname(__FILE__), '..', 'grid_builder')
module Compass
module Commands
class GenerateGridBackground < ProjectBase
include Actions
def initialize(working_path, options)
super
assert_project_directory_exists!
end
def perform
read_project_configuration
Compass.configuration.set_maybe(options)
Compass.configuration.set_defaults!
column_width, gutter_width = options[:grid_dimensions].split(/\+/).map{|d| d.to_i}
unless GridBuilder.new(options.merge(:column_width => column_width, :gutter_width => gutter_width, :output_path => projectize(project_images_subdirectory), :working_path => self.working_path)).generate!
puts "ERROR: Some library dependencies appear to be missing."
puts "Have you installed rmagick? If not, please run:"
puts "sudo gem install rmagick"
end
end
end
end
end

View File

@ -33,6 +33,10 @@ module Compass
Compass.configuration.sass_dir
end
def project_images_subdirectory
Compass.configuration.images_dir
end
# Read the configuration file for this project
def read_project_configuration
if File.exists?(projectize('config.rb'))

View File

@ -73,6 +73,10 @@ module Compass
"stylesheets"
end
def default_images_dir
"images"
end
def default_output_style
if environment == :development
:expanded

View File

@ -170,6 +170,16 @@ END
opts.on_tail("-v", "--version", "Print version") do
self.options[:command] = :print_version
end
opts.on('--grid-img [DIMENSIONS]', 'Generate a background image to test grid alignment. Dimension is given as <column_width>+<gutter_width>. Defaults to 30+10.') do |dimensions|
self.options[:grid_dimensions] = dimensions || "30+10"
unless self.options[:grid_dimensions] =~ /^\d+\+\d+$/
puts "Please enter your dimensions as <column_width>+<gutter_width>. E.g. 20+5 or 30+10."
exit
end
self.options[:command] = :generate_grid_background
end
end
def do_command(command)

View File

@ -1,3 +1,4 @@
# This file came from the Blueprint Project
begin
require 'rubygems'
gem 'rmagick'
@ -5,15 +6,17 @@ begin
rescue Exception => e
end
module Blueprint
module Compass
# Uses ImageMagick and RMagick to generate grid.png file
class GridBuilder
include Actions
begin
include Magick
rescue Exception => e
end
attr_reader :column_width, :gutter_width, :output_path, :able_to_generate
attr_reader :column_width, :gutter_width, :output_path, :able_to_generate, :options
# ==== Options
# * <tt>options</tt>
@ -23,9 +26,14 @@ module Blueprint
def initialize(options={})
@able_to_generate = Magick::Long_version rescue false
return unless @able_to_generate
@column_width = options[:column_width] || Blueprint::COLUMN_WIDTH
@gutter_width = options[:gutter_width] || Blueprint::GUTTER_WIDTH
@output_path = options[:output_path] || Blueprint::SOURCE_PATH
@column_width = options[:column_width]
@gutter_width = options[:gutter_width]
@output_path = options[:output_path]
@options = options
end
def working_path
options[:working_path]
end
# generates (overwriting if necessary) grid.png image to be tiled in background
@ -47,8 +55,18 @@ module Blueprint
end
end
FileUtils.mkdir self.output_path unless File.exists? self.output_path
rvg.draw.write(File.join(self.output_path, "grid.png"))
filename = File.join(self.output_path, "grid.png")
if File.exists?(filename)
if options[:force]
overwrite = true
else
msg = "#{filename} already exists. Overwrite with --force."
raise Compass::FilesystemConflict.new(msg)
end
end
directory self.output_path
logger.record((overwrite ? :overwrite : :create), basename(filename))
rvg.draw.write(filename)
end
end
end