RailsのStatsタスクに調査対象ディレクトリを追加してみる
どうもです。rake stats
コマンドを使っていますか?これは一言で言えばプロジェクトの大まかな統計情報を取るためのコマンドです。リファクタリングすべきかどうか、テストコードを書くべきかどうかをざっくり判断したいときに使ったりします
$ rake stats +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | xxx | xxx | xxx | xxx | xxx | xxx | | Helpers | xxx | xxx | 0 | xxx | xxx | xxx | | Models | xxx | xxx | xxx | xxx | xxx | xxx | | Libraries | xxx | xxx | xxx | xxx | xxx | xxx | | Integration tests | 0 | 0 | 0 | 0 | 0 | 0 | | Functional tests | xxx | xxx | xxx | 0 | 0 | 0 | | Unit tests | xxx | xxx | xxx | xxx | 0 | xxx | | Cucumber features | xxx | xxx | 0 | xxx | 0 | xxx | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | xxx | xxx | xxx | xxx | xxx | xxx | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: xxx Test LOC: xxx Code to Test Ratio: xxx:xxx
※xxxは実際の数字をぼかしているだけです
各項目の具体的な意味は上記を参考に・・
ただ、上記で対象となっているディレクトリはデフォルトのものとGemをインストールしたときに追加されたものしか含まれていません
独自に作ったlib/request/
以下を対象としたい場合などに困りますね
調査対象を独自に追加する
具体的な実装はrails/code_statistics.rbに、拡張方法はRSpecのコードなどを見れば大体わかります(内容は端折ります) 調査対象は::STATS_DIRECTORIESという2次元配列に
> ::STATS_DIRECTORIES => [["Controllers", "xxx/app/controllers"], 略 ["Cucumber features", "features"]]
みたいな感じに表示名とパスが入っているので単純に対象を追加してやればいいです
例えば、lib/tasks/hoge_huga.rb
ファイルに以下のような記述をします
namespace :hoge_huga do task :statsetup do require 'rails/code_statistics' ::STATS_DIRECTORIES << %w(Custom\ Requests lib/requests) if File.exist?('lib/requests') end end task stats: 'hoge_huga:statsetup'
こうすれば既存のstatsタスクが実行される前に必ずhoge_huga:statsetupタスクが実行されるので対象が追加されます
実行結果は以下のとおり
$ rake stats +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | xxx | xxx | xxx | xxx | xxx | xxx | | Helpers | xxx | xxx | 0 | xxx | xxx | xxx | | Models | xxx | xxx | xxx | xxx | xxx | xxx | | Libraries | xxx | xxx | xxx | xxx | xxx | xxx | | Integration tests | 0 | 0 | 0 | 0 | 0 | 0 | | Functional tests | xxx | xxx | xxx | 0 | 0 | 0 | | Unit tests | xxx | xxx | xxx | xxx | 0 | xxx | | Cucumber features | xxx | xxx | 0 | xxx | 0 | xxx | | Custom Requests | xxx | xxx | xxx | xxx | xxx | xxx | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | xxx | xxx | xxx | xxx | xxx | xxx | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: xxx Test LOC: xxx Code to Test Ratio: xxx:xxx
ちなみに対象がテストコードである場合、テスト系の項目に数値を反映させるために
::CodeStatistics::TEST_TYPESに表示名を追加する必要があります
namespace :hoge_huga do task :statsetup do require 'rails/code_statistics' ::STATS_DIRECTORIES << %w(Custom\ Requests lib/requests) if File.exist?('lib/requests') ::CodeStatistics::TEST_TYPES << "Custom Requests" if File.exist?('lib/requests') end end task stats: 'hoge_huga:statsetup'
まあ、これを追加する必要のある人は少ないと思いますが・・