Subversion script for Rails developers

Posted by Chris Barnes Wed, 19 Sep 2007 15:15:00 GMT

I read somewhere that good developers use version control. There's a good article on HowtoUseRailsWithSubversion.

So why should I repeat those steps for every new project I work on.

There is another article which attempted to tackle this problem. However, you still have to do the initial steps and then copy this script into your project directory and execute it.

The following script takes it a bit farther. You call it like this.

rails-svn app_dir repository username

So if I wanted to create a new rails project called toejam, I would execute:

rails-svn toejam svn://randomutterings.com/projects/toejam/trunk chris

Here's what happens under the hood.

  1. After checking to make sure the directory doesn't already exist, your rails app is created with the standard rails command. (If the directory already exists, you will be asked if you want to continue. Choosing yes will allow the script to continue but if this script has been run on the same app twice, all of the svn commits will be duplicated.)

  2. Your new app is imported to the subversion repository specified. Authentication is done with the username provided. You may be prompted for a password from your repository.

  3. The original app directory is deleted and a working copy is checked out from the repository into that directory.

  4. All the default rails log files are removed and subversion is instructed to ignore those files. (Log files can get large and we definitely don't need them in version control.)

  5. The database.yml is moved to database.example to serve as a template for anyone who checks out the code and any newly created database.yml files are ignored by subversion. (Doing this will help if you have multiple developers working on the same project in different environments. It also prevents you from overwriting the database.yml file on the production server, oops!

  6. Subversion is told to ignore everything in the tmp folder, the .htaccess files, and the dispatch.fcgi file.

Without further ado, here's the script. Just copy it and save it somewhere in your path. /usr/bin is a good choice. Don't forget to chmod 755 so you can execute it.

    #!/bin/bash

    if [ "$#" != "3" ]; then
      echo "Usage: rails-svn app_dir repository username"
      exit 1
    fi

    APPDIR=./$1
    SVN_TRUNK=$2
    SVN_USER=$3

    function check_if_exist () {
      if [[ -e $1 ]]; then
        echo ""
        echo "$1 already exists, overwrite? y or n"
        echo ""
        read OVERWRITE
        case "$OVERWRITE" in
        y)
          echo "Overwriting..."
          ;;
        *)
          echo "Action canceled"
          exit 1
          ;;
        esac
      fi
    }

    check_if_exist ${APPDIR}
    rails $APPDIR
    svn import $APPDIR $SVN_TRUNK -m "Import" --username $SVN_USER
    sudo rm -r $APPDIR
    svn checkout $SVN_TRUNK $APPDIR
    cd $APPDIR

    svn remove log/*
    svn commit -m "removing log files" 
    svn propset svn:ignore "*.log" log/
    svn update log/
    svn commit -m "Ignoring all files in /log/ ending in .log"
    svn move config/database.yml config/database.example
    svn commit -m "Moving database.yml to database.example to provide a template for anyone who checks out the code"
    svn propset svn:ignore "database.yml" config/
    svn update config/
    svn commit -m "Ignoring database.yml"
    svn remove tmp/*
    svn propset svn:ignore "*" tmp/
    svn update tmp/
    svn commit -m "ignore tmp/ content from now" 
    svn propset svn:ignore ".htaccess" public/
    svn update public/
    svn commit -m "Ignoring .htaccess"
    svn propset svn:ignore "dispatch.fcgi" public/
    svn update public/
    svn commit -m "Ignoring dispatch.fcgi"

I'm interested in converting this into a gem and possibly rewriting it in ruby. If anyone has the skills and wants to contribute, leave a comment or email me.

Trackbacks

Use the following link to trackback from your own site:
http://blog.randomutterings.com/articles/trackback/25

Comments - Leave a response

  1. Avatar
    matt about 1 month later:
    I was poking around in my own script that I wrote and noticed two lines that I have and you have as well: svn propset svn:ignore "dispatch.fcgi" config/ svn propset svn:ignore ".htaccess" config/ I deleted my cause I don't see how a htaccess or a dispatch file would find it's way into the config directory. Is there something I'm forgetting here?
  2. Avatar
    Chris Barnes about 1 month later:
    You're right Matt, I changed those lines, they should have been applied to the public directory.
  3. Avatar
    rssnewsdigest 6 months later:
    Try rssnewsdigest.com, a new comprehensive news aggregator. With rssnewsdigest, you don ’t really have to go anywhere else. http://rssnewsdigest.com
  4. Avatar
    face blog over 2 years later:
    Thank you. Your explanation is even clear than a tutorial. It is short and sweet....awesome post.
  5. Avatar
    Frederick over 2 years later:
    Hi have you been improving this code lately? I am curious as to whether some people left comments via email. I am interested if your code has indeed reached the gem stage.

    Free Bondage Videos
  6. Avatar
    Perry Cunningham over 2 years later:
    Have you perfected this particular subversion script directed to Rails developers? I am interested in the progress and transformation of any programming script.

    neck firming cream