Macro to enable Code Analysis and define FxCop rules
I’m currently helping a customer to migrate a really large web application from ASP.NET 1.1 to 2.0 (round about 130 projects in the master solution…).
I always liked FxCop but enabling “Code Analysis” manually is way to complicated (or not geekish enough?!?). So I wrote this small macro this morning.
Imports System
Imports System.Diagnostics
Imports System.Text
Imports System.Windows.Forms
Imports EnvDTE
Imports EnvDTE80
Public Module CodeAnalysis
Public Sub EnableFxCop()
Dim objProj As Object()
Dim proj As Project
For i As Integer = 1 To DTE.Solution.Projects.Count
proj = DTE.Solution.Projects.Item(i)
EnableFxCop(proj)
Next
End Sub
Private Sub EnableFxCop(ByVal project As Project)
If project.Kind = "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}" Then
'Filter Project Folders
For Each subProject As ProjectItem In project.ProjectItems
EnableFxCop(subProject.SubProject)
Next
Else
project.ConfigurationManager.ActiveConfiguration.Properties.Item( _
"RunCodeAnalysis").Value = "True"
project.ConfigurationManager.ActiveConfiguration.Properties.Item( _
"CodeAnalysisRules").Value = String.Concat( _
"-Microsoft.Design#CA2210;", _
"-Microsoft.Design#CA1020;", _
"-Microsoft.Naming#CA1705;", _
"-Microsoft.Naming#CA1709")
project.Save()
End If
End Sub
End Module