Web App Autopsy has some juicy metrics such as the 100:10:1 ratio of anonymous visitors to free registered users to paying users. But they also have LOC counts which seem quite high, and which include things that rake:stats (a Rake task that’s part of Rails, which counts lines of source code and provides some basic analysis) doesn’t count. So, I hacked rake:stats to include them. Here’s what I did:
I added this code to my Rakefile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
namespace :spec do desc "Add files that DHH doesn't consider to be 'code' to stats" task :statsetup do require 'code_statistics' class CodeStatistics alias calculate_statistics_orig calculate_statistics def calculate_statistics @pairs.inject({}) do |stats, pair| if 3 == pair.size stats[pair.first] = calculate_directory_statistics(pair[1], pair[2]); stats else stats[pair.first] = calculate_directory_statistics(pair.last); stats end end end end ::STATS_DIRECTORIES << ['Views', 'app/views', /\.(rhtml|erb|rb)$/] ::STATS_DIRECTORIES << ['Test Fixtures', 'test/fixtures', /\.yml$/] ::STATS_DIRECTORIES << ['Email Fixtures', 'test/fixtures', /\.txt$/] # note, I renamed all my rails-generated email fixtures to add .txt ::STATS_DIRECTORIES << ['Static HTML', 'public', /\.html$/] ::STATS_DIRECTORIES << ['Static CSS', 'public', /\.css$/] # ::STATS_DIRECTORIES << ['Static JS', 'public', /\.js$/] # prototype is ~5384 LOC all by itself - very hard to filter out ::CodeStatistics::TEST_TYPES << "Test Fixtures" ::CodeStatistics::TEST_TYPES << "Email Fixtures" end end task :stats => "spec:statsetup" |
(This code is based on this code I found that does something related.)
As you can see it doesn’t count JavaScript because the included Prototype code that’s included by default in Rails projects is huge-mungous. You can just uncomment that and see your project grow by 5KLOC if you want. Or you can temporarily delete those files, maybe in a quick ‘svn export’ temporary directory, and run rake:stats in there to see what your own JavaScript stuff contributes.
(I tried to get a fancy regex going that excluded the six files that make up Prototype, but I couldn’t get it to work quickly. I don’t care enough about that one issue to keep spending time on it, but if you figure out a clean way to get that done I’d love to see it, just post a comment.)
Hope this proves useful!
Thanks, I’ve been looking for something like this. Funny to stumble on it almost a year later!
Pretty clever code, although it negatively impacts the Code to Test Ratio numbers. Could probably hack it further and add the notion of untestable code, but this works pretty well for a quick guesstimate. Thanks!
I tweaked this a bit, this adds a new task stats:static that just reports on HTML and CSS:
http://gist.github.com/52889
Hi Jamie,
I added your code to my RakeFile but I’m getting this error on line 03: “no such file to load — code_statistics”. How do I get around this?
Thanks a lot,
SSCirrus
CodeStatistics is part of Rails so it’s probably a matter of “require”-ing the right stuff in the top of your Rakefile. Mine looks like this:
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
Thanks for this post!!
For solve this problem:
such file to load — code_statistics
You need:
require ‘rails/code_statistics’
Or you can use:
https://github.com/rails/rails/blob/master/railties/lib/rails/tasks/statistics.rake
require 'code_statistics'
Should now be
require 'rails/code_statistics'