Capistrano completion for zsh under Ubuntu

Seeking a way to extend completion in zsh for Capistrano tasks for a Rails project, I found a script that apparently works for OS X. However, it didn’t seem to work out of the box for Ubuntu, so I made a few modifications. Put this in a file and source in your .zshrc file, and you should be set!

_cap_does_task_list_need_generating () {
  if [ ! -f .cap_tasks ]; then return 0;
  else
    accurate=$(stat -c "%y" .cap_tasks)
    changed=$(stat -c "%y" config/deploy.rb)
    return $(expr $accurate '>=' $changed)
  fi
}

_cap () {
  if [ -f config/deploy.rb ]; then
    if _cap_does_task_list_need_generating; then
      cap -T | cut -d " " -f 2 | sed -e '/^ *$/D' -e '1,2D' >! .cap_tasks
    fi
    compadd `cat .cap_tasks`
  fi
}

compdef _cap capompdef _cap cap

The first time that you attempt completion with cap, the available tasks will be analyzed and cached. Then, future completions will work correctly and quickly. If you have any problems, deleting the .cap_tasks file will signal that the generating process needs to happen again.

Categories: development

« The Pomodoro Technique Growing Up »

Comments