Skip to content

Instantly share code, notes, and snippets.

@sbleon
Last active May 7, 2019 14:41
Show Gist options
  • Save sbleon/3439856 to your computer and use it in GitHub Desktop.
Save sbleon/3439856 to your computer and use it in GitHub Desktop.
Start working on a Rails project
#!/bin/sh
#
# Open a new Mac OS X terminal window or tab in the current or another
# directory and optionally run a command in the new window or tab.
#
# - Without any arguments, the new terminal window opens in
# the current directory, i.e. the executed command is "cd $PWD".
# - If the first argument is a directory, the new terminal will "cd" into
# that directory before executing the remaining arguments as command.
# - The optional "-t" flag executes the command in a new tab
# instead of a new window.
# - The optional "-x" flag closes the new window or tab
# after the executed command finishes.
# - The optional "-p" flag takes an argument of the form x,y (e.g. 40,50) and
# positions the terminal window to the indicated location on the screen
# - The optional "-s" flag takes an argument of the form w,h (e.g. 800,400) and
# resizes the terminal window to the indicated width and height in pixels.
#
# Written by Marc Liyanage <http://www.entropy.ch>
#
# Version 2.1
#
set -e
while getopts xtrp:s: OPTION; do
[ $OPTION = "x" ] && { EXIT='; exit'; }
[ $OPTION = "t" ] && { TAB=1; }
[ $OPTION = "p" ] && { POSITION="set position of window 1 to {$OPTARG}"; }
[ $OPTION = "s" ] && { SIZE="set size of window 1 to {$OPTARG}"; }
[ $OPTION = "r" ] && { RETURN=1; }
done
for (( $OPTIND; $OPTIND-1; OPTIND=$OPTIND-1 )); do shift; done
if [[ -d "$1" ]]; then WD=$(cd "$1"; pwd); shift; else WD=$PWD; fi
COMMAND="cd '$WD' && echo -n \$'\\\\ec';"
for i in "$@"; do
COMMAND="$COMMAND '$i'"
done
if [ $TAB ]; then
osascript 2>/dev/null <<EOF
tell application "System Events"
tell process "Terminal" to keystroke "t" using command down
end
tell application "Terminal"
activate
do script with command "$COMMAND $EXIT" in window 1
$POSITION
$SIZE
end tell
EOF
if [ $RETURN ]; then
osascript 2>/dev/null <<EOF
tell application "Terminal"
activate
tell application "System Events"
keystroke (ASCII character 28) using {command down, option down}
end tell
end tell
EOF
fi
else
osascript <<EOF
tell application "Terminal"
activate
do script with command "$COMMAND $EXIT"
$POSITION
$SIZE
end tell
EOF
fi
focus_terminal_window() {
local search_string=$1
local focus_script="
tell application \"Terminal\"
repeat with w in windows
if custom title of w contains \"$search_string\" then
set frontmost of w to true
end if
end repeat
end tell
"
osascript -e "$focus_script"
}
move_front_window() {
local app=$1
local location=$2
osascript -e "
tell application \"$app\" to activate
tell application \"System Events\"
tell application \"SizeUp\" to do action $location
end tell
"
}
zoom() {
local app=$1
local zoom_applescript="tell application \"$app\" to activate
tell application \"System Events\"
tell process \"$app\"
tell menu bar 1
tell menu \"Window\"
click menu item \"Zoom\"
end tell
end tell
end tell
end tell"
osascript -e "$zoom_applescript"
}
#!/bin/bash
#
# work.sh
# Leon Miller-Out - leon@singlebrook.com
#
# Fires up everything I need to work on a web app.
#
# SETUP
# - put Pivotal Tracker project ID in .pivotal_tracker_id
#
# DEPENDENCIES
# - OS X
# - RubyMine
# - IntelliJ IDEA
# - SizeUp
# - Sublime Text
# - bundle command in your path
# - git command in your path
# - term script (included in distribution)
#
# TODO
# * Project-specific TCP ports
# * Use user's default browser instead of Chrome. It's hard to figure out what the
# default browser is on OS X. We have the workaround of setting BROWSER in the
# env, but it doesn't work properly with Firefox.
set -e
# Detect paths and set up variables for various commands
CMD_BIN=`readlink $0`
CMD_PATH=`dirname $CMD_BIN`
TERM=$CMD_PATH/term
source $CMD_PATH/window_funcs.inc
if [ "$BROWSER" == "" ]; then
BROWSER="Google Chrome"
fi
if [ -f Gemfile ]; then
echo "Checking gem dependencies"
bundle check
if [ $? -ne 0 ]; then
bundle
fi
fi
if [ "$CODE_EDITOR" == "" ]; then
if [ `find . -name "Application.cf*" | head -n 1 | wc -l` -eq 1 ]; then
CODE_EDITOR="IntelliJ IDEA"
else
CODE_EDITOR="RubyMine"
fi
fi
# Find an available port
PORT=3000
while [[ PORT -lt 3010 ]]; do
PORT_IN_USE=`netstat -an | grep LISTEN | grep $PORT | wc -l`
if [[ PORT_IN_USE -eq 0 ]]; then
# Port is available
break
fi
PORT=`expr $PORT + 1`
done
if [[ $PORT -eq 3010 ]]; then
echo "No ports between 3000 and 3010 available"
exit 1
fi
# Start server and other processes
if [ -f bin/start ]; then
echo "Running bin/start"
SERVER_CMD="env PORT=$PORT bin/start"
elif [ -f Procfile.dev ]; then
echo "Starting Foreman"
SERVER_CMD="env PORT=$PORT foreman start -f Procfile.dev"
elif [ -f Procfile ]; then
echo "Starting Foreman"
SERVER_CMD="env PORT=$PORT foreman start"
elif [ -f Vagrantfile ]; then
echo "Starting Vagrant"
CODE_EDITOR="Sublime Text"
SERVER_CMD="env PORT=$PORT vagrant up"
elif [ -f bin/rails ]; then
echo "Starting Rails"
SERVER_CMD="bin/rails server -p $PORT -b 127.0.0.1"
fi
$TERM -r -t $SERVER_CMD > /dev/null
# GIT WINDOW POSITION
move_front_window "Terminal" "Upper Right"
if [ -f Guardfile ]; then
echo "Starting Guard"
$TERM bundle exec guard -c > /dev/null
move_front_window "Terminal" "Lower Left"
fi
echo "Opening project in $CODE_EDITOR"
if [ -d .idea ]; then
if [ "$CODE_EDITOR" == "RubyMine" ]; then
mine .
else
idea .
fi
elif [ -f *.sublime-project ]; then
subl -n *.sublime-project
else
subl -n .
fi
sleep 1
move_front_window "$CODE_EDITOR" "Lower Right"
# Make sure we've got a browser window on the current desktop
osascript -e "tell application \"$BROWSER\" to make new window" > /dev/null
if [[ -f .pivotal_tracker_id ]]; then
echo "Opening Tracker project"
TRACKER_ID=`cat .pivotal_tracker_id`
open https://www.pivotaltracker.com/projects/$TRACKER_ID
fi
GITHUB_REPO=`git remote -v | grep github.com | head -n 1 | sed -E -e 's/^.*://' -e 's:(\.git)? .*$::'`
if [[ "$GITHUB_REPO" != "" ]]; then
echo "Opening Github repo"
open https://github.com/$GITHUB_REPO
fi
osascript -e "tell application \"Terminal\" to activate"
echo "Waiting for local site to come up..."
# Open the local site once it's up and running
SITE_AVAILABLE=0
while [[ SITE_AVAILABLE -eq 0 ]]; do
SITE_AVAILABLE=`netstat -an | grep LISTEN | grep $PORT | wc -l`
sleep 1
done
echo "Opening local site"
PROJECT_FOLDER=`pwd | sed 's:.*/::' | sed 's/_//g'`
open http://$PROJECT_FOLDER.lvh.me:$PORT
@sbleon
Copy link
Author

sbleon commented Sep 21, 2012

Just updated:

  • now works with Rails 2.x projects
  • uses $EDITOR instead of hard-coded Textmate
  • You can now use a different browser by setting $BROWSER, but it doesn't work well with Firefox thus far.

@sbleon
Copy link
Author

sbleon commented Dec 26, 2012

New features:

  • Finds an open port between 3000 and 3010 on which to run Rails server
  • Opens local site in browser

I've also added the term helper script, with an addition to return to the original tab after opening a new one.

@sbleon
Copy link
Author

sbleon commented Oct 24, 2013

Now with Foreman support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment