Sunday, December 23, 2012

Running a MonoMac application with a custom Mono Runtime

One situation that sometimes comes up during development is running a MonoMac application on a custom Mono Runtime.

When I did my last batch of improvements to MonoDevelop's "Add Web Reference", I had to do this a lot. Since I'm currently working on a few improvements in that area, the problem came up again, so I decided to share the magic that I'm using with you.

A MonoMac application always uses the runtime that's currently installed in /Library/Frameworks/Mono.framework/Versions/Current - and this path is hard-coded in the launcher script that it's using.

Well, I really didn't feel like manually compiling gtk-sharp and all that stuff just to quickly test something - and building and installing a new .dmg each time I changed one line of code would also be very tedious task.  All I really need to replace is
System.ServiceModel.dll, but just copying the dll for testing over the installed packages would be really ugly.

One great thing about Apple's .app's is that it bundles everything in one place, so you can easily make a backup copy of your 
/Applications/MonoDevelop.app or copy it to some other place.  So after backing it up, copied the newly compiled Add-In over /Contents/MacOS/lib/monodevelop/AddIns/MonoDevelop.WebReferences/.

That was the easy part, now I just had to make the installed /Applications/MonoDevelop.app with a different Mono runtime, which contains my System.ServiceModel.dll changes - and I didn't feel like messing without anything outside that Add-In directory.

So I took the launcher scrip from monodevelop/main/build/MacOSX/monostub.m and added some command-line arguments to run it outside the .app bundle and use an arbitrary Mono runtime.  I also setup the environment variables in a way that it still loads stuff like gtk-sharp from the installed Mono, so you don't have to manually compile all that stuff.

I put the result of this little magic into my workspace module on github: baulig/Provcon-Faust/tools/monomac-launcher.m.

You can run it like this:

monomac-launcher --mono=/Workspace/INSTALL --app=/Applications/MonoDevelop.app --exe=/Applications/MonoDevelop.app/Contents/MacOS/lib/monodevelop/bin/MonoDevelop.exe