Over lunch at Scotland on Rails, Jim, Chad, and I got talking about the way you use desc to document tasks in rake.
desc "Remove intermediate files and other work products"
task :cleanup do
...
end
task :cleanup do
...
end
This has always been an interesting feature of rake—the idea that you have a kind of temporal coupling between one method call and the next. It works well, but somehow it's always struck me as a little obtrusive.
So, during the afternoon, I hacked up a quick change to rake that looks for a single-line comment on the line prior to all task, file, etc directives, and uses that as the task description. With this version of rake, you can write
# Remove intermediate files and other work products
task :cleanup do
...
end
task :cleanup do
...
end
The comment becomes the description of the task, because it occurs before the task directive with no intervening blank line. desc still works, and if both desc and a comment are present, the desc takes priority. It's purely a matter of taste, but I personally find the comment form more natural.
You can get this patched version from http://github.com/pragdavespc/rake/tree/master. I've passed the patch on to Jim, so if you like the idea, let him know and maybe he'll roll it into the next rake release.




Looks way better. This should definitely be in the next release.
Posted by: Martijn Lafeber | March 27, 2009 at 06:41 AM
I think something like
task :cleanup, "Remove intermediate files and other work products" do
# Do stuff
end
looks more natural. After all, the description is a property of the task.
Posted by: lasso | March 27, 2009 at 08:40 AM
My problems is that, although it's a pretty neat trick, using comments doesn't really fix the temporal coupling issue you described - it just hides it a little bit more with further magic.
I would much prefer something like lasso's approach where you tightly couple the description in the task method, you could easily add it as an option to the opts hash:
task :cleanup, :desc => "Remove intermediate files and other work products" do
#Do stuff
end
On a side note, this is actually one of the things I really like about the language Ioke: each method has an optional documentation cell. It's perfect for things like this :-)
Posted by: Sam Aaron | March 27, 2009 at 09:19 AM
I think the example shows the most important benefit - with syntax highlighting the description is much more visible when the description is not just another string.
Posted by: Daniel Sandbecker | March 27, 2009 at 03:40 PM
This change requires reading the file for every undocumented task. For a complex task tree this would be a huge performance killer. I don't care for it myself.
Also, people understand what comments are, and that they're not interpreted or compiled in anyway. To have them injected indirectly into the closest task? No thanks.
Posted by: Patrick | March 27, 2009 at 08:51 PM
-1
IMO, comments should stay comments. Such a feature would definitely not adhere to the principle of least surprise...
Posted by: Jake | March 28, 2009 at 01:01 PM
Ah, stealing features from Lisp... a time-honored tradition. Does this use the AST or just hand-parsing from caller?
Posted by: Phil | March 29, 2009 at 12:23 AM
I definitely agree with Iasso, Sam Aaron, and Jake. I'd rather be able to set the description as a property of the task.
Posted by: Derek Perrault | March 29, 2009 at 07:03 PM
This is nice except in situations where you are dynamically generating tasks and therefore need to dynamically generate description strings. For that desc is necessary.
Posted by: Daniel Lucraft | March 30, 2009 at 02:56 PM
i think all the approaches are fairly fugly, including the current rake behaviour. it'd be a super small backward compatible change to simply support
task "this is the description", :name => "taskname", ...
with about 2 lines of argument parsing. probably supporting
task "this is the description", "taskname"
would be good too. i see no advantage whatsoever for separating the annotation from call to task.
Posted by: ara.t.howard | September 28, 2009 at 01:30 PM