Friday, January 16, 2009

Ack in Project Skipping Rake

Ack in Project is probably the TextMate bundle I use the most (after the Ruby bundle, I suppose, which I constantly use without even thinking about it). If you haven't already, you should install it immediately for very fast project searches (and recursive searches of just the selected directory, which on its own is hugely useful).

What it took me a while to realize, though, is that my project's Rake files weren't being searched. It turns out this is because Ack will by default search every file of a known type and ignore everything else. Ack knows Ruby as .rb, .rhtml, .rjs, .rxml and .erb, but that's all by default. (You can see what extensions are associated with what types from the command line with ack --help types. This assumes you've got ack installed on your path somewhere.)

To teach Ack about new extensions or entirely new types, create an .ackrc file in your home directory and fill it with things like this:

# Add the .rake extension to the existing ruby type.
--type-add
ruby=.rake

# Create a new type for Clojure source files.
--type-set
clojure=.clj

(type-set can also be used to completely replace a built in type definition. Read about this and all sorts of other ack options from the command line with ack --man.)

In addition to getting .ackrc in place, you also have to tell the TextMate bundle that you want to use it. From inside TextMate do an Ack in Project, hit the "Advanced Options" twisty, and check "load defaults from .ackrc." You're all set.

It may have occurred to you that your top-level Rakefile doesn't have an extension. Likewise for the executables in your Rails projects' script directory. And your Capfile.

Damn.

Luckily, Ack knows unix-ey people like dropping extensions from executables and using a shebang line to tell the shell how to run them. So any file with an unrecognized extension (or none at all) will be checked for a shebang line that might qualify it for the perl, ruby, php, python, shell or xml types!

So that takes care of RAILS_ROOT/script/*. As far as Rakefile and Capfile ... well, the shebang line would really be a lie, since they're not executable without the supporting Rake and Capistrano libraries loaded, but the workaround of adding the shebang will get them into your Ack results. I leave it to you to decide whether the lying shebang or excluded files is a lesser evil, and promise to let you know if I find a better way to get Ack to consider them Ruby.

Thanks for reading!

3 comments:

Andy Lester said...

You could certainly post your question to the ack-users list. Sign up form on http://petdance.com/ack/

Are your Rake files always called 'Rakefile'? Ack will recognize them if so.

Is .rake a more-or-less standard convention these days?

Anonymous said...

rakefile.rb is a valid top level rakefile name also, it's just not the convention Rails follows. A simple rename can take care of the issue.

Anonymous said...

Humidor. El even. nice to see you online. I wanted to do this outside of TM just today (was not always remembering the -a option). so thanks for answering my question.