Hacking rake:stats to get gross LOC

28 06 2007

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:

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!


Actions

Informations

3 responses to “Hacking rake:stats to get gross LOC”

15 07 2008
Scott Andreas (00:41:10) :

Thanks, I’ve been looking for something like this. Funny to stumble on it almost a year later!

26 01 2009
Kevin Marsh (11:51:19) :

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!

26 01 2009
Kevin Marsh (11:56:44) :

I tweaked this a bit, this adds a new task stats:static that just reports on HTML and CSS:

http://gist.github.com/52889

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>