Bootstrapping an ASP.NET MVC4 Project for Mono

I got around to setting up a template solution for ASP.NET MVC4 for Mono on github. You can use this to bootstrap new MVC4 Mono projects (using the unfortunately named strapping-young-lad bootstrapper.)

This setup gives you a way to quickly get going with a new MVC4 project that builds and runs with Mono and XSP4. It’s kind of like doing a file new empty MVC project in Visual Studio.

My primary use case so far is so I can quickly answer MVC questions on stackoverflow without having to reboot into Windows, but hopefully other uses will become apparent…

strapping young lad

strapping-young-lad, which I will henceforth refer to as just lad to avoid any further embarrassment, is a project bootstrapper. It’s based on warmuP, which is great but only works on Windows. (I think. To be more accurate — I struggled a little getting it to work with Mono, so I just opted to use lad instead.)

What lad does is:

  • grab a git repository (can be local or somewhere remote like github)
  • replace all instances of certain tokens in that repository with a given replacement

So say I have a file named __NAME__.sln in my repository, and I run

1
$ lad path/to/my/repo FunkyMonkey

I’ll get a new folder with the file FunkyMonkey.sln in it. Token replacement also works inside files.

The first place I personally saw solution generation being used (via warmup in this case) was in Amir Rajan’s cool Oak project. It’s a great idea.

Installing lad

There’s installation instructions at lad’s repo.

On Ubuntu I just did

1
$ gem install lad

A couple of fixes

I had to fix up a couple of things in order to get lad to work properly for me.

First off, in lad.rb, I had to change the command for moving the project files from their temporary location to their new location.

/var/lib/gems/1.9.1/gems/lad-0.1.2/lib/lad.rb
1
2
3
Console.task 'Moving project files' do
    File.rename dir, File.join(Dir.pwd, options[:name])
end

becomes

/var/lib/gems/1.9.1/gems/lad-0.1.2/lib/lad.rb
1
2
3
Console.task 'Moving project files' do
    mv(dir, File.join(Dir.pwd, options[:name]))
end

The File.rename version doesn’t seem to work when moving files across partitions, and as my /home folder is on a separate partition this step was failing.

Second up, the command for replacing the tokens inside files wasn’t working in some cases. I’m not entirely sure why. I noticed it on packages.config and Web.config, which after the token replacement step end up as empty files. These are both xml, which could have something to do with it – but the token replacement worked fine on the csproj file, which is also xml, so I’m not really sure.

Anyway, the following change is made to line 22 of files.rb:

/var/lib/gems/1.9.1/gems/lad-0.1.2/lib/files.rb
1
contents = File.read(file).gsub!(token, name)

becomes

/var/lib/gems/1.9.1/gems/lad-0.1.2/lib/files.rb
1
contents = File.read(file).gsub(token, name)

You can make these changes directly to the gem’s files, in /var/lib/gems/1.9.1/gems/lad-0.1.2/lib.

I’m going to put in a pull request to the lad project for these changes, so hopefully they’ll be incorporated soon and you won’t need to make these manual changes.

Running lad

So once lad is fixed, we can run it and pull down our solution template and switch all instances of __NAME__ to our new project name.

The template project that I set up is located at:

https://github.com/ngm/mono-mvc-template

This template has the various bits and pieces needed to get the NuGet restore to work, as described in my previous post, and a suitably configured packages.config.

As per that post, for this template to work you’ll need to build Mono 3.0.10 and xsp4, and you’ll still need to do the export of EnableNuGetPackageRestore.

Ok, so the command to run lad is:

1
$ lad https://github.com/ngm/mono-mvc-template HelloMvcWorld

This will set up a new directory with a solution named HelloMvcWorld and an MVC4 project named HelloMvcWorld.

You can have multiple tokens for lad, so I might look at allowing the solution name to be different from the project name, but I don’t think that’s really a big deal for now.

MVC ahoy

To get it all fired up, switch to the new solution, run xbuild, switch to the MVC project folder, and run xsp4.

1
2
3
4
$ cd HelloMvcWorld
$ xbuild
$ cd HelloMvcWorld
$ xsp4

Browse to localhost:8080 and you’ll see your brand new MVC4 app! Flip yeah! It’s pretty bare bones at the moment, but it’s a handy starting point for mucking about. I’m thinking of making one that’s a bit more fancy, for example pulling in the nuget packages for twitter bootstrap etc. But this does the job for now.

Comments