The Pug Automatic

"cap deploy" command for TextMate

Written June 8, 2007. Tagged Ruby, OS X, TextMate, Ruby on Rails, Shell scripting, Capistrano.

Capistrano allows you to, among other things, deploy a Ruby on Rails app with a simple cap deploy, abstracting away having to check out code, restart servers etc.

It's just ten characters to write in a terminal, but I still wanted a "cap deploy" command in TextMate, so I don't have to switch between apps. I was a bit surprised to find no one had made and shared such a command already, but I wrote one myself.

The command can be downloaded as cap deploy.tmCommand. Right-click and save, then double-click the downloaded file. It should slip into the Rails bundle.

After having written this command, I think I understand why no one had before. Assuming you want incremental output as the deployment progresses, things get a bit tricky.

The code (with save: nothing, input: none, output: show as HTML, no scope selector) is

# By Henrik Nyh <> 2007-06-08
# Free to modify and redistribute with credit.
# If you get errors, make sure to set your PATH in ~/.bash_profile per
# Adding /opt/local/bin (where it is for me) to avoid having to do this.
export PATH="/opt/local/bin/:$PATH"

. "${TM_SUPPORT_PATH}/lib/"

html_header "CapMate" "$TM_PROJECT_DIRECTORY"

cd "$TM_PROJECT_DIRECTORY" 2> /dev/null
require_cmd cap
[ -d app/controllers ] || exit_show_html "Not in a Rails project!"

ruby -e 'class << STDERR; alias_method :old_puts, :puts; def puts(m); old_puts(m.strip); Kernel.puts; end; end; load(`which cap`.strip);' deploy | pre


Capistrano buffers its output. After trying a whole lot of different things, the only thing that actually worked to get unbuffered, incremental output was the Ruby kludge above. STDERR.puts, which the Capistrano logger uses for output, is monkey-patched to run Kernel.puts after each piece of output.

I only started playing with Capistrano yesterday. If I find I use more of Capistrano regularly, I might write more TextMate commands, but I've no such plans at this time. If you need more commands, feel free to write and distribute your own with this as a starting point.

Update 2007-06-10
Added quotes around shell variables. Thanks, Allan.