Luke Melia

software dev

March 10, 2008

capistrano task autocompletion for bash

Remembering all those capistrano tasks can be tough for the sleep-impaired startup employee.... Help yourself out with this autocomplete script:

RUBY:
  1. #!/usr/bin/env ruby
  2.  
  3. # Save this somewhere, chmod 755 it, then add
  4. #   complete -C path/to/this/script -o default cap
  5. # to your ~/.bashrc
  6. #
  7. # If you update your tasks, just $ rm ~/.captabs*
  8. #
  9.  
  10. exit 0 unless /^cap\b/ =~ ENV["COMP_LINE"]
  11.  
  12. def cap_silent_tasks
  13.   if File.exists?(dotcache = File.join(File.expand_path('~'), ".captabs-#{Dir.pwd.hash}"))
  14.     File.read(dotcache)
  15.   else
  16.     tasks = `cap -qT`
  17.     tasks = tasks.select{|task| task =~ /^cap/ }.join
  18.     File.open(dotcache, 'w') { |f| f.puts tasks }
  19.     tasks
  20.   end
  21. end
  22.  
  23. after_match = $'
  24. task_match = (after_match.empty? || after_match =~ /\s$/) ? nil : after_match.split.last
  25. tasks = cap_silent_tasks.split("\n")[1..-1].map { |line| line.split[1] }
  26. tasks = tasks.select { |t| /^#{Regexp.escape task_match}/ =~ t } if task_match
  27.  
  28. # handle namespaces
  29. if task_match =~ /^([-\w:]+:)/
  30.   upto_last_colon = $1
  31.   after_match = $'
  32.   tasks = tasks.map { |t| (t =~ /^#{Regexp.escape upto_last_colon}([-\w:]+)$/) ? "#{$1}" : t }
  33. end
  34.  
  35. puts tasks
  36. exit 0

2 Responses to “capistrano task autocompletion for bash”

  1. a gravatar Unnsse Khan chimed in:

    Can you give us an example of what this Ruby script does?

    Unix already supports tab completions...

    What is an "auto completion", per se'?

    Happy programming!

  2. a gravatar d. chimed in:

    Hi Luke. Congrats on the launch of weplay. Wow, the site rocks way harder than Exile on Mainstreet!!! I think you are really onto something big. Congrats on your new endeavor, and get some sleep.

Leave a Reply

LukeMelia.com created 1999. ··· Luke Melia created 1976. ··· Live With Passion!