Thursday, April 03, 2008

Rake gotcha on Windows

We use Rake as our build system of choice for all of our projects, as it's much more flexible and pleasant to work with than make or ANT, and by and large it's pretty cross-platform. Except.....

I just spent about two hours tearing my hair out over something which should have worked transparently on Windows aswell as *NIX, but it just wasn't playing.

For reasons which would be tedious to go into, inside one particular rake task I needed to cd to a different directory and execute rake in there, to pick up a completely different set of tasks and ActiveRecord model classes, etc, and then resume execution of the containing task.

The following code worked fine on UNIX, but just completely failed to do anything on Windows:

Dir.chdir("../sonar-web") { system( "rake", "db:name_of_other_task" ) }


No error messages, nothing, it just wasn't doing anything.

I'll spare you the headbanging frustration and exhaustive list of things I tried that didn't work, and jump straight to the solution -

rake on Windows needs to execute c:/ruby/bin/rake.bat, not c:/ruby/bin/rake !

rake.bat will in turn call ruby.exe c:/ruby/bin/rake and pass on the command line parameters.

So at the top of my rakefile, I just added:

# on Windows, you can't invoke rake via a "system" cmd, as rake actually should invoke rake.bat
RAKE_CMD = RUBY_PLATFORM.match(/win/) ? "rake.bat" : "rake"


and then changed my system call to :

system( "#{RAKE_CMD}", "name_of_other_task" )

....and everything worked fine.

9 comments:

Martin said...

Yo! Simple question: would you recommend Rake as an alternative to ant for building non ruby apps (by which I mean CF)? I like ant but it sometimes does my head in.

Do you know if you can integrate it with Eclipse?

julien said...

Thanks a lot, very handy tip

Alistair Davidson said...

Martin: Now then, now then!

You can indeed run Rake from Eclipse, I haven't done it myself but here's a guide to getting it started

As for using rake for CF projects, I can't think of any reason why you wouldn't. We use it to build our Java stuff (with our home-grown JERBIL to simplify Java compilation and execution)

Rake gives you the full expressive power of Ruby, and means you can cut down the bloated XML of an ANT file tremendously. It also means you can do things like if-then-else just like that, without jumping through the icky minging semantic workaround hoops that ANT sometimes puts you through.

Martin said...

Thanks. I'll give it a try. The if-then-else alone would make me very happy!

Joe said...

RUBY_PLATFORM =~ /(:?mswin|mingw)/
actually more correct
(macOSX = darwin) ;)

Emeka said...

Great post, help saved me from sleepless nights

Anonymous said...

An easier way to do this is to go to c:\ruby\bin and copy one of the bat files (I copied gem.bat), edit the file to say 'rake' instead of 'gem' and then you need to copy the rake.rb file (c:\ruby\bin\rake.rb) to be instead just rake (c:\ruby\bin\rake) and then you don't have to modify any of your code anyway since now 'rake' at the command line will work.

Rebeca said...

I am listening first time about Rake. Can you tell me something about rake technology and use of this.

Namescape said...

Sorry to comment on such an old post, but I am confused. Do I insert your comments and code line at the top of the rakefile that is in my application root folder (C:\Sites\ffcrm\rakefile) or is there another rakefile I need to edit with your comments and code. Then do I enter in the system("#{RAKE_CMD}", "name_of_other_task") in the cmd prompt? I am trying to run rake db:migrate:plugins, but it keeps giving me the message: "The syntax of the command is incorrect."

I am new to all of this, so I am struggling to figure out how to run rake commands in Windows.