Daniel Fisher (lennybacon.com)

SOA, DATA & THE WEB

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

Comments

Thanks Daniel! The code doesn't work for website projects (you might not want it to). if you do want to get it to work for websites, here is the code to run for that project item: Private Sub SetRulesInWebsiteProject(ByVal project As Project) For Each prop As [Property] In project.Properties If prop.Name = "EnableFxCop" Then prop.Value = "True&q
What does this mean? Something like I can have Code analysis in my Visual Studio 2005 projects using a marco. I heard that integrated code analysis is applicable only for VSTS.

Write a comment