Two weeks ago, Microsoft released "Microsoft Source Analyzer Tool for C#" (I'll call it MSAT). This tool, also known as "StyleCop", enforces coding style rules against your source code. It is really useful when working in teams : everyone has to follow the same coding style, making source understanding easier for everyone.
Installing
MSAT can be downloaded here : http://code.msdn.microsoft.com/sourceanalysis/Release/ProjectReleases.aspx?ReleaseId=1047. Installation is straightforward, just be sure to have the MSBuild checkbox checked, or there won't be any MSBuild integration available.
Integrating in your projects
When you have installed MSAT, a new entry appears in the "Tools" menu inside Visual Studio : "Run Source Analysis". You can now launch source analysis inside Visual Studio, the results will be inside "Source Analysis" tab. Double-click a message, and you will go to the affected line. Nothing unusual!
You can also directly include source analysis in the build process. And that's pretty cool : every time a developper build the project, the analysis is run, and results displayed inside "Warning" section of the "Errors" tab. Now, there are two options :
- MSAT is installed on every developper's pc
For every project you wish to enable source analysis, open the .csproj file using a text editor, and edit it this way :
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
[...]
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(ProgramFiles)\MSBuild\Microsoft\SourceAnalysis\v4.2\Microsoft.SourceAnalysis.targets" />
[...]
</Project>
- MSAT isn't installed on every developper's PC
Following my project tree post, you can add MSAT folder inside the "External" folder of your solution : create a "SourceAnalysis" folder, and copy everything inside "C:\Program Files\Microsoft Source Analysis Tool for C#" into it. For every project you wish to enable source analysis, open the .csproj file using a text editor, and edit it this way :
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
[...]
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\External\SourceAnalysis\Microsoft.SourceAnalysis.targets" />
[...]
</Project>
Finally, add this directory ("SourceAnalysis") to the repository : every developper will get free Source Analysis on their next update !
Note : when you load your projects after modifying the .csproj file, a warning will appear : choose to load the project normally.
Et voilà! Source analysis is now integrated in our build process!