My first home-made rake task

I wanted to be able to examine my development log for rails deprecation warnings, and came up with this handy one-liner:


grep “^DEPRECATION WARNING:” development.log | awk ‘{print $3, $4, $5, “:”, $NF}’ | sort | uniq


This gives me output something like this:


@params is deprecated! : script/../config/../app/views/waiting_messages/prepare_reply.rhtml:9)
@request is deprecated! : ./script/../config/../app/controllers/account_controller.rb:10)
You called render(‘new_member_in_program’), : /opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/li […]
end_form_tag is deprecated : script/../config/../app/views/members/bulknew.rhtml:10)
find_all is deprecated : (irb):3)
start_form_tag is deprecated : script/../config/../app/views/members/bulknew.rhtml:2)


It’s not perfect; I’m missing some useful info from some of the lines (such as the ‘You called render…’) that I’ll have to go back and pick over. But in broad strokes, it gives me a fairly concise list of some things that need to be cleaned up.


However, that one-liner is fairly meaty. Who wants to remember that, or make an alias for it, or need to look it up? Yuck.


How about a rake task? I’ve never written one before, but everyone’s always raving about how simple it is, so I figured I’d go ahead. Hopefully this helps show how simple it is. While I’m at it, I’ll go ahead and include the full text of the warning (except for the redundant “DEPRECATION WARNING” at the beginning, since we already know we’re looking for that) instead of truncating it like I did in my awk-based one-liner.


Here it is:


namespace :utils do
  desc "List deprecation warnings in development log"
  task :find_deprecated do
    File.open('log/development.log') do |file|
      lines = file.select {|line| line =~ /^DEPRECATION WARNING:/}
      lines.sort.uniq.each {|line| puts line.split('DEPRECATION WARNING: ')[1]}
    end
  end
end


Put that in a file called utils.rake inside your lib/tasks directory, and call it by typing rake utils:find_deprecated, and you’ll get something like this (lines chomped for presentation purposes):


@params is deprecated! Call params.[] instead of @params.[]. Args: [:response_text]  See http://www. [...]
@request is deprecated! Call request.accepts instead of @request.accepts. Args: []  See http://www.r [...]
You called render('new_member_in_program'), which is a deprecated API call. Instead you use render : [...]
end_form_tag is deprecated and will be removed from Rails 2.0  See http://www.rubyonrails.org/deprec [...]
find_all is deprecated and will be removed from Rails 2.0 (use find(:all, ...))  See http://www.ruby [...]
start_form_tag is deprecated and will be removed from Rails 2.0 (use form_tag instead)  See http://w [...]


There are certainly improvements that could be made, such as adding the ability to search test.log and production.log as well, but this’ll do for now.

Comments