[TEAMBUILD2010] HowTo: Perform code analysis with StyleCop in Team Build 2010

image47dd1de4

Good,

After commenting on the launch of the first version of tfs build extensions to Team build 2010, because today is detailed step by step to one of the custom have released, join a Team Build definition. In this case, an analysis of code withStyleCop after compiling a project or solution.

Tutorial

1. Download and install StyleCop, from its CodePlexproject site: http://stylecop.codeplex.com/. In addition, download and unzip the current version of tfs build extensions from its CodePlexsite: http://tfsbuildextensions.codeplex.com/

2. For this example we will use a project of type class library with a single class with the following code:

   1: using System;

 

   2: namespace ElBruno.StyleCopBuild.Demo

 

   3: {

 

   4:     public class Valentino

 

   5:     {

 

   6:         public Valentino(int edad)

 

   7:         {

 

   8:             if(edad > 100)

 

   9:             {

 

  10:

 

  11:                 throw  new ArgumentException("la edad no puede ser mayor que 100", "edad");

 

  12:             }

 

  13:         }

 

  14:     }

 

  15: }

 

With these few lines of code we can already launch a couple of warnings of StyleCop. If we make an analysis of the project, we will find the following:

image

3 Protect this code in our server Team Foundation Server 2010 and create a definition of Team Build 2010 to compile and validate this project. Obviously, the expected result is the following:

image

a Build with State success, which has been compiled and tests have been executed correctly.

4. The following a little organise the directory structure in order thus to have assembled required for custom definition of Team Build to implement analysis of code with StyleCoprules. For this, I created a structure with several folders in the SCC:

  • build
    Own assembled used during the build process will be stored in this folder. For example, the dlls that ship with tfs build extensions. Also in this folder copy a template from Team Build (for example from$/BuildProcessTemplates/DefaultTemplate.xaml) and on it will make the changes in the build process.
  • src
    This folder stores the code source of the solution

image

5 Modify the definition of the Build Controller, use the directory configured in the previous step as a repository of custom assemblies for the execution of the Build.

image

Note: If you want to see a full example on this topic, this post can help > https://elbruno.com/2011/03/22/teambuild2010-howto-crear-una-custom-activity-para-team-build-2010/

6. Below we create a new project of type Class Library in Visual Studio 2010, the location of the directory templates of Builds with the assembled. This project you will use to edit build definition.

7 Remove the class (Class1.cs) project and add a reference to the xaml file using an Add As Link to the project. The project is created in its own folder in the SCC, so we will have to upload a couple of levels until you reach theBuildTemplateWithStyleCop.xamlfile.

image

8 Add the references to all the assembled in the Build folder, which are those who have downloaded from tfs build extensions.

image

9 Edit the BuildTemplateWithStyleCop.xaml file and in the ToolBox of tools, add a new section called [tfs build extensions]. Within the same add the activities of the Assembly that we have stored in the build directory. The section should be similar to the following:

image

10 Access to the compilation of each project and activity [Run MSBuild for Project] Add a new sequence which we call [Run StyleCop Analysis]. Add the following variables with the scope of the sequence in the sequence

  • StyleCopFiles – iEmumerable < string >
  • StyleCopSettingsFile – string
  • StyleCopResults – bool
  • StyleCopViolations – int32

11. In the sequence we add 2 activities of the type [WriteBuildMessage] to indicate the beginning and the end of the process of analysis with StyleCop. We call them “start stylecop analysis” and “finish stylecop analysis“, and show you a message with the same name of each activity.

image

12 Of the first message, add an activity of the type [FindMatchingFile] to identify the .cs files on which the analysis will be made. In the same configure:

  • Display Name > Find CS files for source analysis
  • Result > StyleCopFiles
  • (“MatchPattern > String.Format(“{0}\**\*.cs “, BuildDirectory)

image

13. Then add an activity of type [Assign] to locate the stylecop settings file. In the same configure:

  • Display Name > Assign StyleCop Settings File
  • To > StyleCopSettingsFile
  • (“Value > String.Format(“{0}\Settings.StyleCop “, localProject.Substring (0, localProject.LastIndexOf(“\”)))

image

14. Then we add an activity of the type [WriteBuildMessage]. In the same configure:

  • Display Name > Start StyleCop Analysis using settings file
  • Importance > Microsoft.TeamFoundation.Build.Client.BuildMessageImportance.High
  • Message > string.format(“about_to_run_stylecop_with_[{0}]”,_stylecopsettingsfile)

15. Then add an activity of the type [StyleCop] is that performed the analysis of code. In the same configure:

  • Display Name > Run StyleCop Analysis
  • SettingsFile > StyleCopSettingsFile
  • SourceFiles > StyleCopFiles.ToArray)
  • Succeeded > StyleCopResults
  • TreatViolationsErrorASWarnings > True
  • ViolationCount > StyleCopViolations

An important detail is True the property [TreatViolationsErrorASWarnings] so that errors are in the process of code analysis are displayed in the log as Warnings.

16. Then add an activity of the type [WriteBuildMessage]. In the same configure:

  • Name display > Display StyleCop Analysis Results
  • Importance > Microsoft.TeamFoundation.Build.Client.BuildMessageImportance.High
  • Message > String.Format(“StyleCop_Successed:{0}_with_{1}_violations”,_StyleCopResults,_StyleCopViolations)

17. We protect the files modified in TFS. The process should be similar to the following:

image

18 We changed the build definition that you use the process that we have changed in the previous steps.

image

19 Launched the build, and we can already see how the execution of code with StyleCopanalysis process is included in the result of the same.

image

 

To take into account

Yes by the way, when Spears a new build you find yourself with the following error:

Error: Could not load file or assembly ‘ Microsoft.StyleCop, Version = 4 4 0 14, Culture = neutral, PublicKeyToken = 31bf3856ad364e35′ or one of its dependencies. The system cannot find the file specified… Stack Trace: at TfsBuildExtensions.Activities.CodeQuality.StyleCop.Scan (at TfsBuildExtensions.Activities.BaseCodeActivity.Execute(CodeActivityContext_context)) in D:\Projects\teambuild2010contrib\MAIN\Source\Common\BaseCodeActivity.cs:line 66.
Could not load file or assembly ‘ Microsoft.StyleCop, Version = 4 4 0 14, Culture = neutral, PublicKeyToken = 31bf3856ad364e35′ or one of its dependencies. The system cannot find the file specified.

image

It is very likely not have installed the correct version of StyleCop. The version that you download and install ishttp://stylecop.codeplex.com/releases/view/44839

Another option is to not be chargeable the StyleCop assembled for the execution of the build, in this case you should copy the Assembly from the StyleCop installation path [C:\Program Files (x 86) \Microsoft StyleCop 4.4.0.14] and add them into the Build directory.

image

 

Puedes bajar el ejemplo completo desde:

https://skydrive.live.com/embedicon.aspx/Code%20Samples/2011%2007%2010%20ElBruno.StyleCop.TeamBuild.zip?cid=bef06dffdb192125&sc=documents

 

Saludos @ Home

El Bruno

   

Resources:

One thought on “[TEAMBUILD2010] HowTo: Perform code analysis with StyleCop in Team Build 2010

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.