Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild File to Run Code Analysis Without Overlap

I am creating an MSBuild file to combine some tasks of the things I need to do on my build server into one step. Part of this process requires running code analysis. My problem is that I have running code analysis for multiple top level projects but they each have the same dependencies on other projects like our Utility Library. This means that each run of code analysis duplicates a code analysis run of the Utility Library. Here is my MSBuild file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Dev</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <RunCodeAnalysis>true</RunCodeAnalysis>
        <CodeAnalysisTargets>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\CodeAnalysis\Microsoft.CodeAnalysis.targets</CodeAnalysisTargets>
        <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
        <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
        <DefineConstants>TRACE;DEBUG;CODE_ANALYSIS</DefineConstants>
        <BuildInParallel>true</BuildInParallel>
        <WebProjectTargets>Clean;Build;Package</WebProjectTargets>
        <OtherProjectTargets>Clean;Build</OtherProjectTargets>
        <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
    </PropertyGroup>

    <Target Name="Build">
        <!-- Web Projects -->
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\Source\WebProjects\WebApp1\WebApp1.csproj"
            Properties="
                Configuration=$(Configuration);
                PackageLocation=$(MSBuildProjectDirectory)\Packages\WebApp1.zip;
                RunCodeAnalysis=$(RunCodeAnalysis);
                CodeAnalysisTargets=$(CodeAnalysisTargets);
                CodeAnalysisTreatWarningsAsErrors=$(CodeAnalysisTreatWarningsAsErrors);
                StyleCopTreatErrorsAsWarnings=$(StyleCopTreatErrorsAsWarnings);
                DefineConstants=$(DefineConstants);
                BuildInParallel=$(BuildInParallel);
                GenerateSerializationAssemblies=$(GenerateSerializationAssemblies);
                "
            Targets="$(WebProjectTargets)" />
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\Source\WebProjects\WebApp2\WebApp2.csproj"
            Properties="
                Configuration=$(Configuration);
                PackageLocation=$(MSBuildProjectDirectory)\Packages\WebApp2.zip;
                RunCodeAnalysis=$(RunCodeAnalysis);
                CodeAnalysisTargets=$(CodeAnalysisTargets);
                CodeAnalysisTreatWarningsAsErrors=$(CodeAnalysisTreatWarningsAsErrors);
                StyleCopTreatErrorsAsWarnings=$(StyleCopTreatErrorsAsWarnings);
                DefineConstants=$(DefineConstants);
                BuildInParallel=$(BuildInParallel);
                GenerateSerializationAssemblies=$(GenerateSerializationAssemblies);
                "
            Targets="$(WebProjectTargets)" />
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\Source\WebProjects\Administrator\Administrator.csproj"
            Properties="
                Configuration=$(Configuration);
                PackageLocation=$(MSBuildProjectDirectory)\Packages\Administrator.zip;
                RunCodeAnalysis=$(RunCodeAnalysis);
                CodeAnalysisTargets=$(CodeAnalysisTargets);
                CodeAnalysisTreatWarningsAsErrors=$(CodeAnalysisTreatWarningsAsErrors);
                StyleCopTreatErrorsAsWarnings=$(StyleCopTreatErrorsAsWarnings);
                DefineConstants=$(DefineConstants);
                BuildInParallel=$(BuildInParallel);
                GenerateSerializationAssemblies=$(GenerateSerializationAssemblies);
                "
            Targets="$(WebProjectTargets)" />
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\Source\OtherProjects\Service\Service.csproj"
            Properties="
                Configuration=$(Configuration);
                RunCodeAnalysis=$(RunCodeAnalysis);
                CodeAnalysisTargets=$(CodeAnalysisTargets);
                CodeAnalysisTreatWarningsAsErrors=$(CodeAnalysisTreatWarningsAsErrors);
                StyleCopTreatErrorsAsWarnings=$(StyleCopTreatErrorsAsWarnings);
                DefineConstants=$(DefineConstants);
                BuildInParallel=$(BuildInParallel);
                GenerateSerializationAssemblies=$(GenerateSerializationAssemblies);
                "
            Targets="$(OtherProjectTargets)" />
    </Target>
</Project>

Any idea as to how to prevent code analysis from running on the same assembly more than once in this situation?

like image 384
Hungry Beast Avatar asked Apr 08 '13 20:04

Hungry Beast


1 Answers

Usually, I run code analysis command line tool, after "packing" my binary distribution. The MSBuildCodeMetrics is an "embryo" project for running metrics inside build.

The idea is to break the build if some user commits a method with too many lines or with too many cyclomatic complexity, but the project isn't with this maturity yet.

Anyway, if you want to run the metrics through command line, try:

http://msdn.microsoft.com/en-us/library/bb385914.aspx

Of course you'll need some magic to copy all the assemblies to a temp dir (like your final package) and then run the metrics based on this directory.

like image 74
Eric Lemes Avatar answered Sep 19 '22 14:09

Eric Lemes