New Rails Plugin: App Version
Mar 31st, 2007 by phil
A while back I began refactoring reusable pieces of code in my Rails applications into plugins. Overall this has been a successful strategy for keeping my application codebase clean. I have decided to release some of these plugins as open source in the hope that they are useful to someone else. The first plugin I will be releasing is called App Version.
This is a simple plugin that makes it easy to manage the version number of your Rails application. The version numbers supported by this plugin look like ‘2.0.1 M4 (600)’. The components of the version number are:
- 2 => major
- 0 => minor
- 1 => patch
- M4 => milestone
- (600) => build number (usually Subversion revision)
Only the major and minor numbers are required. The rest can be omitted and the plugin will attempt to do the right thing.
I use this in two of my current Rails applications. In one, we have a convention that version numbers look like ‘2007.3 M4’ where 2007 is the year, the 3 is a release number and M4 indicates this is the fourth milestone release. Generally, I bump the milestone number every time I deploy to our testing environment and then bump the release number when we deploy to production. The other application uses more traditional version numbers that look like ‘1.0.0 (600)’ where the 600 is the Subversion revision number. Both types of version numbers are supported by this plugin.
I have setup a Trac instance for this and any other plugins I release at http://dev.fiatdev.com/plugins.
Update April 10, 2008: The code is now hosted on GitHub. See this post for more information.
Installation
Installation is the same as any other Rails plugin. From your application directory type
$ script/plugin install http://dev.fiatdev.com/svn/plugins/app_version
Usage
To use, simply place a file in RAILS_ROOT/config called version.yml with the following format:
major: 2
minor: 0
patch: 1
milestone: 4
build: svn
If the milestone or patch fields are less than 0 then they will not show up in the version string. The build field can be a build number or the string svn. If it is a number then that number will be used as the build number, if it is the string ‘svn’ then the plugin will attempt to query Subversion for the build number.
The plugin creates a constant APP_VERSION that contains the version number of the application. Calling the #to_s method on APP_VERSION will result in a properly formatted version number. APP_VERSION also has #major, #minor, #patch, #milestone and #build methods to retrieve the individual components of the version number.
Finally, there is a rake task for displaying the version number from the command line. rake app:version will output the version number of the current rails application.
Update April 1, 2007: Esad Hajdarevic wrote to me to point out that svn info on the RAILS_ROOT directory will miss changes committed to subdirectories. This generally isn’t a problem once your app is deployed with Capistrano, but during development the revision numbers may be wrong. I have updated the plugin to use svnversion instead. This should fix the problem.


