Using IronPython to Enforce Code Conventions in Visual Studio Builds

Recently I was reading a blog post by Scott Guthrie about the new syntax for HTML encoding output in ASP.NET 4, where he said the following:

“This enables you to default to always using <%: %> code nuggets instead of <%= %> code blocks within your applications. If you want to be really hardcore you can even create a build rule that searches your application looking for <%= %> usages and flags any cases it finds as an error to enforce that HTML encoding always takes place.”

It seemed like a fun exercise, so I decided to take up the challenge and build a simple rules engine for enforcing code conventions as part of the build. I know there already exist some tools out there for doing this, such as StyleCop, and they almost certainly do it more comprehensively than what I implemented. However, I thought this would be a good example of how easy it is to extend the build process with your own custom checks.


Tags: ,