Loading...

February 19, 2011

Grails Goodness: Add More Paths to the Stats Report

If we invoke $ grails stats for our Grails project we get to see the number of files and lines of code (LOC) for several Grails artifacts. For example we can see how many controllers, services and taglibs are in our project and how many lines of code is written for each.

    +----------------------+-------+-------+
    | Name                 | Files |  LOC  |
    +----------------------+-------+-------+
    | Controllers          |     1 |    89 | 
    | Domain Classes       |     1 |     5 | 
    | Jobs                 |     1 |     6 | 
    | Unit Tests           |     3 |    36 | 
    | Scripts              |     1 |     4 | 
    +----------------------+-------+-------+
    | Totals               |     7 |   140 | 
    +----------------------+-------+-------+

We can add new source directories to the report. When the stats report is generated the StatsStart event is triggered. The default list of paths is passed as the argument of the event. We can subscribe to this event in our own Grails application. Because we get the list of paths as an argument, we can define our own path info and add it to the list. We add the paths grails-app/conf and grails-app/utils to be included in the stats report.

// File: scripts/_Events.groovy
eventStatsStart = { pathInfo ->
    def confPathInfo = [name: "Configuration Files", path: "^grails-app.conf", filetype: [".groovy"]]
    def utilPathInfo = [name: "Utils", path: "^grails-app.utils", filetype: [".groovy"]]
    pathInfo << confPathInfo << utilPathInfo
}

Now we can run the stats command again and we see the new paths in our report:

    +----------------------+-------+-------+
    | Name                 | Files |  LOC  |
    +----------------------+-------+-------+
    | Controllers          |     1 |    89 | 
    | Domain Classes       |     1 |     5 | 
    | Jobs                 |     1 |     6 | 
    | Unit Tests           |     3 |    36 | 
    | Scripts              |     1 |     5 | 
    | Configuration Files  |     7 |    87 | 
    | Utils                |     1 |     5 | 
    +----------------------+-------+-------+
    | Totals               |    15 |   233 | 
    +----------------------+-------+-------+