ruby-lang
March 8th, 2006, 10:20 PM
<p>Those of you who love running Rake tasks but don’t like typing are in for a treat. Although there’s been task completion for Rake for a while now, most of the scripts for it are painfully slow, especially with Rails’ Rakefile.</p>
<p>Below is a small zsh completion script that uses a cache file (named <tt>.rake_tasks</tt>) to improve the performance of your tab keystrokes.</p>
<p>To use, throw it in your home folder somewhere and add <tt>source $HOME/.rake_completion.zsh</tt> to your <tt>.zshrc</tt> file.</p>
<p>A few disclaimers: Yes, it doesn’t work with lowercase named <tt>rakefile</tt>’s. Only barbarians use such names though, so hopefully you won’t have a problem there. And no, it doesn’t complete the other assorted arguments that the rake command can accept, frankly because I rarely use them.</p>
<p>Without further ado, here’s the bytes.</p>
<pre>
_rake_does_task_list_need_generating () {
if [ ! -f .rake_tasks ]; then return 0;
else
accurate=$(stat -f%m .rake_tasks)
changed=$(stat -f%m Rakefile)
return $(expr $accurate '>=' $changed)
fi
}
_rake () {
if [ -f Rakefile ]; then
if _rake_does_task_list_need_generating; then
echo "\nGenerating .rake_tasks..." > /dev/stderr
rake --silent --tasks | cut -d " " -f 2 > .rake_tasks
fi
compadd `cat .rake_tasks`
fi
}
compdef _rake rake
</pre>
<p>(Use at your own risk. Comments and improvements welcome.)</p>
<a href="http://weblog.rubyonrails.com/articles/2006/03/09/fast-rake-task-completion-for-zsh" target="_blank">http://weblog.rubyonrails.com/articles/2006/03/09/fast-rake-task-completion-for-zsh</a>
<p>Below is a small zsh completion script that uses a cache file (named <tt>.rake_tasks</tt>) to improve the performance of your tab keystrokes.</p>
<p>To use, throw it in your home folder somewhere and add <tt>source $HOME/.rake_completion.zsh</tt> to your <tt>.zshrc</tt> file.</p>
<p>A few disclaimers: Yes, it doesn’t work with lowercase named <tt>rakefile</tt>’s. Only barbarians use such names though, so hopefully you won’t have a problem there. And no, it doesn’t complete the other assorted arguments that the rake command can accept, frankly because I rarely use them.</p>
<p>Without further ado, here’s the bytes.</p>
<pre>
_rake_does_task_list_need_generating () {
if [ ! -f .rake_tasks ]; then return 0;
else
accurate=$(stat -f%m .rake_tasks)
changed=$(stat -f%m Rakefile)
return $(expr $accurate '>=' $changed)
fi
}
_rake () {
if [ -f Rakefile ]; then
if _rake_does_task_list_need_generating; then
echo "\nGenerating .rake_tasks..." > /dev/stderr
rake --silent --tasks | cut -d " " -f 2 > .rake_tasks
fi
compadd `cat .rake_tasks`
fi
}
compdef _rake rake
</pre>
<p>(Use at your own risk. Comments and improvements welcome.)</p>
<a href="http://weblog.rubyonrails.com/articles/2006/03/09/fast-rake-task-completion-for-zsh" target="_blank">http://weblog.rubyonrails.com/articles/2006/03/09/fast-rake-task-completion-for-zsh</a>