Created
February 28, 2013 22:02
-
-
Save southpolesteve/5060497 to your computer and use it in GitHub Desktop.
Rake taks for finding unused methods in your rails app. A bit hacky and slow, but seems to work for most cases!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :cleaner do | |
task :find do | |
models = Dir.glob("app/models/**/*") | |
all_files = Dir.glob("app/**/*") | |
models.each do |model| | |
if File.file?(model) | |
File.open(model).lines.each do |line| | |
if line=~ /def ([a-z0-9._?!]*)/ | |
method_name = line.match(/def ([a-z0-9._?!]*)/)[1] | |
method_name.sub!(/self./, '') | |
use_count = 0 | |
all_files.each do |file| | |
if File.file?(file) | |
begin # Hack to handle binary files. Should probably do something else here | |
file_count = File.readlines(file).grep(Regexp.new(method_name)).size | |
rescue | |
end | |
if file_count && file_count > 0 | |
use_count = use_count + file_count | |
end | |
end | |
end | |
if use_count == 1 | |
puts "#{method_name} is a candidate for deletion" | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another revision. Haven't run this code, so I might've messed something up here: https://gist.github.com/strand/5062975