From 43fc3be14ba2307e3c6502818fa6f3ee272a7fa6 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Thu, 2 Apr 2009 20:20:29 -0700 Subject: [PATCH] Generate a grid background image with the --grid-img command line option. --- .../commands/generate_grid_background.rb | 27 +++++++++++++++ lib/compass/commands/project_base.rb | 4 +++ lib/compass/configuration.rb | 4 +++ lib/compass/exec.rb | 10 ++++++ .../blueprint => lib/compass}/grid_builder.rb | 34 ++++++++++++++----- 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 lib/compass/commands/generate_grid_background.rb rename {frameworks/blueprint/lib/blueprint => lib/compass}/grid_builder.rb (64%) diff --git a/lib/compass/commands/generate_grid_background.rb b/lib/compass/commands/generate_grid_background.rb new file mode 100644 index 00000000..212573c4 --- /dev/null +++ b/lib/compass/commands/generate_grid_background.rb @@ -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 \ No newline at end of file diff --git a/lib/compass/commands/project_base.rb b/lib/compass/commands/project_base.rb index e5ba5b58..3c34ffe6 100644 --- a/lib/compass/commands/project_base.rb +++ b/lib/compass/commands/project_base.rb @@ -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')) diff --git a/lib/compass/configuration.rb b/lib/compass/configuration.rb index b9b5bc33..31b867c0 100644 --- a/lib/compass/configuration.rb +++ b/lib/compass/configuration.rb @@ -73,6 +73,10 @@ module Compass "stylesheets" end + def default_images_dir + "images" + end + def default_output_style if environment == :development :expanded diff --git a/lib/compass/exec.rb b/lib/compass/exec.rb index 25cb79f1..66686062 100644 --- a/lib/compass/exec.rb +++ b/lib/compass/exec.rb @@ -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 +. 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 +. E.g. 20+5 or 30+10." + exit + end + self.options[:command] = :generate_grid_background + end + end def do_command(command) diff --git a/frameworks/blueprint/lib/blueprint/grid_builder.rb b/lib/compass/grid_builder.rb similarity index 64% rename from frameworks/blueprint/lib/blueprint/grid_builder.rb rename to lib/compass/grid_builder.rb index 4f0d688a..0c91f60f 100644 --- a/frameworks/blueprint/lib/blueprint/grid_builder.rb +++ b/lib/compass/grid_builder.rb @@ -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 # * options @@ -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 @@ -46,9 +54,19 @@ module Blueprint baseline.line(0, (height - 1), total_width, (height- 1)).styles(:fill => "#e9e9e9") 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 \ No newline at end of file