From 06af286c4a6706974456940f354bce7af3f8f1e3 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sun, 26 Feb 2012 17:34:28 -0800 Subject: [PATCH] Add filesize to the compass stats output. --- doc-src/content/CHANGELOG.markdown | 1 + lib/compass/commands/project_stats.rb | 41 ++++++++++++++++++--------- lib/compass/stats.rb | 5 ++++ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/doc-src/content/CHANGELOG.markdown b/doc-src/content/CHANGELOG.markdown index 087fc3d3..fb347471 100644 --- a/doc-src/content/CHANGELOG.markdown +++ b/doc-src/content/CHANGELOG.markdown @@ -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 +* Add filesize to the `compass stats` output. 0.11.7 (01/05/2012) ------------------- diff --git a/lib/compass/commands/project_stats.rb b/lib/compass/commands/project_stats.rb index 42ab579e..7da2e227 100644 --- a/lib/compass/commands/project_stats.rb +++ b/lib/compass/commands/project_stats.rb @@ -34,13 +34,16 @@ module Compass compiler = new_compiler_instance sass_files = sorted_sass_files(compiler) total_label = "Total (#{sass_files.size} files):" - rows = [[ :-, :-, :-, :-, :-, :-, :- ], - [ 'Filename', 'Rules', 'Properties', 'Mixins Defs', 'Mixins Used', 'CSS Selectors', 'CSS Properties' ], - [ :-, :-, :-, :-, :-, :-, :- ]] - maximums = [ total_label.length, 5, 10, 14, 11, 13, 14 ] - alignments = [ :left, :right, :right, :right, :right, :right, :right ] - delimiters = [ ['| ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'] ] - totals = [ total_label, 0, 0, 0, 0, 0, 0 ] + rows = [[ :-, :-, :-, :-, :-, :-, :-, :-, :- ], + [ 'Filename', 'Rules', 'Properties', 'Mixins Defs', 'Mixins Used', 'Filesize', 'CSS Selectors', 'CSS Properties', 'CSS Filesize' ], + [ :-, :-, :-, :-, :-, :-, :-, :-, :- ]] + maximums = [ total_label.length, 5, 10, 14, 11, 13, 13, 14, 14 ] + alignments = [ :left, :right, :right, :right, :right, :right, :right, :right, :right ] + delimiters = [ ['| ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'] ] + 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| css_file = compiler.corresponding_css_file(sass_file) unless sass_file[0..0] == '_' @@ -53,12 +56,12 @@ module Compass end rows << row end - rows << [:-] * 7 + rows << [:-] * columns rows << totals.map{|t| t.to_s} - rows << [:-] * 7 + rows << [:-] * columns rows.each do |row| 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 print "\n" end @@ -75,11 +78,23 @@ module Compass else filler = ' ' end + c = send(:"format_#{options[:formatter]}", c) if options[:formatter] spaces = max - c.size filled = filler * [spaces,0].max "#{options[:left]}#{filled if options[:align] == :right}#{c}#{filled if options[:align] == :left}#{options[:right]}" 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) sass_files = compiler.sass_files(:exclude_partials => false) sass_files.map! do |s| @@ -100,7 +115,7 @@ module Compass def sass_columns(sass_file) sf = Compass::Stats::SassFile.new(sass_file) 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 end end @@ -109,11 +124,11 @@ module Compass if File.exists?(css_file) cf = Compass::Stats::CssFile.new(css_file) 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 end else - return [ '--', '--' ] + return [ '--', '--' , '--'] end rescue LoadError @missing_css_parser = true diff --git a/lib/compass/stats.rb b/lib/compass/stats.rb index 243b42af..28de86e4 100644 --- a/lib/compass/stats.rb +++ b/lib/compass/stats.rb @@ -29,6 +29,7 @@ module Compass class CssFile attr_accessor :path, :css attr_accessor :selector_count, :prop_count + attr_accessor :file_size def initialize(path) require 'css_parser' self.path = path @@ -44,6 +45,7 @@ module Compass contents.inject(0){|m,c| m + 1 } end def analyze! + self.file_size = File.size(path) css.each_selector do |selector, declarations, specificity| sels = selector.split(/,/).size props = declarations.split(/;/).size @@ -55,6 +57,8 @@ module Compass class SassFile attr_accessor :path attr_reader :visitor + attr_accessor :file_size + def initialize(path) self.path = path end @@ -72,6 +76,7 @@ module Compass @visitor end def analyze! + self.file_size = File.size(path) visit_tree! end def lines