Using rake under IronRuby


Download and install IronRuby

Go to http://ironruby.codeplex.com/ and click on the Downloads tab. Grab the .zip file that is appropriate for your platform. There is also a Windows Installer, but I don’t know what it does, so I stick with the zip file. Extract it to a folder on your computer – I recommend c:ironruby. Add c:ironrubybin to your path (if you already have a version of ruby installed, I recommend putting IronRuby in the path AFTER your existing ruby).

Download and install Rake

This is easy, as rubygems comes along with IronRuby. Open a command prompt and type:

igem install rake

In your c:ironrubybin folder, there are two new files: rake.bat and rake. I made copies of those files and named them irake.bat and irake, respectively. This is completely unnecessary, but helpful if you already have an existing ruby installed. irake will run a script with IronRuby, and rake will run a script with your existing ruby.

To test it, create a new file named rakefile.rb in a temporary folder. Add the following contents:

task :default do
  puts 'hello world'
end

Open a command prompt to the folder containing that file, and type irake. After an unfortunate pause, you should see the “”hello world” message. You now have a working IronRuby installation that can run rake scripts. If you have another version of ruby installed (and in your path), you can execute the same script by typing rake. You should see that it also (though much more quickly) puts the greeting message on screen.

Now for something interesting

If you ran the last demo under both versions of ruby, you witnessed the downside of using rake under IronRuby– the slow startup time. However, IronRuby does have one nice big advantage for .NET developers: the ability to easily call your .NET code from the script.

For example, I recently built a simple website using a Web Application Project, which compiles to a DLL. I wanted to be able to build out the database schema using my Fluent NHibernate configuration. I created a static BuildSchema method that does just that:</p>

   1: public class Database 

   2: {

   3:     public static void BuildSchema()

   4:     {

   5:         new SchemaExport(GetConfiguration()).Create(false, true);

   6:     }

   7:  

   8:     public static Configuration GetConfiguration()

   9:     {

  10:         return Fluently.Configure()

  11:             .Database(MsSqlConfiguration.MsSql2008.ConnectionString(x =>

  12:             {

  13:                 x.Database("mydatabse");

  14:                 x.Server(".");

  15:                 x.TrustedConnection();

  16:             }))

  17:             .Mappings(map => map.AutoMappings.Add(

  18:             AutoMap

  19:             .AssemblyOf<Database>(t => typeof(Entity).IsAssignableFrom(t))

  20:             ))

  21:  

  22:         .BuildConfiguration();

  23:     }

  24: }

</div> </div>

I cannot easily execute the DLL from the command-line (to rebuild the database as part of a build script). Normally I would have to create a separate Console application project, which calls BuildSchema from its Main method. However, with IronRuby, I can invoke this method with the following code:

   1: desc "Generate the database (requires IronRuby)"

   2: task :db do

   3:   $:.unshift('.srcMyAppbin')

   4:   require 'MyApp'

   5:   MyApp::Database.BuildSchema

   6:   puts 'Database rebuilt'

   7: end

</div> </div>

Most of this is standard rake code. The interesting bits are in:

  • Line 3 – Extend $: (the array of paths that ruby searches for files to load) to include the location of my binary. Otherwise, IronRuby will not be able to find any of the assemblies my assembly depends on.
  • Line 4 – Load the libary built from the web application project (MyApp.dll)
  • Line 5 – Invoke the static Database.BuildSchema method

With IronRuby and Rake, it is trivial to create build script tasks that make use of your existing .NET code. No need to create special task libraries, as you would with NAnt or MSBuild.

Austin Code Camp 2010