{"id":30,"date":"2007-06-28T13:05:03","date_gmt":"2007-06-28T19:05:03","guid":{"rendered":"http:\/\/www.pervasivecode.com\/blog\/2007\/06\/28\/hacking-rakestats-to-get-gross-loc\/"},"modified":"2007-06-28T13:09:36","modified_gmt":"2007-06-28T19:09:36","slug":"hacking-rakestats-to-get-gross-loc","status":"publish","type":"post","link":"http:\/\/www.pervasivecode.com\/blog\/2007\/06\/28\/hacking-rakestats-to-get-gross-loc\/","title":{"rendered":"Hacking rake:stats to get gross LOC"},"content":{"rendered":"<p><a href=\"http:\/\/particletree.com\/features\/web-app-autopsy\/\">Web App Autopsy<\/a> 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 <a href=\"http:\/\/rubyforge.org\/projects\/rake\/\">Rake<\/a> task that&#8217;s part of Rails, which counts lines of source code and provides some basic analysis) doesn&#8217;t count. So, I hacked rake:stats to include them. Here&#8217;s what I did:<br \/>\n<!--more--><br \/>\nI added this code to my Rakefile:<\/p>\n<pre>\r\nnamespace :spec do\r\n  desc \"Add files that DHH doesn't consider to be 'code' to stats\"\r\n  task :statsetup do\r\n  require 'code_statistics'\r\n\r\n  class CodeStatistics\r\n    alias calculate_statistics_orig calculate_statistics\r\n    def calculate_statistics\r\n      @pairs.inject({}) do |stats, pair|\r\n        if 3 == pair.size\r\n          stats[pair.first] = calculate_directory_statistics(pair[1], pair[2]); stats\r\n        else\r\n          stats[pair.first] = calculate_directory_statistics(pair.last); stats\r\n        end\r\n      end\r\n    end\r\n  end\r\n  ::STATS_DIRECTORIES << ['Views',  'app\/views', \/\\.(rhtml|erb|rb)$\/]\r\n  ::STATS_DIRECTORIES << ['Test Fixtures',  'test\/fixtures', \/\\.yml$\/]\r\n  ::STATS_DIRECTORIES << ['Email Fixtures',  'test\/fixtures', \/\\.txt$\/]\r\n  # note, I renamed all my rails-generated email fixtures to add .txt\r\n  ::STATS_DIRECTORIES << ['Static HTML', 'public', \/\\.html$\/]\r\n  ::STATS_DIRECTORIES << ['Static CSS',  'public', \/\\.css$\/]\r\n  # ::STATS_DIRECTORIES << ['Static JS',  'public', \/\\.js$\/]\r\n  # prototype is ~5384 LOC all by itself - very hard to filter out\r\n\r\n  ::CodeStatistics::TEST_TYPES << \"Test Fixtures\"\r\n  ::CodeStatistics::TEST_TYPES << \"Email Fixtures\"\r\n  end\r\nend\r\ntask :stats => \"spec:statsetup\"\r\n<\/pre>\n<p>(This code is based on <a href=http:\/\/rubyforge.org\/pipermail\/rspec-devel\/2006-August\/000494.html\">this code I found that does something related<\/a>.)<\/p>\n<p>As you can see it doesn&#8217;t count JavaScript because the included Prototype code that&#8217;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 &#8216;svn export&#8217; temporary directory, and run rake:stats in there to see what your own JavaScript stuff contributes.<\/p>\n<p>(I tried to get a fancy regex going that excluded the six files that make up Prototype, but I couldn&#8217;t get it to work quickly. I don&#8217;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&#8217;d love to see it, just post a comment.)<\/p>\n<p>Hope this proves useful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s part of Rails, which counts lines of source code and provides some basic &hellip; <a href=\"http:\/\/www.pervasivecode.com\/blog\/2007\/06\/28\/hacking-rakestats-to-get-gross-loc\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Hacking rake:stats to get gross LOC&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,26,20,9],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-articles","category-ruby","category-ruby-on-rails","category-tools"],"_links":{"self":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/30"}],"collection":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":0,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}