Oct 12, 2010

Update: Fast path switching between projects

Yesterday I overheard a couple co-workers talking about this cool script they found for quickly switching between development projects.  Turns out they were talking about my old post so I thought I’d share the latest version of the script.

Back in December I posted the script I use when switching from one project to another (…/2009/12/29/fast-path-switching-between-projects). With this script trying different versions of things that are wired into the command line path is fast and easy.

I’ve updated the use script so now you can pass in more than one
component/version pair. Like this

    use grails 1.3 tomcat 6.0.29 maven 2.2.1

And calling use with no arguments echos all of the *_HOME vars. Like this

    yaal.local[535]> use
        ANT_HOME      /Users/amiller/devtools/ant/current     -> ant-1.8.1
        CATALINA_HOME /Users/amiller/devtools/tomcat/current  -> tomcat-6.0.29
        EMACS_HOME    /Users/amiller/devtools/emacs/current   -> emacs-23.2.app
        GRAILS_HOME   /Users/amiller/devtools/grails/current  -> grails-1.3.4
        GROOVY_HOME   /Users/amiller/devtools/groovy/current  -> groovy-1.7.5
        JAVA_HOME     /Users/amiller/devtools/jdk/current     -> jdk-1.6.0
        TOMCAT_HOME   /Users/amiller/devtools/tomcat/current  -> tomcat-6.0.29

Here’s the modified 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
# You can pass in more than one component/version pair...
#   `use grails 1.3 tomcat 6.0.29 maven 2.2.1`
# Set your path to include ~/devtools/grails/current/bin.  Got it?

#~ functions ----------------------------------------------------------------------------------

setCurrentLink() {
    rm -f current
    ln -s $1 current
    # this will echo the link to the console...
    #ls -l current | awk '{ printf "%s %s %s", $9, $10, $11 }'


    #echo "default_tool_dir = $DEFAULT_TOOL_DIR"

    if [ ! -d $USE ]; then
        # maybe there is a missing .0 ?

    if [ -d $USE ]; then
        if [ -h current ]; then
            CURRENT=`ls -l current | awk '{ print $11 }'`
            if [ "$CURRENT" = "$USE" ]; then
                echo "using $CURRENT"
                echo "switching from $CURRENT to $USE"
                setCurrentLink $USE
            echo "using $USE"
            setCurrentLink $USE
        echo "ERROR: missing directory for $@"
        echo "ERROR: available version are..."
        find . -name $COMPONENT-* -depth 1

        if [ -h current ]; then
            CURRENT=`ls -l current | awk '{ print $11 }'`
            echo "WARNING: keeping $CURRENT"
            echo "WARNING: current not set."
        exit -1

echoCurrentHomeVars() {
    #echo "The current _HOME vars are..."
    for home_var in `printenv | grep _HOME | sort`; do
        home_var_name=`echo $home_var | awk -F= '{ print $1 }'`
        home_var_value=`echo $home_var | awk -F= '{ print $2 }'`
        if [ -h $home_var_value ]; then
        # echo out what symbolic links are pointing to...
            echo "    $home_var_name    `ls -l $home_var_value | awk '{ printf "%s -> %sn", $9, $11 }'`"
            echo "    $home_var_name    $home_var_value"

#~ main body of the script --------------------------------------------------------------------


if [ $# -eq 0 ]; then
    exit -1
elif [ $# -eq 1 ]; then
    echo "WARNING: Assuming you are making a link in the current directory."
    useComponentVersion `basename $PWD` $1
    # use each component/version pair
    while [ $# -gt 0 ]; do
        useComponentVersion $1 $2

#~ END OF FILE --------------------------------------------------------------------------------

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.8.1
            current -> jdk-1.5.0
            current -> grails-1.3.4

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: export GRAILS_HOME=~/devtools/grails/current). 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 grails 1.1.1; cd ~/opi/clients/mdh/iaap"
    alias buyer="use jdk 1.6.0 grails 1.2.0; cd ~/opi/clients/buyer"
    alias mtm="use jdk 1.6.0 grails 1.3.4; cd ~/dev/mtm"

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.

About the Author

Object Partners profile.

One thought on “Update: Fast path switching between projects

Leave a Reply

Your email address will not be published.

Related Blog Posts
Building Better Data Visualization Experiences: Part 1 of 2
Through direct experience with data scientists, business analysts, lab technicians, as well as other UX professionals, I have found that we need a better understanding of the people who will be using our data visualization products in order to build them. Creating a product utilizing data with the goal of providing insight is fundamentally different from a typical user-centric web experience, although traditional UX process methods can help.
Kafka Schema Evolution With Java Spring Boot and Protobuf
In this blog I will be demonstrating Kafka schema evolution with Java, Spring Boot and Protobuf.  This app is for tutorial purposes, so there will be instances where a refactor could happen. I tried to […]
Redis Bitmaps: Storing state in small places
Redis is a popular open source in-memory data store that supports all kinds of abstract data structures. In this post and in an accompanying example Java project, I am going to explore two great use […]
Let’s build a WordPress & Kernel updated AMI with Packer
First, let’s start with What is an AMI? An Amazon Machine Image (AMI) is a master image for the creation of virtual servers in an AWS environment. The machine images are like templates that are configured with […]