Shutdown and restart (automated dev env setup)
A laptop had been my main development computer for the past four or five years, until January. Since then, I’ve been using a Mac Mini and it’s taken me some time to develop a habit for turning it off at night. The reason I dislike shutting the machine down is the time it takes to setup my development environment each morning. This dislike is nothing compared to the grief I give myself for wasting electricity. The decision was simple, I needed to automate the setup of my development environment. First on the agenda was to launch Terminal.app and open tabs from a shell script. I found this script which can do that for me:
#!/bin/sh -
# newtab
if [ $# -ne 1 ]; then
PATHDIR=`pwd`
else
PATHDIR=$1
fi
/usr/bin/osascript > /dev/null <<-EOF
activate application "Terminal"
tell application "System Events"
keystroke "t" using {command down}
end tell
tell application "Terminal"
repeat with win in windows
try
if get frontmost of win is true then
do script "cd $PATHDIR; clear" in (selected tab of win)
end if
end try
end repeat
end tell
EOF
Next up was to be able to launch programs from these tabs. For example, I start PostgreSQL in one tab, and Apache in another. I used the script above as a basis for this and created termexec
:
#!/bin/sh -
/usr/bin/osascript > /dev/null <<-EOF
activate application "Terminal"
tell application "Terminal"
repeat with win in windows
try
if get frontmost of win is true then
do script "$*" in (selected tab of win)
end if
end try
end repeat
end tell
EOF
And last but not least was to launch Firefox and have it open the webapp automatically:
open $FIREFOX --args $APP_URL
Putting all these pieces together I created a shell script called devapps
:
#!/bin/sh -
if [ $# -ne 1 ]; then
PATHDIR=`pwd`
else
PATHDIR=$1
fi
# get the directory this script was launched from
bindir=`dirname $0`
# open TextMate
$bindir/termexec "cd $PATHDIR"
$bindir/termexec "mate ."
$bindir/newtab $PATHDIR # for svn, git, etc.
# start Apache
$bindir/newtab $PATHDIR
$bindir/termexec "httpd -k restart -f $PATHDIR/dev_conf/localhost.conf"
# start PostgreSQL
$bindir/newtab $PATHDIR # for postgres
$bindir/termexec "$PATHDIR/bin/postgresql start"
# open Firefox
open $FIREFOX --args $APP_URL
Next, I created a project specific shell script. In this case demodev
:
#!/bin/sh
FIREFOX="/Applications/Johns/Firefox.app"
APP_URL="http://localhost:12001"
export FIREFOX APP_URL
bindir=`dirname $0`
$bindir/devapps "/Users/john/Workspace/demo/jobs"
Almost there, but not quite. I use Alfred App to launch applications. I thought it would be nifty if I could get it to launch my demodev
script. I created a new workflow in Automator and added a Run AppleScript
action, and inserted the following AppleScript:
on run {input, parameters}
do shell script "/Users/john/bin/demodev"
return input
end run
I saved this workflow as an Application (in my case I called it Demo Development) and hey presto I was in business. To make sure this runs each time you login, you could add the app to your
Login Items. Check out the short video below to see me launching the app from Alfred.