Auto-launching my rails development environment

My morning routine is pretty much always the same. My wife wakes me up, saying something about “late” that I can’t quite comprehend. I writhe about on the bed a bit trying to break free from the remaining tendrils of sleep. Finally, I look at my watch and spend a moment processing the numbers.

Shit. I have to go to work.

A 30 second shower later and I am in my car heading to the office. Luckily, Stephanie understands this process enough to get it started earlier enough for me to get to work on time. I roll into my office at the last possible moment though.

Okay. I have to get to work fast. Unlock my windows box, open my MacBook, launch Outlook on my windows box, launch iTerm on my MacBook. Have you seen the scene in office space where the main character is trying to get out of work before his boss asks him to work on the weekend? That is how I feel at about this point.

iTerm is finally up. Now, I just need to open 3 tabs and start my server, my rails console, and a tail of my development log.

Am I not a programmer? Can’t I make my Mac do all of this work for me? Of course I can.

listing for /usr/local/bin/rdev

#!/bin/zsh -f

if [[  $TERM_PROGRAM != iTerm.app ]]; then
    open -a Terminal
    return 0
fi

# First, get the directory for rails

if [[ $# == 0 ]]; then
      ThisDirectory=$PWD
elif [[ $# == 1 && -d "$1" ]]; then
      ThisDirectory=”$@”
else
      print “usage: rdev [directory]”
      return 1
fi

if [[ -e "$ThisDirectory/script/server" ]]; then
      print “Starting Rails Dev Environment”
else
      print “$ThisDirectory does not look like a rails app!”
      return 1
fi

osascript <<-eof
tell application “iTerm”
	make new terminal
	tell the front terminal
		activate current session
		launch session “Default Session”
		tell the last session
			write text “cd \”$ThisDirectory\”"
		end tell
		tell the last session
			write text “./script/server”
		end tell
	end tell
end tell
tell application “iTerm”
	make new terminal
	tell the front terminal
		activate current session
		launch session “Default Session”
		tell the last session
			write text “cd \”$ThisDirectory\”"
		end tell
		tell the last session
			write text “./script/console”
		end tell
	end tell
end tell
tell application “iTerm”
	make new terminal
	tell the front terminal
		activate current session
		launch session “Default Session”
		tell the last session
			write text “cd \”$ThisDirectory\”"
		end tell
		tell the last session
			write text “tail -f log/development.log”
		end tell
	end tell
end tell
tell application “iTerm”
	activate
end tell
eof

Now, when I get in I use Quicksilver to open an iTerm in my development directory. Then I simply type “rdev” and I am ready to roll.

This code is thanks largely to the scripts available at http://xanana.ucsc.edu/xtal/iterm_tab_customization.html

4 Responses to “Auto-launching my rails development environment”

  1. J$ Says:

    Applescript is a weird language.

  2. GregH Says:

    Hi - I’m just about to try this out :) How have you been going with this method? Did you make any changes? just double-checking before I launch into it :)

  3. GregH Says:

    PS How did you set your environment variables up for ruby & rails? I’ve come from Locomotive so I’ve never had to try to work this out before? I’m getting errors like the below:

    greg$ ruby ./script/server
    ./script/../config/boot.rb:18:in `require’: No such file to load — rubygems (LoadError)
    from ./script/../config/boot.rb:18
    from ./script/server:2:in `require’
    from ./script/server:2

    tks

  4. gnubbs Says:

    Yup. That script is exactly how I use it now. Not perfect, but it works.

    As for getting rails set up and working, I would recommend:

    http://hivelogic.com/narrative/articles/ruby-rails-mongrel-mysql-osx

    That is the best article that I have seen on setting up rails on a Mac. It is more work than locomotive, but allows my rails dev environment to more closely reflect my production environment.

Leave a Reply