From 2675f8ed9ab73175ea132490216dc67ef0f7665b Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Thu, 29 Oct 2009 11:35:20 -0700 Subject: [PATCH] CSS Stats for projects so that the complexities can be compared. --- features/command_line.feature | 11 ++++---- lib/compass/commands/project_stats.rb | 37 ++++++++++++++++++++------- lib/compass/stats.rb | 16 +++++++++++- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/features/command_line.feature b/features/command_line.feature index b77b0912..6aa615c7 100644 --- a/features/command_line.feature +++ b/features/command_line.feature @@ -210,9 +210,10 @@ Feature: Command Line Given I am using the existing project in test/fixtures/stylesheets/compass When I run: compass stats Then I am told statistics for each file: - | Filename | Rules | Properties | Mixins Defs | Mixins Used | - | sass/layout.sass | 0 | 0 | 0 | 1 | - | sass/print.sass | 0 | 0 | 0 | 2 | - | sass/reset.sass | 4 | 1 | 0 | 2 | - | sass/utilities.sass | 2 | 0 | 0 | 2 | + | Filename | Rules | Properties | Mixins Defs | Mixins Used | CSS Rules | CSS Properties | + | sass/layout.sass | 0 | 0 | 0 | 1 | 5 | 9 | + | sass/print.sass | 0 | 0 | 0 | 2 | 61 | 61 | + | sass/reset.sass | 4 | 1 | 0 | 2 | 191 | 665 | + | sass/utilities.sass | 2 | 0 | 0 | 2 | 5 | 11 | + | Total.* | 6 | 1 | 0 | 7 | 262 | 746 | diff --git a/lib/compass/commands/project_stats.rb b/lib/compass/commands/project_stats.rb index 2cabb0d7..05d20acf 100644 --- a/lib/compass/commands/project_stats.rb +++ b/lib/compass/commands/project_stats.rb @@ -33,33 +33,37 @@ module Compass require 'compass/stats' compiler = new_compiler_instance sass_files = sorted_sass_files(compiler) - rows = [[ :-, :-, :-, :-, :- ], - [ 'Filename', 'Rules', 'Properties', 'Mixins Defs', 'Mixins Used' ], - [ :-, :-, :-, :-, :- ]] - maximums = [ 8, 5, 10, 14, 11 ] - alignments = [ :left, :right, :right, :right, :right ] - delimiters = [ ['| ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'] ] - totals = [ "Total (#{sass_files.size} files):", 0, 0, 0, 0 ] + rows = [[ :-, :-, :-, :-, :-, :-, :- ], + [ 'Filename', 'Rules', 'Properties', 'Mixins Defs', 'Mixins Used', 'CSS Rules', 'CSS Properties' ], + [ :-, :-, :-, :-, :-, :-, :- ]] + maximums = [ 8, 5, 10, 14, 11, 9, 14 ] + alignments = [ :left, :right, :right, :right, :right, :right, :right ] + delimiters = [ ['| ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'], [' ', ' |'] ] + totals = [ "Total (#{sass_files.size} files):", 0, 0, 0, 0, 0, 0 ] sass_files.each do |sass_file| css_file = compiler.corresponding_css_file(sass_file) unless sass_file[0..0] == '_' row = filename_columns(sass_file) row += sass_columns(sass_file) + row += css_columns(css_file) row.each_with_index do |c, i| maximums[i] = [maximums[i].to_i, c.size].max totals[i] = totals[i] + c.to_i if i > 0 end rows << row end - rows << [:-, :-, :-, :-, :-] + rows << [:-] * 7 rows << totals.map{|t| t.to_s} - rows << [:-, :-, :-, :-, :-] + rows << [:-] * 7 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) end print "\n" end + if @missing_css_parser + puts "\nInstall css_parser to enable stats on your css files:\n\n\tgem install css_parser" + end end def pad(c, max, options = {}) @@ -100,6 +104,21 @@ module Compass end end + def css_columns(css_file) + if File.exists?(css_file) + cf = Compass::Stats::CssFile.new(css_file) + cf.analyze! + %w(selector_count prop_count).map do |t| + cf.send(t).to_s + end + else + return [ '--', '--' ] + end + rescue LoadError + @missing_css_parser = true + return [ 'DISABLED', 'DISABLED' ] + end + class << self def option_parser(arguments) diff --git a/lib/compass/stats.rb b/lib/compass/stats.rb index 18a93790..30f59093 100644 --- a/lib/compass/stats.rb +++ b/lib/compass/stats.rb @@ -27,9 +27,15 @@ module Compass end end class CssFile - attr_accessor :path + attr_accessor :path, :css + attr_accessor :selector_count, :prop_count def initialize(path) + require 'css_parser' self.path = path + self.css = CssParser::Parser.new + self.css.add_block!(contents) + self.selector_count = 0 + self.prop_count = 0 end def contents @contents ||= File.read(path) @@ -37,6 +43,14 @@ module Compass def lines contents.inject(0){|m,c| m + 1 } end + def analyze! + css.each_selector do |selector, declarations, specificity| + sels = selector.split(/,/).size + props = declarations.split(/;/).size + self.selector_count += sels + self.prop_count += props + end + end end class SassFile attr_accessor :path