December 18, 2006

Simplifying Edge Rails Setup
Post by Robert Evans

This entry is by Robert Evans, a fulltime Ruby/Rails developer. You can check out what he is doing over on his blog - http://robertrevans.com. He has several exciting projects that will be released within the next 2 months, so go check them out.

I am a huge fan of edge Rails and use it for every project that I work on. My only complaint about this great framework, is that I constantly have to do the same things over and over to setup my application. It can take a decent amount of time to create your directory, checkout edge Rails via subversion, run rails myproject using edge Rails, and then download all the wonderful plugins. Seriously, you could have a blog written in the amount of time that it takes to get everything setup and ready to go. So, we need to DRY up this process.

I’ve written a very simple shell script that does all the work for you. It’ll create a directory, download edge rails, create the edge project, setup the database.yml file, and get all the plugins I would normally use. And what do we need to do? Just type ./set_rails myprojectname postgres at the prompt and sit back and let it do the work for you.

Let’s take a walk through this script and see what it is actually doing and talk about some ideas that you can do to personalize it for you.


#!/bin/sh
clear
echo "Creating Rails Folder..."
mkdir -p $1/vendor
cd $1

echo "Downloading Edge Rails..."
svn export http://dev.rubyonrails.org/svn/rails/trunk vendor/rails
echo "Setting up Edge Rails Project..."
ruby vendor/rails/railties/bin/rails .

This is the first part of our shell script. As you can see it clears the prompt, gives you a message that it is creating a folder for you. It takes the name of that folder you passed when you typed *myprojectname* into the prompt as the first argument of the script call. That first argument is stored in $1 of our script and the second argument is stored in $2.

Next, we grab the latest edge Rails from the svn server and run the command to setup our rails application. You may be more used to seeing/doing rails myprojectname, but for edge Rails, we run ruby vendor/rails/railties/bin/rails . to create our project on edge rails.


echo "I'm going to remove the README file and add a CHANGELOG file..."
rm README
touch CHANGELOG
echo "Removing the public/index.html file..."
rm public/index.html
echo "Creating the config/database.yml.sample file..."
touch config/database.yml.sample
echo "login: &login
  adapter: $2
  host: localhost
  username: 
  password: 

development:
  database: $1_dev
  <<: *login 

test:
  database: $1_test
  <<: *login 

production:
  database: $1_production
  <<: *login" > config/database.yml.sample

echo "Editing the database.yml file..."
cp config/database.yml.sample config/database.yml
echo "Creating the app/views/layout/application.rhtml file..."
touch app/views/layouts/application.rhtml
echo "Creating the public/stylesheets/screen.css file..."
touch public/stylesheets/screen.css

This snippet does a few things: removes some files, creates a sample YAML file for our db to be checked into Subversion and also adds our database information to that YAML file and then copies the sample YAML file contents over to the database.yml file.

The database YAML file takes the second argument you passed via the script call, postgres, and uses it as the adapter value telling our application that we are using that specific database program for this application. So you could pass postgres, mysql, sqlite or whatever database you are using. Also, the first argument is used to setup the names of our development, test and production databases.

Here is an instance where you could add your username and password for your development database and not worry about having to open that file again to set it. We then create an application.rhtml file within our app/views/layouts directory and a stylesheet file called screen.css. I also never use the README file that is installed by default, so I remove that and add the CHANGELOG file. This has been a great to keep a text based account of what you have completed and/or need to complete for this specific project. Works well within teams.


clear
echo "Adding an assortment of plugins..."

./script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/
./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_versioned/
./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid/
./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_attachment/
............

This is where a lot of time can be taken up: installing plugins. Nothing fancy here, just some plugins being installed.

RaPT, being worked on by Geoffrey Grosenbach, has the nice addition of plugin packs, thanks to Luke Redpath, which would be a nice replacement here in the script. In the future, I’ll be changing the installation of plugins over to a RaPT plugin pack so that it is generic enough to install whatever plugins you have in your own pack.

There is one last snippet of code, but it is just letting you know your edge Rails application has been made and sends you off with a ‘Happy Programming!’.

You can download this script off my website, here as set_rails.zip

Happy Programming!

Robert Evans
http://robertrevans.com