[VS2010] Configure Code Analysis for the Whole Solution
Cet article est disponible en francais.
In Visual Studio, configuring Code Analysis was a bit cumbersome. If you had more than a bunch (say 10), this could take a long time to manage and have a single set of rules for all your solution. You had to resort to update all the projects files by hand, or use a small tool that would edit the csproj file to set the same rules everywhere.
Not very pleasant, nor efficient. Particularly when you have hundreds of projects.
In Visual Studio 2010, the product team added two things :
- Rules are now in external files, and not embedded in the project file. That makes the rules reuseable in other projects in the solution. Nice.
- There’s a new section in the Solution properties named “Code Analysis Settings”, that allows to set rule files to use for single projects, and even better, for all projects ! Very nice.
That option is also available from the “Analyze” menu, with “Configure Code Analysis for Solution”.
One gotcha there though, to be able to select all files, you can’t use Ctrl+A but you have to select all files by selecting the first item, then hold Ctrl while selecting the last item. Maybe the Product Team will fix that for the release...
Migrating Rules from VS2008
If you’re migrating your projects from VS2008, and were using code analysis there, you’ll notice that the converter will generate a file named “Migrated rules for MyProject.ruleset” for every project in the solution. That’s nice if all your projects don’t have the same rules. But if they do, you’ll have to manage all of them...
Like all programmers, I’m lazy, and I wrote a little macro that will remove all generated ruleset files for the current solution, and use a single rule set.
This is not a very efficient macro, but since it won’t be used that often... You’ll probably live with the bad performance, and bad VB.NET code :)
Here it is :
Sub RemoveAllRuleset()
For Each project As Project In DTE.Solution.Projects
FindRuleSets(project)
Next
End Sub
Sub FindRuleSets(ByVal project As Project)
For Each item As ProjectItem In project.ProjectItems
If Not item.SubProject Is Nothing Then
If Not item.SubProject.ProjectItems Is Nothing Then
FindRuleSets(item.SubProject)
End If
End If
If Not item.SubProject Is Nothing Then
If Not item.SubProject.ProjectItems Is Nothing Then
Dim ruleSets As List(Of ProjectItem) = New List(Of ProjectItem)
For Each subItem In item.SubProject.ProjectItems
If subItem.Name.StartsWith("Migrated rules for ") Then
ruleSets.Add(subItem)
End If
Next
For Each ruleset In ruleSets
ruleset.Remove()
Next
End If
End If
Next
End Sub