A rake task for setting up new Rails projects for Subversion
Jan 31 ’08
One of the more annoying things about using Subversion for source code control when using rails is that every time you start a new project, you have to go through this dance to get the new project's Subversion setup done. Really, this means that you're repeating yourself every time you start a new project, so in the interest of DRYness, it seems like you would want to script this. Even better, you could just setup one canonical project that you checkout instead of running rails my_new_project.
I've found several pages on the internets that discuss how to do a good subversion setup, and they each had their strong points. I've combined their approaches into a method for making a canonical blank app.
Creating the canonical app
First, you should create a repository for your canonical blank app. Let's say it's at http://svn.my_svn.com/canonical/trunk. I trust that you know how to do this. Then, you can create your app:
rails canonical cd canonical
Next you can do an in-place import of your SVN repo into your app directory:
svn checkout http://svn.my_svn.com/canonical/trunk .
Don't forget the final period in that command; it's critical. SVN should say something like Checked out revision 1. This step puts our newly created canonical directory under Subversion control.
Adding all of your files to the repository
Now we put all of our files under Subversion control:
svn add --force .
Again, don't forget the trailing period. This command will output a long list of files with a leading "A", indicating that they've been added.
Create the rake task
Put the following code in a file called svn.rake in the lib/tasks folder:
desc "Configure Subversion for Rails" task :setup_svn do puts "Removing /log" system "svn remove log/*" system "svn commit -m 'removing all log files from subversion'" system 'svn propset svn:ignore "*.log" log/' system "svn update log/" system "svn commit -m 'Ignoring all files in /log/ ending in .log'"
puts "Ignoring /db"
system 'svn propset svn:ignore "*.db" db/'
system "svn update db/"
system "svn commit -m 'Ignoring all files in /db/ ending in .db'"
puts "Renaming database.yml database.example"
system "svn move config/database.yml config/database.example"
system "svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'"
system 'svn propset svn:ignore "database.yml" config/'
system "svn update config/"
system "svn commit -m 'Ignoring database.yml'"
puts "Ignoring /tmp"
system 'svn propset svn:ignore "*" tmp/'
system "svn update tmp/"
system "svn commit -m 'Ignoring all files in /tmp/'"
puts "Ignoring /doc"
system 'svn propset svn:ignore "*" doc/'
system "svn update doc/"
system "svn commit -m 'Ignoring all files in /doc/'"
end
Do any other setup
If there are any other setup or configuration bits that you use all the time — plugins, ActionMailer configs, date formats, etc., put those in the appropriate places. When using script/plugin make sure that you use the -x flag to get the Subversion stuff right. When using script/generate be sure to use the --svn flag to add the files automatically to Subversion.
For example, at my work most of our apps routinely connect to multiple databases, so I put stubs for each of them in database.example. I also like to have certain date formats available to me,
Run the rake task
Now, execute the new rake task:
rake setup_svn
Be warned, though, you should only run this once on the canonical: it moves the database.yml file to database.example, making the project non-functional. You don't want any passwords in there, either, since you don't want to expose them in the repository.
The script does a bunch of commits, so any changes you have made to your canonical will be saved to the repository.
Export new projects
The next time you want to create a new Rails project, instead of doing rails projectname you should do the following:
-
Export the canonical from the repository
Do an in-place import from the new project's repository
Rename database.example as database.yml and populate it
Run rake secret and copy the new secret code into your environment.rb (you don't want all of your apps to have the same code)
If you don't want to mess with the canonical, you can just drop the svn.rake file in to your new projects and run it. That would give you some DRYness, like a good hitchhiker's towel.
That's it!
Now you should have a good canonical app that makes creating new projects a breeze. At my work we have multiple developers and we will have dozens of apps by the time we're done migrating ourselves out of the old platform. By standardizing our new projects this way, it makes our startup time much faster and leads to better habits (like always using Subversion), and there is no bad in any of that.
Conversation in progress…
Join the conversation
* indicates a required field









On October 7th Stewart Matheson said:
Hey nice article. On thing I noticed is that your using an underscore in the rake task name. Have a look in to creating a namespace for the task. So you might want to do something like "rake svn:setup".
Just a suggestion...