Add filesize to the compass stats output.

This commit is contained in:
Chris Eppstein 2012-02-26 17:34:28 -08:00
parent fee53b1a60
commit 06af286c4a
3 changed files with 34 additions and 13 deletions

View File

@ -18,6 +18,7 @@ The Documentation for the [latest preview release](http://beta.compass-style.org
------------------- -------------------
* Fix a bug in gradients that used the transparent keyword * Fix a bug in gradients that used the transparent keyword
* Add filesize to the `compass stats` output.
0.11.7 (01/05/2012) 0.11.7 (01/05/2012)
------------------- -------------------

View File

@ -34,13 +34,16 @@ module Compass
compiler = new_compiler_instance compiler = new_compiler_instance
sass_files = sorted_sass_files(compiler) sass_files = sorted_sass_files(compiler)
total_label = "Total (#{sass_files.size} files):" total_label = "Total (#{sass_files.size} files):"
rows = [[ :-, :-, :-, :-, :-, :-, :- ], rows = [[ :-, :-, :-, :-, :-, :-, :-, :-, :- ],
[ 'Filename', 'Rules', 'Properties', 'Mixins Defs', 'Mixins Used', 'CSS Selectors', 'CSS Properties' ], [ 'Filename', 'Rules', 'Properties', 'Mixins Defs', 'Mixins Used', 'Filesize', 'CSS Selectors', 'CSS Properties', 'CSS Filesize' ],
[ :-, :-, :-, :-, :-, :-, :- ]] [ :-, :-, :-, :-, :-, :-, :-, :-, :- ]]
maximums = [ total_label.length, 5, 10, 14, 11, 13, 14 ] maximums = [ total_label.length, 5, 10, 14, 11, 13, 13, 14, 14 ]
alignments = [ :left, :right, :right, :right, :right, :right, :right ] alignments = [ :left, :right, :right, :right, :right, :right, :right, :right, :right ]
delimiters = [ ['| ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'] ] delimiters = [ ['| ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'] ]
totals = [ total_label, 0, 0, 0, 0, 0, 0 ] formatters = [ nil, nil, nil, nil, nil, :kb, nil, nil, :kb ]
totals = [ total_label, 0, 0, 0, 0, 0, 0, 0, 0 ]
columns = rows.first.size
sass_files.each do |sass_file| sass_files.each do |sass_file|
css_file = compiler.corresponding_css_file(sass_file) unless sass_file[0..0] == '_' css_file = compiler.corresponding_css_file(sass_file) unless sass_file[0..0] == '_'
@ -53,12 +56,12 @@ module Compass
end end
rows << row rows << row
end end
rows << [:-] * 7 rows << [:-] * columns
rows << totals.map{|t| t.to_s} rows << totals.map{|t| t.to_s}
rows << [:-] * 7 rows << [:-] * columns
rows.each do |row| rows.each do |row|
row.each_with_index do |col, i| row.each_with_index do |col, i|
print pad(col, maximums[i], :align => alignments[i], :left => delimiters[i].first, :right => delimiters[i].last) print pad(col, maximums[i], :align => alignments[i], :left => delimiters[i].first, :right => delimiters[i].last, :formatter => formatters[i])
end end
print "\n" print "\n"
end end
@ -75,11 +78,23 @@ module Compass
else else
filler = ' ' filler = ' '
end end
c = send(:"format_#{options[:formatter]}", c) if options[:formatter]
spaces = max - c.size spaces = max - c.size
filled = filler * [spaces,0].max filled = filler * [spaces,0].max
"#{options[:left]}#{filled if options[:align] == :right}#{c}#{filled if options[:align] == :left}#{options[:right]}" "#{options[:left]}#{filled if options[:align] == :right}#{c}#{filled if options[:align] == :left}#{options[:right]}"
end end
def format_kb(v)
return v unless v =~ /^\d+$/
v = Integer(v)
if v < 1024
"#{v} B"
else
v = v / 1024.0
"#{v.ceil} KB"
end
end
def sorted_sass_files(compiler) def sorted_sass_files(compiler)
sass_files = compiler.sass_files(:exclude_partials => false) sass_files = compiler.sass_files(:exclude_partials => false)
sass_files.map! do |s| sass_files.map! do |s|
@ -100,7 +115,7 @@ module Compass
def sass_columns(sass_file) def sass_columns(sass_file)
sf = Compass::Stats::SassFile.new(sass_file) sf = Compass::Stats::SassFile.new(sass_file)
sf.analyze! sf.analyze!
%w(rule_count prop_count mixin_def_count mixin_count).map do |t| %w(rule_count prop_count mixin_def_count mixin_count file_size).map do |t|
sf.send(t).to_s sf.send(t).to_s
end end
end end
@ -109,11 +124,11 @@ module Compass
if File.exists?(css_file) if File.exists?(css_file)
cf = Compass::Stats::CssFile.new(css_file) cf = Compass::Stats::CssFile.new(css_file)
cf.analyze! cf.analyze!
%w(selector_count prop_count).map do |t| %w(selector_count prop_count file_size).map do |t|
cf.send(t).to_s cf.send(t).to_s
end end
else else
return [ '--', '--' ] return [ '--', '--' , '--']
end end
rescue LoadError rescue LoadError
@missing_css_parser = true @missing_css_parser = true

View File

@ -29,6 +29,7 @@ module Compass
class CssFile class CssFile
attr_accessor :path, :css attr_accessor :path, :css
attr_accessor :selector_count, :prop_count attr_accessor :selector_count, :prop_count
attr_accessor :file_size
def initialize(path) def initialize(path)
require 'css_parser' require 'css_parser'
self.path = path self.path = path
@ -44,6 +45,7 @@ module Compass
contents.inject(0){|m,c| m + 1 } contents.inject(0){|m,c| m + 1 }
end end
def analyze! def analyze!
self.file_size = File.size(path)
css.each_selector do |selector, declarations, specificity| css.each_selector do |selector, declarations, specificity|
sels = selector.split(/,/).size sels = selector.split(/,/).size
props = declarations.split(/;/).size props = declarations.split(/;/).size
@ -55,6 +57,8 @@ module Compass
class SassFile class SassFile
attr_accessor :path attr_accessor :path
attr_reader :visitor attr_reader :visitor
attr_accessor :file_size
def initialize(path) def initialize(path)
self.path = path self.path = path
end end
@ -72,6 +76,7 @@ module Compass
@visitor @visitor
end end
def analyze! def analyze!
self.file_size = File.size(path)
visit_tree! visit_tree!
end end
def lines def lines