Automating MSBuild

From IronPython Cookbook

From an example of invoking MSBuild from IronPython. The discussion of the example relates to XNA, but it can be used anywhere you are building with MSBuild.

The MSBuild Api is part of the .Net Framework, check out theses namespaces:

The Engine class has a simple example of building a project file but unlike the sample you don't have to hard code the path to the .Net runtime, you can use System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() instead.

Here's an IronPython console sessions that shows how easy it can be (I use IronPython to check out .Net libs):

>>> import System
>>> import clr
>>> clr.AddReference("Microsoft.Build.Engine")
>>> from Microsoft.Build.BuildEngine import ConsoleLogger, Engine
>>> e = Engine()
>>> e.BinPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
>>> log = ConsoleLogger()
>>> e.RegisterLogger(log)
>>> e.BuildProjectFile("buildmulti.proj")
Build started 20/12/2006 07:14:13.
__________________________________________________
Project "X:\msbuild\buildmulti.proj" (default targets):

Target XNAContentBuild:
 bin\Debug\cloudyhills.xnb is already up to date

Build succeeded.
 0 Warning(s)
 0 Error(s)

Time Elapsed 00:00:01.01
True
>>>

I would also suggest that you read the MSBuild docs and learn how build projects work - you could move a lot of logic into the project file itself to avoid you having to generate project files for every asset. For example if you decide on a directory layout for your level assets then you could have a single build project that builds any file in your textures directory using a texture processor and any file in your models directory using a model processor and so on.

Once you have a system set up you have some very powerful options open to you; you could build from the level editor or provide shortcuts that just run the build without starting the level editor (for users that are just editing assets) or levels can be built from the command line on a network server, or check for changes to a source control system so that as soon as an artist checks in a change the level data is rebuilt and can then be shared between users. There are many options.


Back to Contents.

TOOLBOX
LANGUAGES