Skip to content

Commit

Permalink
Moved compress.rb to standard options using OptionParse, added code t…
Browse files Browse the repository at this point in the history
…o generate grid if ImageMagick and RMagick are installed, cleaned up classes
  • Loading branch information
joshuaclayton committed Jan 30, 2008
1 parent 709c173 commit 9fb4e04
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 58 deletions.
1 change: 1 addition & 0 deletions scripts/blueprint.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'core_ext'
require 'fileutils'

class Blueprint
ROOT_PATH = File.dirname(File.expand_path(File.dirname(__FILE__)))
Expand Down
43 changes: 10 additions & 33 deletions scripts/compress.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require 'blueprint'
require 'compress/compressor'
require 'compress/custom_layout'

# **Basic
#
Expand All @@ -10,31 +11,9 @@
#
# However, variables can be set to change how this works.
#
# **Custom Locations
#
# Passing LOCATION will save the files to a designated location; for example, calling
#
# LOCATION='/Users/me/Sites/application_name/public/stylesheets' ruby compress.rb
#
# will output the three CSS files to that specified directory (if permissions allow).
#
# **Custom CSS Files
#
# If you pass a custom location, the generation code will look for three files:
#
# LOCATION/my-ie.css
# LOCATION/my-screen.css
# LOCATION/my-print.css
#
# If any of these exist, their contents will be compressed and appended to the end of the CSS generated from Blueprint
#
# **Custom Namespace
#
# If you would like a custom namespace to be appended to all the classes, that too can be passed in to the call; for example, calling
#
# NAMESPACE='my-custom-namespace-' ruby compress.rb
#
# will prepend all Blueprint classes with 'my-custom-namespace-'
# ruby compress.rb -h
#
# Will reveal basic arguments you can pass to the compress.rb file.
#
# **Custom Settings
#
Expand All @@ -48,7 +27,7 @@
#
# Root nodes are project names. You use these when calling compress.rb as such:
#
# PROJECT=projectname ruby compress.rb
# ruby compress.rb -p PROJECTNAME
#
# A sample YAML with only roots and output paths would look like this:
#
Expand All @@ -61,9 +40,11 @@
#
# You can then call
#
# PROJECT=project1 ruby compress.rb
# ruby compress.rb -p project1
#
# PROJECT=project3 ruby compress.rb
# or
#
# ruby compress.rb -p project3
#
# This would compress and export Blueprints CSS to the respective directory, checking for my-(ie|print|screen).css and
# appending it if present
Expand Down Expand Up @@ -156,8 +137,4 @@
# }
# }

# instantiate new Compressor, passing in variables set in call
c = Compressor.new(:destination => ENV['LOCATION'], :namespace => ENV['NAMESPACE'], :project => ENV['PROJECT'])

# the meat-and-potatoes method that handles generation of all the files and saves them in the correct location
c.generate!
Compressor.new.generate!
82 changes: 67 additions & 15 deletions scripts/compress/compressor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'yaml'
require 'optparse'

class Compressor < Blueprint
# class constants
CSS_FILES = {
Expand All @@ -16,8 +18,8 @@ class Compressor < Blueprint
] unless const_defined?("TEST_FILES")

# properties
attr_accessor :namespace, :custom_css, :custom_layout, :semantic_classes
attr_reader :custom_path, :loaded_from_settings, :destination_path
attr_accessor :namespace, :custom_css, :custom_layout, :semantic_classes, :project_name
attr_reader :custom_path, :loaded_from_settings, :destination_path, :script_name

def destination_path=(path)
@destination_path = path
Expand All @@ -26,16 +28,20 @@ def destination_path=(path)

# constructor
def initialize(options = {})
process_required_files

# set up defaults
@script_name = File.basename($0)
@loaded_from_settings = false
self.namespace = ""
self.destination_path = Blueprint::BLUEPRINT_ROOT_PATH
self.custom_layout = CustomLayout.new
self.project_name = nil
self.custom_css = {}
self.semantic_classes = {}

initialize_project_from_yaml(options[:project])

self.namespace = options[:namespace] ? options[:namespace] : self.namespace || ""
self.destination_path = options[:destination] ? options[:destination] : self.destination_path || Blueprint::BLUEPRINT_ROOT_PATH
process_required_files
self.options.parse!(ARGV)
initialize_project_from_yaml(self.project_name)
end

# instance methods
Expand All @@ -46,6 +52,38 @@ def generate!
output_footer # informs the user that the CSS generation process is complete
end

def options
OptionParser.new do |o|
o.set_summary_indent(' ')
o.banner = "Usage: #{@script_name} [options]"
o.define_head "Blueprint Compressor"

o.separator ""
o.separator "options"

o.on( "-o", "--output_path=OUTPUT_PATH", String,
"Define a different path to output generated CSS files to.") { |path| self.destination_path = path }

o.on( "-n", "--namespace=BP_NAMESPACE", String,
"Define a namespace prepended to all Blueprint classes (e.g. .your-ns-span-24)") { |ns| self.namespace = ns }

o.on( "-p", "--project=PROJECT_NAME", String,
"If using the settings.yml file, PROJECT_NAME is the project name you want to export") {|project| @project_name = project }

o.on( nil, "--column_width=COLUMN_WIDTH", Integer,
"Set a new column width (in pixels) for the output grid") {|cw| self.custom_layout.column_width = cw }

o.on( nil, "--gutter_width=GUTTER_WIDTH", Integer,
"Set a new gutter width (in pixels) for the output grid") {|gw| self.custom_layout.gutter_width = gw }

o.on( nil, "--column_count=COLUMN_COUNT", Integer,
"Set a new column count for the output grid") {|cc| self.custom_layout.column_count = cc }

#o.on("-v", "--verbose", "Turn on verbose output.") { |$verbose| }
o.on("-h", "--help", "Show this help message.") { puts o; exit }
end
end

private

def process_required_files
Expand All @@ -65,7 +103,7 @@ def initialize_project_from_yaml(project_name = nil)

if (project = projects[project_name]) # checks to see if project info is present
self.namespace = project['namespace'] || ""
self.destination_path = project['path'] || Blueprint::BLUEPRINT_ROOT_PATH
self.destination_path = self.destination_path == Blueprint::BLUEPRINT_ROOT_PATH ? project['path'] : self.destination_path || Blueprint::BLUEPRINT_ROOT_PATH
self.custom_css = project['custom_css'] || {}
self.semantic_classes = project['semantic_classes'] || {}
if (layout = project['custom_layout'])
Expand All @@ -85,7 +123,7 @@ def generate_css_files
css_output += "\n\n"

# Iterate through src/ .css files and compile to individual core compressed file
css_source_file_names.each_with_index do |css_source_file, index|
css_source_file_names.each do |css_source_file|
puts " + src/#{css_source_file}"
css_output += "/* #{css_source_file} */\n" if css_source_file_names.any?

Expand All @@ -107,6 +145,11 @@ def generate_css_files
#save CSS to correct path, stripping out any extra whitespace at the end of the file
File.string_to_file(css_output.rstrip, css_output_path)
end

#attempt to generate a grid.png file
if (grid_builder = GridBuilder.new(:column_width => self.custom_layout.column_width, :gutter_width => self.custom_layout.gutter_width, :output_path => File.join(self.destination_path, 'src')))
grid_builder.generate!
end
end

def append_custom_css(css, current_file_name)
Expand Down Expand Up @@ -145,26 +188,31 @@ def generate_tests
end

def output_header
puts "\n\n"
puts " ************************************************************"
puts "\n"
puts " #{"*" * 100}"
puts " **"
puts " ** Blueprint CSS Compressor"
puts " ** Builds compressed files from the source directory."
puts " ** Loaded from settings.yml" if loaded_from_settings
puts " ** Namespace: '#{namespace}'" unless namespace.blank?
puts " ** Output to: #{destination_path}"
puts " ** Grid Settings:"
puts " ** - Column Count: #{self.custom_layout.column_count}"
puts " ** - Column Width: #{self.custom_layout.column_width}px"
puts " ** - Gutter Width: #{self.custom_layout.gutter_width}px"
puts " ** - Total Width : #{self.custom_layout.page_width}px"
puts " **"
puts " ************************************************************"
puts " #{"*" * 100}"
end

def output_footer
puts "\n\n"
puts " ************************************************************"
puts " #{"*" * 100}"
puts " **"
puts " ** Done!"
puts " ** Your compressed files and test files are now up-to-date."
puts " **"
puts " ************************************************************"
puts " #{"*" * 100}\n\n"
end

def css_file_header
Expand All @@ -179,4 +227,8 @@ def css_file_header
----------------------------------------------------------------------- */)
end

def putsv(str)
puts str if $verbose
end
end
14 changes: 10 additions & 4 deletions scripts/compress/custom_layout.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'erb'

class CustomLayout < Blueprint
# class constants
CSS_ERB_FILE = File.join(Blueprint::LIB_PATH, 'compress', 'grid.css.erb')
CSS_ERB_FILE = File.join(Blueprint::LIB_PATH, 'compress', 'grid.css.erb') unless const_defined?("CSS_ERB_FILE")

# properties
# widths/column count default to core BP settings
Expand All @@ -25,12 +26,17 @@ def page_width

# constructor
def initialize(options = {})
self.column_count = options[:column_count]
self.column_width = options[:column_width]
self.gutter_width = options[:gutter_width]
@column_count = options[:column_count]
@column_width = options[:column_width]
@gutter_width = options[:gutter_width]
end

# instance methods

def default?
self.column_width == Blueprint::COLUMN_WIDTH && self.column_count == Blueprint::COLUMN_COUNT && self.gutter_width == Blueprint::GUTTER_WIDTH
end

def generate_grid_css
# loads up erb template to evaluate custom widths
css = ERB::new(File.path_to_string(CustomLayout::CSS_ERB_FILE))
Expand Down
8 changes: 4 additions & 4 deletions scripts/compress/grid.css.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@
/* Use these classes on an element to push it into the
next column, or to pull it into the previous column. */

<% (1..(0.5*column_count).to_i).each do |column| %>
<% (1..column_count).each do |column| %>
.pull-<%= column %> { margin-left: -<%= (column * (column_width + gutter_width)).to_i %>px; }
<% end %>
<%= (1..(0.5*column_count).to_i).map {|c| ".pull-#{c}" }.join(", ") %> {float: left;}
<%= (1..column_count).map {|c| ".pull-#{c}" }.join(", ") %> {float: left;}

<% (1..(0.5*column_count)).each do |column| %>
<% (1..column_count).each do |column| %>
.push-<%= column %> { margin: 0 -<%= (column * (column_width + gutter_width)).to_i %>px 1.5em <%= (column * (column_width + gutter_width)).to_i %>px; }
<% end %>
<%= (1..(0.5*column_count).to_i).map {|c| ".push-#{c}" }.join(", ") %> {float: right;}
<%= (1..column_count).map {|c| ".push-#{c}" }.join(", ") %> {float: right;}


/* Misc classes and elements
Expand Down
49 changes: 49 additions & 0 deletions scripts/compress/grid_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
begin
require 'rubygems'
gem 'rmagick'
require 'rvg/rvg'
rescue
end

class GridBuilder < Blueprint
# included modules
begin
include Magick
rescue
end


# properties
attr_reader :column_width, :gutter_width, :output_path, :able_to_generate

# constructor
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
end

# instance methods
def generate!
return false unless self.able_to_generate
total_width = self.column_width + self.gutter_width
height = 20
RVG::dpi = 100

rvg = RVG.new((total_width.to_f/RVG::dpi).in, (height.to_f/RVG::dpi).in).viewbox(0, 0, total_width, height) do |canvas|
canvas.background_fill = 'white'

canvas.g do |column|
column.rect(self.column_width, height).styles(:fill => "#e8effb")
end

canvas.g do |baseline|
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"))
end
end
4 changes: 2 additions & 2 deletions scripts/compress/semantic_class_names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def css_from_assignments(assignments = {})
classes = []
# loop through each BP class, grabbing the full hash (containing tags, index, and CSS rules)
blueprint_classes.each do |bp_class|
classes << blueprint_assignments.find_all {|line| line[:tags] =~ Regexp.new(/^(\w+, ?)*\.#{bp_class}(, \w+)?$/)}.uniq
classes << blueprint_assignments.find_all {|line| line[:tags] =~ Regexp.new(/^(\w+, ?)*\.#{self.namespace}#{bp_class}(, \w+)?$/)}.uniq
end

# clean up the array
classes = classes.flatten.uniq

Expand All @@ -43,5 +44,4 @@ def css_from_assignments(assignments = {})
end
css
end

end

0 comments on commit 9fb4e04

Please sign in to comment.