Integrate Microsoft C# Source Analysis with TFS

UPDATE – 15 October 2008 : Please see MSBuild Extension Pack for the StyleCop task.

UPDATE – 20 July 2008: This task is now available again. This task works with Source Analysis 4.2. A version for StyleCop 4.3 will be made available in the FreeToDev MSBuild Tasks Suite soon…

The FTDSourceAnalysis MSBuild task (download here) provides a mechanism for scanning C# files for Source Analysis violations based on a collection of files, rather than the MSBuild integration offered by the tasks provided with the  Source Analysis install (which scans @(Compile) items).

To use this task, place the FTDSourceAnalysis.dll file in C:\Program Files\MSBuild\Microsoft\SourceAnalysis\v4.2


<Project DefaultTargets=”Default” xmlns=”″>
    <UsingTask AssemblyFile=”C:\Program Files\MSBuild\Microsoft\SourceAnalysis\v4.2\FTDSourceAnalysis.dll” TaskName=”FTDSourceAnalysis”/>
    <Target Name=”Default”>
<!– Create a collection of files to scan –>
        <CreateItem Include=”C:\SomeFiles\**\*.cs”>
            <Output TaskParameter=”Include” ItemName=”SourceAnalysisFiles”/>
        <!– Run the FTDSourceAnalysis MSBuild task –>
        <FTDSourceAnalysis SourceFiles=”@(SourceAnalysisFiles)” ShowOutput=”true” ForceFullAnalysis=”true” CacheResults=”false” logFile=”C:\FTDSALog.txt” SettingsFile=”C:\Settings.SourceAnalysis”>
            <Output TaskParameter=”Succeeded” PropertyName=”AllPassed”/>
            <Output TaskParameter=”ViolationCount” PropertyName=”Violations”/>
            <Output TaskParameter=”FailedFiles” ItemName=”Failures”/>
        <Message Text=”Succeeded: $(AllPassed), Violations: $(Violations)”/>
    /// FailedFile format is:
    ///     <ItemGroup>
    ///     <FailedFile Include=”filename”>
    ///         <CheckId>SA Rule Number</CheckId>
    ///         <RuleDescription>Rule Description</RuleDescription>
    ///         <RuleName>Rule Name</RuleName>
    ///         <LineNumber>Line the violation appears on</LineNumber>
    ///         <Message>SA violation message</Message>
    ///     </FailedFile>
    ///  </ItemGroup>
        <Message Text=”%(Failures.Identity) – Failed on Line %(Failures.LineNumber). %(Failures.CheckId): %(Failures.Message)”/>

Sample Output

Target Default:
    Performing Microsoft C# Source Analysis scan…
    Pass 1: FTDSourceAnalysis (2).cs…
    1 violations encountered.
    Succeeded: False, Violations: 1
    C:\SomeFiles\FTDSourceAnalysis (2).cs – Failed on Line 1. SA1638: The file attribute in the file header’s copyright tag must contain the name of the file.

This custom collection based scanning provides an easy way to scan various files as part of your TFS builds and flag any violations to the team.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s