After developing in Grails for a while, I went to a Rails talk by Matt. He demoed developing a little blogging app, and I recognised that I could do most of what he was talking about just as quickly and easily in Grails.
But one cool little thing he demoed was the “rake stats” command. This gave you a bunch of metrics on your project (lines of code, test/code ratios, NCLOC, etc). Check out a sample run.
Anyways, I thought it was a neat feature, and I was keen to run a similar thing for groovyblogs, so I set aside a lunch hour putting together a cheap and cheerful version in Groovy. Certainly not anywhere near as feature complete, but it’ll get us started, and maybe someone out there will pick up the batton and run with it. Here’s my output from a “grails stats” command when run on the groovyblogs source base:
+----------------------+-------+-------+ | Name | Files | LOC | +----------------------+-------+-------+ | Controllers | 7 | 588 | | Domain Classes | 3 | 101 | | Jobs | 3 | 66 | | Services | 6 | 618 | | Tag Libraries | 2 | 207 | | Tests | 11 | 66 | | Groovy Helpers | 5 | 179 | +----------------------+-------+-------+ | Totals | 37 | 1825 | +----------------------+-------+-------+
Mine is very rudimentary, simply calculating the number of files and total lines of code (sorry, no smart stuff around whitespace or comments).
The amazing part is how quick is it to whip this sort of thing up in groovy. Building that table I became acquainted with the String.padRight() and String.padLeft() enhancements. Add the File.eachFileRecurse closure and a sprinkling of Expandos, and voila! You’ve saved an incredible amount of mucking about!
Converting to a command that can integrate with Grails commandline was pretty much a drag and drop to the “scripts” directory along with a few scaffold lines to return the name of the target. A snack!
Anyways, I’ll talk to the Grails boys and see if I can get it added to a future release. In the meantime, if you’re keen to use it in your own project, grab Stats.groovy from the downloads directory, and put it in your $GRAILS_HOME/scripts directory. Then you’re only a “grails stats” away from getting a lay of the land.