How to find the name of the current Rake task

A few weeks ago I needed a Rake task to answer a simple question for me:

“What’s my name?”

I honestly can’t quite remember why exactly the task needed to know it’s own name, but at the time it seemed important and I spent a couple hours digging about before I found out that Rake tasks are shy by nature. Or at least when implemented the way almost every tutorial tells you to implement them:

namespace :life do

  desc "The answer to life, the universe, and everything"
  task :answer do
    puts "The answer to life, the universe, and everything is 42."
  end

end

But how do I know what the name of the current task is? Well, after making a couple of random attempts (like “puts task.name”) and digging around in the very sparse documentation on the Rake homepage, I stumbled across a bit of documentation that talked about “Tasks with Actions”:

Actions are defined by passing a block to the task method. Any Ruby code can be placed in the block. The block may reference the task object via the block parameter.

task :name => [:prereq1, :prereq2] do |t|
  # actions (may reference t)
end

Ah ha! “The block may reference the task object via the block parameter.” That’s exactly what I was looking for. Adding the block parameter allows us to reference the task object itself. And the task object itself has a good bit of useful meta information:

namespace :life do

  desc "The answer to life, the universe, and everything"
  task :answer do |t|
    puts t.name
    puts t.comment
    puts t.scope
  end

end

The task now returns the following:

life:answer
The answer to life, the universe, and everything
life

The first line of output is nifty, and answers the “What’s my name?” question with the fully scoped name of the task. Given that much it’s probably not hard to return the task name in even the most deeply namespaced Rakefile. The second line is the “comment” — actually the description of the task. And the third line gets us the scope (namespaces). There are more methods available, but check teh googles for “Rake::Task api”.

So with this meta information we can do something useful like:

namespace :life do

  desc "The answer to life, the universe, and everything"
  task :answer do |t|
    puts "#{t.comment} is 42."
  end

end

Voila. And that’s probably enough meta for one day.


Posted: November 14th, 2009 by Neal Enssle
Tags: , , , , ,
No Comments »

How to filter Subversion logs by date and author

I keep forgetting this snippet, and then spend hours re-discovering/re-inventing it. So for the record: Here’s a bit of magic to let you filter your Subversion (SVN) logs by date and author:

svn log -vr {20091014}:"HEAD" | sed -n "/nealenssle/,/-----$/ p"

I use it in a bash shell script function that I’ve got in my .bash_profile script like so:

function svnlog {
    svn log -vr {$1}:"HEAD" | sed -n "/$2/,/-----$/ p"
}

And then call it like so:

svnlog 20091014 nealenssle

Happy spelunking!


Posted: October 14th, 2009 by Neal Enssle
Tags: , , , ,
No Comments »

How to define multiple users in a Capistrano deployment file

We’re just starting to get into Ruby on Rails at my work, and we’re close to deploying our first application to a production server with Capistrano.

Capistrano makes it easy for developers to deploy code to the production server from their own workstations. But we quickly ran into two problems:

  1. It’s often the case that a developer’s username on their local workstation is different from their username on the production server. This requires you to set the developer’s server username in the “deploy.rb” file. Hard-coded.
  2. Hard-coding a single username makes it difficult for multiple users to share a “deploy.rb” file.

Well, it turns out that Capistrano is better than all that. With a little bit of conditional logic and the ability for Cap to read environment variables, the problem is solved like this in the “deploy.rb” file:

if ENV["USER"] == "robert"
    set :user, "bob"
elsif ENV["USER"] == "elizabeth"
    set :user, "beth"
end

Voila.


Posted: April 11th, 2007 by Neal Enssle
Tags: , , , ,
No Comments »