Dec 29, 2009

Fast path switching between projects

I just finished some mods to the script I use when switching from one project to another.  The script makes it really fast and easy to try different versions of things that are wired into the command line path.  For example, switching grails versions is as simple as

  use grails 1.1.1
  grails run-app

Switching back again…

  use grails 1.2.0
  grails run-app

Three things come together to make this work.

First, I keep a directory called devtools full of different versions of tools.  Like this…

      current -> ant-1.7.1
      current -> jdk-1.5.0
      current -> grails-1.1.1

and so on. I keep this in my home directory so it is easy for me to replicate across machines. You might want to keep it in some system specific location; like /usr/local.

Second, in my .profile I set up a bunch of environment variables to point to the current symlinks (for example: <nobr>export GRAILS_HOME=~/devtools/grails/current</nobr>). Also in .profile, my path is configured to include GRAILS_HOME/bin.

And third, the use script (see below) sets the current symbolic links.

With these three things in place I can quickly switch between projects with aliases that call the use script. For example…

  alias iaap="use jdk 1.5.0; use grails 1.1.1; cd ~/opi/clients/mdh/iaap"
  alias buyer="use jdk 1.6.0; use grails 1.2.0; cd ~/opi/clients/buyer"

There have got to be a million ways to do this kind of thing, but with this script I can try out different versions of things really fast. And it is especially useful when I have to go back and pick up an old project for doing a demo, debugging or whatever.

[Note: This works great for me on macosx/linux/unix though I am not sure how well it’ll fly with cygwin; don’t don’t know how well symbolic links work on windows (ln -s.)]

The use script:

# sets a symlink by convention...
# For example,
#   in a directory names ~/devtools/grails with
#     ~/devtools/grails/grails-1.1.2 and
#     ~/devtools/grails/grails-1.2,
#   executing `use 1.2`
#   will create a symlink ~/devtools/grails/current -> ~/devtools/grails/grails-1.2
# Set your path to include DEFAULT_TOOL_DIR/grails/current/bin.  Got it?
USAGE: `basename $0`: must specify version (and optional component name.)n
USAGE: For example: `basename $0` [component] version



if [ $# -eq 2 ]; then
    echo "default_tool_dir = $DEFAULT_TOOL_DIR"
elif [ $# -eq 1 ]; then
    COMPONENT=`basename $PWD`
    echo $USAGE
    exit -1


if [ -d $USE ]; then
    if [ -h current ]; then
        CURRENT=`ls -l current | awk '{ print $11 }'`
        if [ "$CURRENT" = "$USE" ]; then
            echo "keeping $CURRENT"
            echo "switching from $CURRENT to $USE"
            rm current
            ln -s $USE current
            ls -l current | awk '{ printf "%s %s %s", $9, $10, $11 }'
        echo "using $USE"
        ln -s $USE current
        ls -l current | awk '{ printf "%s %s %s", $9, $10, $11 }'
    echo "ERROR: missing directory for $@"
    if [ -h current ]; then
        CURRENT=`ls -l current | awk '{ print $11 }'`
        echo "WARNING: keeping $CURRENT"
        echo "WARNING: current not set."
    echo $USAGE
    exit -1

About the Author

Object Partners profile.

One thought on “Fast path switching between projects

  1. Mike Miller says:

    Any options for Windows users?

  2. amiller says:

    Mike Miller wrote:
    > Any options for Windows users?

    Yes! For the most part I’m on osx or linux these days, but I just tried my use script out on a windows XP box with cygwin and it worked fine. And by “just fine” I mean from within cygwin shells which have no problems with windows environment variables that reference symbolic links. BUT there is a catch: windows programs (like cmd) see symlinks as files even if the link is to a directory.

    Ditching windows environment variables and adopting cygwin as your only shell is one way around this! There is some overhead if you’re not already a unix user, but it is worth it — using the same set of command line tools and configurations across windows/osx/linux can be a huge time saver. (I’ll do another post about a convenient way to see exactly how your shell environment configured.)

    Happy New Year,

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Performance Test Liquibase Update
When doing a liquibase update to a database if you’re having performance issues, it can be hard to find out which updates are causing problems. If you need to measure the time to apply each […]
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]