Thursday, 16 July 2009

protobuf-net; now with added Orcas

A long running gripe with protobuf-net is that while the code generation (from .proto) works, it isn't easy to integrate into your development process. Well, wait no longer! I'm very happy to announce that protobuf-net now has Visual Studio 2008 support

(important note: this does not include the "Express" editions of Visual Studio).

A big vote of thanks goes to Shaun Cooley, who kindly created me the initial "cut" which I've extended. I did try this before, but I drowned in the complexity that is Visual Studio extensibility.

So what do I get?

Initially, the installer includes a "Custom Tool" (registered against the .proto extension), and an item template (not currently registered by the tooling):

The Custom Tool

This means you can add new text file called "mymessage.proto", and it will automatically start running C# (see below) code-generation on that .proto - writing a .cs file to match your .proto. This includes things like error-processing, so if you have errors in your .proto you can click on them and it will take you to the right place in the file. It will also add the protobuf-net reference if you don't already have it.


For existing files, you can set the Custom Tool manually; the string you need is "ProtoBufTool" (original, huh?).

At the moment, protobuf-net lets you pass a number of options on the command line. This same functionality is available in the IDE too - just (ab)use the "Custom Tool Namespace" - it accepts a semi-colon delimited set of arguments (the first being reserved for the actual namespace to use if your file doesn't specify a package) - i.e. if you use the string ";fixCase", it will apply C#-style case fixups (so "name" becomes "Name", etc). The arguments are exactly the same as on the command line tool (protogen.exe).

And if you think this is a bit ugly; I have "plans" (see below).

The template

The installer also includes (in the loosest sense) a custom item template, so you can use Add -> New Item to get a new .proto file with a suitable skeleton (including things like the project's namespace, etc). It doesn't currently install this template - and I'll be honest; this is simply because I haven't yet had time to crack the installer specifics of this (it is.... fiddly).

To install this manually:

  • copy the file from (protobuf-net install dir)\ItemTemplates\csharp\data\1033 into (VS install location - Microsoft Visual Studio 9.0)\Common7\IDE\ItemTemplates\CSharp\Data\1033
  • from the VS2008 command prompt, execute "devenv /installvstemplates" (it may take a minute or two)

Enough; where is it?

The installer is available from the project home page.

What next

The support at the moment is limited; I have a number of things I want to do:

  • Add VB support! (the main problem here is detecting the project's language; the code generation itself works)
  • Register the ItemTemplate during installation
  • Move a lot of the command-line switches into custom .proto attributes
  • Fix the annoying UTF8 BOM issue once and for all...
  • etc

I hope you find it useful; working with VS extensibility has certainly been a different set of challenges - I'll blog about some of those separately.