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. Required fields are marked *

Related Blog Posts
AWS Cloud HSM, Docker and NGINX
There is quite a bit of easily searchable content on the security benefits of leveraging a Hardware Security Module to manage cryptographic keys, so I will leave that to the scope of another article. The […]
Google Professional Machine Learning Engineer Exam 2021
Exam Description A Professional Machine Learning Engineer designs, builds, and productionizes ML models to solve business challenges using Google Cloud technologies and knowledge of proven ML models and techniques. The ML Engineer is proficient in all aspects […]
Designing Kubernetes Controllers
There has been some excellent online discussion lately around Kubernetes controllers, highlighted by an excellent Speakerdeck presentation assembled by Tim Hockin. What I’d like to do in this post is explore some of the implications […]
React Server Components
The React Team recently announced new work they are doing on React Server Components, a new way of rendering React components. The goal is to create smaller bundle sizes, speed up render time, and prevent […]