I make tools to help people create wonderful things.


Easy pull requests from master with git-offload

When using git, I often find myself committing some work to master locally, and having to move it all to a branch from which I can make a pull request. Then I have to clean up afterwards.

I do this often, yet it's tedious and I still sometimes have to look up the exact format of the git commands. So, I wrote a script to automate it, git-offload.

Simply pass in the new branch name and the remote to you want to push the new branch, and git-offload automatically does the following things:

  • Creates a new branch from your current commit
  • Pushes that branch to an upstream of your choice
  • Goes back to the original branch
  • Resets the original branch to its upstream state

You can download git-offload from Gist.

MSBuild Code Generation in VS2015

A couple of weeks ago I was helping Jason Smith debug an issue with the build-time code generation in Xamarin.Forms. The build targets were based on my Build Time Code Generation in MSBuild post, but in recent Visual Studio 2015 versions the generated classes would occasionally disappear from IntelliSense.

Eventually we figured out that this happened after a very specific sequence of circumstances: when the project was cleaned, then closed and reopened.

I verified that the CoreCompile target is still run on each project when first opening a solution in Visual Studio. However, it wasn't getting run when the solution was closed and reopened unless the project file had changed, or the SUO files were deleted. All of this pointed to Visual Studio caching the Csc inputs in the solution options in order to improve solution load performance by eliminating the need to run CoreCompile.

This is a fine optimization, except for one minor detail. The cache was not getting flushed when the project was cleaned. This is a HUGE problem when the CoreCompile target creates input files for Csc, and the Clean targets correctly cleans them up. If the solution is closed and reopened after a clean, and no build has taken place since the clean, the input files will be missing!

For now, a reasonable workaround is simply not to incrementally clean generated files, and I've updated my original post with this. As long as you regenerate and collect the generated files correctly, skipping cleaning them shouldn't cause any problems except wasting a tiny bit of disk space.

Some Changes Around Here

I’d like to make a brief announcement. I’m coming out as transgender - I identify as a woman, I am changing my name to Mikayla, and I am switching to she/her/hers pronouns. Going forwards, I would like to ask people to use my new name and female pronouns, and avoid using my old name and male pronouns unless unavoidable.

I realize this may see like an abrupt change, but I’m still the same person, and it shouldn’t really matter to other people. For those of you who want to know more, I’ve posted details in the form of a list of questions and answers.

Build Time Code Generation in MSBuild

Build-time code generation is a really powerful way to automate repetitive parts of your code. It can save time, reduce frustration, and eliminate a source of copy/paste bugs.

This is something I'm familiar with due to my past work on MonoDevelop's tooling for ASP.NET, T4 and Moonlight, and designing and/or implementing similar systems for Xamarin.iOS and Xamarin.Android. However, I haven't seen any good documentation on it, so I decided to write an article to outline the basics.

This isn't just something for custom project types, it's also something that you can include in NuGets, since they can include MSBuild logic.


The basic idea is to generate C# code from other files in the project, and include it in the build. This can be to generate helpers, for example CodeBehind for views (ASPX, XAML), or to process simple DSLs (T4), or any other purpose you can imagine.

MSBuild makes this pretty easy. You can simply hook a custom target before the Compile target, and have it emit a Compile item based on whatever input items you want. For the purposes of this guide I'm going to assume you're comfortable with enough MSBuild to understand that - if you're not, the MSDN docs are pretty good for the basics.

The challenge is to include the generated C# in code completion, and update it automatically.

An IDE plugin can do this fairly easily - see for example the Generator mechanism used by T4, and the *.designer.cs file generated by the old Windows Forms and ASP.NET designers. However, doing it this way has several downsides, for example you have to check their output into source control, and they won't update if you edit files outside the IDE. Build-time generation, as used for XAML, is a better option in most cases.

This article describes how to implement the same model used by WPF/Silverlight/Xamarin.Forms XAML.

Generating the Code

First, you need a build target that updates the generated files, emits them into the intermediate output directory, and injects them to the Compile ItemGroup. For the purposes of this article I'll call it UpdateGeneratedFiles and assume that it's processing ResourceFile items and emitting a file called GeneratedCode.g.cs. In a real implementation, you should use unique names won't conflict with other targets, items and files.

For example:

<Target Name="UpdateGeneratedFiles"
  Condition=="'@(ResourceFile)' != ''"
    <Compile Include="$(IntermediateOutputDir)GeneratedFile.g.cs" />
    <!-- see https://mhut.ch/journal/2016/04/19/msbuild_code_generation_vs2015 
    <FileWrites Include="$(IntermediateOutputDir)GeneratedFile.g.cs" />
<Target Name="_UpdateGeneratedFiles"

A quick breakdown:

The UpdateGeneratedFiles target runs if you have any ResourceFile items. It injects the generated file into the build as a Compile item, and also injects a FileWrites item so the file is recorded for incremental clean. It depends on the 'real' generation target, _UpdateGeneratedFiles, so that the file is generated before the UpdateGeneratedFiles target runs.

The _UpdateGeneratedFiles target has Inputs and Outputs set, so that it is incremental. The target will be skipped if the output file exists is newer than all of the input files - the project file and the resource files.

The project file is included in the inputs list because its write time will change if the list of resource files changes.

The _UpdateGeneratedFiles target simply runs a tasks that generates the output file from the input files.

Note that the generated file has the suffix .g.cs. This is the convention for built-time generated files. The .designer.cs suffix is used for user-visible files generated at design-time by the designer.

Hooking into the Build

The UpdateGeneratedFiles target is added to the dependencies of the CoreCompile target by prepending it to the CoreCompileDependsOn property.


This means that whenever the the project is compiled, the generated file is generated or updated if necessary, and the injected Compile item is injected before the compiler is called, so is passed to the compiler - though it never exists in the project file itself.

Live Update on Project Change

So how do the types from the generated file show up in code completion before the project has been compiled? This takes advantage of the way that Visual Studio initializes its in-process compiler that's used for code completion.

When the project is loaded in Visual Studio, or when the project file is changed, Visual Studio runs the CoreCompile target. It intercepts the call to the compiler via a host hook in the the MSBuild Csc task and uses the file list and arguments to initialize the in-process compiler.

Because UpdateGeneratedFiles is a dependency of CoreCompile, this means that the generated file is updated before the code completion system is initialized, and the injected file is passed to the code completion system.

Note that the UpdateGeneratedFiles target has to be fast, or it will add latency to code completion availability when first loading the project or after cleaning it.

Live Update on File Change

So, the generated code is updated whenever the project changes. But what happens when the contents of the ResourceFile files that it depends on change?

This is handled via Generator metadata on each of the ResourceFile files:

  <ResourceFile Include="Foo.png">

This takes advantage of another Visual Studio feature. Whenever the file is saved, VS runs the UpdateGeneratedFiles target. The code completion system detects the change to the generated file and reparses it.

This metadata has to be applied to the items by the IDE (or the user). It may be possible for the build targets to apply it automatically using an ItemDefinitionGroup but I haven't tested whether VS respects this for Generator metadata.

Xamarin Studio/MonoDevelop

But we have another problem. What about Xamarin Studio/MonoDevelop?

Although Xamarin Studio respects Generator metadata, it doesn't have an in-process compiler. It doesn't run CoreCompile, nor does it intercept the Csc file list, so its code completion system won't see the generated file at all.

The solution - for now - is to add explicit support in a Xamarin Studio addin to run the UpdateGeneratedFiles target on project load and when the resource files change, parse the generated file and inject it into the type system directly.


Migrating automatically from a designer-generation system to a build-generation system has a few implications.

You either have to force migration of the project to the new system via an IDE, or handle the old system and make the migration optional - e.g. toggled by the presence of the old files. You have to update the project templates and samples, and you have to build a migration system that removes the designer files from the project and adds Generator metadata to existing files.

MonoDevelop.AddinMaker 1.2

I've finally released MonoDevelop.AddinMaker 1.2, making it easier than ever before to customize and add features to Xamarin Studio and MonoDevelop. This release has been stalled for a while due to factors beyond my control, and I'm very happy to be able to make it available at last. Thanks to Lluis for getting rid of the roadblocks!

This release improves the basic user experience by introducing the concept of "Addin References". Instead of referencing an addin's assemblies and explicitly adding a dependency on that addin, you can add an addin reference, which will automatically take care of both of these things for you.

However, the most important changes are below the surface. Switching to the MSBuild engine allows you to use custom MSBuild syntax to customize your build process, and enables command-line builds via MSBuild/xbuild. More importantly, it provides a solid foundation on which to build future improvements.

Happy extending!

Razor Preprocessed Templates

When Miguel asked me to investigate making MonoDevelop support using Razor templates in MonoTouch and Mono for Android apps, I realized that it could be done simply and with very few dependencies by taking the same approach as T4 preprocessed templates, which I implemented for MonoDevelop a couple of years ago. Fortunately, this time the hard part was already done: I could use Microsoft's open-source Razor parser instead of writing my own parser. I also found a Visual Studio extension called Razor Generator that was very close in functionality to what I wanted, and was able to use this as a basis for my work. I was able to hook it it into the fantastic Razor editing and code completion support written by Piotr Dowgiallo in the Google Summer of Code this year.

After a few days work implementing, tweaking and tuning (and bouncing ideas off Bojan Rajković), I present Razor Preprocessed Templates in MonoDevelop.

As a basis for this demo, I created new a MonoTouch iPhone Single View application, added a UIWebView to the View's xib, and connected it to an outlet on the controller called webview. However, you can use these templates in any project type.

Just add a new Text Templating -> Preprocessed Razor Template file to the project:

Adding a new Preprocessed Razor Template

You will see that this adds a cshtml Razor C# file to the project, grouped with a C# file that contains the generated code. Like T4 files, this uses the "custom tool" extensibility mechanism. By setting the custom tool property on the cshtml file set to "RazorTemplatePreprocessor", it causes MonoDevelop to use this new custom tool to regenerate the cs file whenever the cshtml file is saved.

The files added by the Preprocessed Razor Template

I wrote a simple Razor page to demonstrate the power of Razor. It uses a simple Razor helper to demonstrate that Razor helpers work correctly. The page also demonstrates using the @model directive to specify the type for a Model property, which easily allows us to pass data into the template before running it. Since this demo is very simple, I just used an int as the model instead of defining a proper model class.

@model int
@helper boldtd (int i) {
		<title>Multiplication Table</title>
		<h1>Multiplication Table</h1>
			@* Header row *@
			@for (int i = 1; i <= Model; i++) {
			@* Main table *@
			@for (int i = 1; i <= Model; i++) {
				@for (int j = 1; j <= Model; j++) {
				<td>@(j * i)</td>

When writing this, the Razor code completion was very helpful. It has full C# completion, including locals, helpers and members from the generated class and base class, including the generated Model property:

Code completion for C# in Razor Templates

There's also HTML completion and on-the-fly underlining of HTML and Razor errors:

Code completion and error underlining for HTML in Razor Templates

After saving the cshtml file, you can look at the generated cs file. It's pretty messy, so I won't show it here, but note that it includes a well-commented generated base class. If you want, you can specify a base class using the @inherits directive, so you can pull that generated base class out and share it between multiple templates, or customize it. The template's only dependency is Action<System.IO.TextWriter>, and the default base class's dependencies are only System.Web.HttpUtility.HtmlEncode(string) and System.IO.StringWriter, so it can easily be made to run anywhere. If your target framework lacks the one potentially awkward dependency, HttpUtility.HtmlEncode(string), you can provide an alternative implementation via a custom base class.

More documentation on the generated class and the available directives can be found on the MonoDevelop website.

To use the template, I simply added two lines to my ViewDidLoad method to instantiate the template, generate a string using the Generate() method, and load it into the UIWebView:

public override void ViewDidLoad ()
    base.ViewDidLoad ();
    var template = new MultiplicationTable () { Model = 12 };
    webview.LoadHtmlString (template.GenerateString (), null);

Then run the app, and you can see it in action:

Razor Template running on iPhone

This is a fantastic way to generate HTML pages without pulling in the whole System.Web stack, and I hope you're as excited about it as I am. It isn't available today, unless you build MonoDevelop from source, however the code is committed to MonoDevelop master and should make it into the next 3.x release.

The State of MSBuild Support in MonoDevelop

I occasionally get questions about support for advanced MSBuild features, and my answers are usually fairly short. This post aims to be a more comprehensive description of the current state state of MSBuild support in MonoDevelop, so I can refer people to it. If you've never hand-edited custom targets for an MSBuild file, this doesn't affect you at all, but feel free to read it if you're curious.

MSBuild is the Microsoft build engine that was introduced with .NET 2.0 and Visual Studio 2005. It's the format for the project files of Visual Studio 2005 and later, and has been MonoDevelop's default/native file format since MonoDevelop 2.0. It's XML-based, so it can be handled easily and reliably by tools such as IDEs. It's essentially intended to be consumed primarily by IDEs — but it also has the power of an advanced, extensible build system that lets you do pretty much anything if you're willing to get your hands dirty. Calling it "Makefiles in XML" wouldn't be too far off the mark.

The MSBuild engine and hosting API are part of the .NET framework. Mono has its own implementation called xbuild, which runs on Mac, Linux and Windows. The core features of xbuild are solid, but it's missing some of the advanced features added to MSBuild in .NET 3.5 and 4.0, and some of the common targets such as the ASP.NET web project targets.

MSBuild is extremely complex and exposing all its features in the MonoDevelop GUI would be practically impossible. However, the projects created by the IDE by default use the "common targets" to build the project, which understand a certain number of properties (e.g. "OutputPath") and item types (e.g. "Compile", "EmbeddedResource"), and implement certain targets (e.g. "Build", "Clean") that operate on these items and are controlled by these properties. They also make very limited use of conditions, by conventionally having several groups of properties conditional upon the value of the "Configuration" and "Platform" variables. To edit projects that haven't been hand-modified, the IDE only really has to understand the item types, properties, and targets used by the common targets.

MonoDevelop has an internal project model that represents the projects and their properties and items. When loading the project, MD deserializes the MSBuild file into its internal model, and ignores the items, properties and imports it does not understand. When saving MSBuild projects, MD serializes its internal model into the MSBuild format and substitutes them for the parts of the MSBuild file it understood, thereby preserving the parts of the file that it did not understand: custom items, properties, targets, conditions, and imports.

There are a couple of things the serialization doesn't handle — when hand-edited projects use properties or items as values of any of the standard properties or items. MonoDevelop does not evaluate these — doing so would require a full MSBuild engine — and even if it did, it would not easily be able to present them cleanly in the UI and serialize back any changes to the values. It might be possible to special-case some things, but it's not something that can be fully solved in a generic way. Probably the best we could get would be to have MonoDevelop detect property/item values it cannot handle, evaluate them via the MSBuild engine when loading, make them non-editable with a warning flag in the Project Options GUI, and skip them when serializing.

Wildcards in items paths are a similar problem. For example, evaluating a wildcard in an item when deserializng would result in adding several items to MD's internal model. And in simple cases, we could match all those items back to the wildcard MSBuild item when serializing. So far so good. But what happens if a new file is added on disk outside of MD while the project is open? What happens if the user manually removed a single item in the solution tree? What happens if the user changes the metadata of a single item? In all of these cases, the items can no longer be mapped back to the single wildcard MSBuild item. It would probably be possible to remove the wildcard item and serialize all the items to MSBuild individually — but that might not be what the user expected. This is just one example of how supporting a simple MSBuild feature in the IDE might not be as simple as it looks.

For building the projects, MonoDevelop's story is much better, because it has the ability to build the projects using the actual MSBuild/xbuild engine, thereby supporting all features that MSBuild/xbuild does. There are currently two build codepaths in MonoDevelop: the old internal build engine, and the MSBuild engine. The internal build engine is very old, and predates the existence of MSBuild. It operates directly on MonoDevelop's internal project model, and is extensible via the addin system. The MSBuild engine loads the actual project files into a builder process and builds them using the MSBuild hosting API.

By default MonoDevelop uses the old engine, but the addins for individual project types can opt into using the MSBuild engine for just those projects — Mono for Android does this. Enabling the experimental option "Compile projects using MSBuild/XBuild" in MonoDevelop's preferences (and restarting MonoDevelop) causes the MSBuild engine to be used for all projects. This is marked experimental because it does not work for all project types — for example, there are no MSBuild targets for MonoTouch, MonoMac or ASP.NET projects yet. User projects that depend on MonoDevelop's old custom build command system will not build correctly with the MSBuild engine. And some of the old MonoDevelop custom project types such as translation projects and C/C++ projects would need to be migrated to a new file format before they could even have MSBuild targets.

In general, expansion of the MSBuild support in MonoDevelop is not a high priority for the MonoDevelop team, since these advanced build features are of interest to a very small subset of users, and there are other things that could be done to improve the IDE experience for a much greater number of users. However, it's an area of ongoing improvement and will likely become more important in the future.

MonoMac video from NDC 2011

The videos from NDC 2011 are now online, including my talk Developing .NET Applications for the Mac App Store (direct link). You can also download a printable version of my presentation. Hopefully there will be an official torrent of the videos soon, because there were plenty of other sessions that are worth seeing. Thanks to everyone who helped make it such a great conference!

Correction: In the presentation I said that native objects don't retain references to managed objects, which is incorrect. The problem I intended to refer to is a common coding error where views are retained but their controllers are not, e.g. mainView.AddSubview(new ChildController().View).

Speaking at DevTeach

This week I'm going to be presenting two sessions at DevTeach 11 in Montreal.

My first topic will be Native .NET Apps for the Mac with MonoMac. I'll be taking about MonoMac and the native Mac APIs that it exposes to .NET developers, and how to build native apps that can be deployed to the Mac App store. My second topic will be Using Mono for Native Apps on Mac, Android, iPhone and more. I'll be giving an overview of Mono's most exciting platform integration technologies: Mono for Android, MonoTouch (iPhone), and MonoMac. I'll also explore strategies to share and re-use code between these and other .NET platforms such as Windows, Silverlight and Windows Phone 7.

I'll also be on the DotNetRocks panel to discuss mobile platforms, and outside my talks I'll be happy to discuss any Mono-related topic, especially the technologies I've worked on — MonoDevelop, MonoTouch, Mono for Android, ASP.NET MVC, and the use of Mono in games.

Preview of Xcode 4 Support for MonoTouch

In their recent update of the iOS and Mac developer tools, Apple removed the standalone Interface Builder application and integrated the GUI designer functionality directly into Xcode 4. This was a very significant change and removed several features that were necessary for MonoTouch and MonoMac to integrate with the designer. As soon as Xcode 4 went final, we started working on MonoDevelop support for integrating MonoTouch with Xcode 4's GUI designer, but it's a complex project and will take some time to complete.

For now we recommend using Xcode 3's Interface Builder, which can easily be done even while having Xcode 4 installed. However, I understand that many people are concerned how Xcode 4 will be better supported going forward for MonoTouch and MonoMac, so I've made a screencast to introduce MonoDevelop's upcoming Xcode 4 integration and demonstrate how it works.

MonoTouch integration with Xcode 4

Although XIB files can be opened standalone in Xcode 4, this isn't very useful, as it's no longer possible to define custom types, outlets and actions within the interface designer. Instead the designer is aware of the Objective-C types defined in the Xcode project that contains the XIB file. This means that we have to generate an Xcode project file and synchronize it with the MonoDevelop project. Since Apple doesn't support writing third-party Xcode plugins, we can't make the Xcode designer directly aware of types defined in C# source, so the generated Xcode project also has to contain Objective-C stubs for all the types defined in C#. The Xcode designer now modifies the Objective-C source files when adding outlets or actions, so those changes need to be synchronized back the the MonoDevelop project.

The new model of Xcode integration resolves several outstanding issues in the previous Interface Builder integration. Because the generated project contains all bundle resources (Content and Page files) from the project, they will now be accessible directly from the GUI designer. In addition, all user-defined types are accessible from the designer, not just those defined for the current xib file.

Obviously, synchronizing files and types between two projects in separate applications is complex, and if anything goes wrong it's possible to lose data, so we want to make sure it's as reliable as possible before we release it. We hope to be able to offer a beta of this functionality within the next few weeks.


Subscribe to Journal