Sunday, April 4, 2010

Rails Code Stats gem

When rails 2.2 came out it added some syntactic sugar by implementing a declarative block syntax for tests. The syntax for this looks like this:

test "make sure something really critical happens" do
  #code that tests for something really critical
end

While this syntax update is great some of the tools that come with rails still haven't been updated to handle it nicely. The stats rake task which outputs basic code statistics such as lines of code and methods per class still doesn't count these tests. In the past I just copied the rake tasks code and made a new rake task that could handle the new syntax as well as the format shoulda uses. After I started playing around with rails 3 and figured out how to put rake tasks in a gem I decided to write a gem that would generate an improved stats output.

Introducing Rails Code Stats

For this gem I didn't just copy the code and slightly improve the output. I refactored the code to clean things up as well as making it easier to extend functionality. One of the main changes was separating the code that calculates the stats from the code that displays the stats. Currently it still just outputs to the command line, but I intend to add the ability to have it output a HTML version as well. In addition to the new test syntax introduced in rails 2.2, I also added support for shoulda's test syntax and improved the code so that it should be quite trivial to add support for another test method syntax. Installing this plugin is as easy as adding the following to your rails 3 applications Gemfile.

gem 'rails_code_stats'

I am contemplating suggesting to the rails core team as an alternative to the current rails stats task. It seems that it fits with where rails 3 is trying to go, if you can easily swap out DB persistance layers and javascript engines why not modularize the code statistics tool as well. Please comment if you have any suggestions on improving this gem.

7 comments:

DanMayer said...

Cool I have done the same thing, pulled out the stats and improved it to work better with some of the changes in rails. It also works better with RSpec tests and shoulda. We originally needed to solve this problem for running stats on non rails projects as part of getcaliper.com . Perhaps we could combine efforts to improve both our gems, and as you said modularize the stats part of rails out to make it a little more generic. Take a look at the project on GitHub http://github.com/devver/code_statistics

Unknown said...

It would be nice to seen an example or listing of the stats so I could know if it's worth trying out.

Unknown said...

Currently the output looks almost character for character like the original rake stats output, except that it counts test methods with the new block syntax as well as the shoulda block syntax.

DanMayer said...

Yeah right now it only has been improving some of the detection as well as some of the options. So that you can try to change which folders are considered test or not (for the test to code ratio calculations). It doesn't improve the output at all. In fact at the moment it has been trying to stay a character for character match.

Unknown said...

I will take a look at code_statistics sometime this week and see if I can find a nice way to combine the gems. Right now I am thinking code_statistics could end up being a good place for the general statistic generation and rails_code_stats could end up being the way to nicely tie it into a rails app.

Stefan Kanev said...

I don't want to be the obnoxious one, but I believe you missed a "do" in your code example.

Unknown said...

good catch, I updated the example