CSS Stats for projects so that the complexities can be compared.

This commit is contained in:
Chris Eppstein 2009-10-29 11:35:20 -07:00
parent 7d04f480b9
commit 2675f8ed9a
3 changed files with 49 additions and 15 deletions

View File

@ -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 |

View File

@ -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)

View File

@ -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