The Codeplex Community TFS Build Extensions contains a StyleCop activity, but the way to use it is less than obvious. This is not helped by the complexity in using any custom activity within TFS 2010 builds. In this post I will try to show how to get going with the StyleCop activity, which might shed some light on using other custom assemblies.
Get the files
- Download and install StyleCop on you development PC. The alpha 22.214.171.124 custom activity is built against StyleCop 126.96.36.199.
- Download and unzip the Community TFS Build Extensions to a directory on you development PC.
Get the files onto the build box
The assemblies that contain the custom activity and StyleCop need to put under source control so they are available to the build controller and agent(s).
- In VS2010 open Source Control Explorer select your Team Project and map the BuildProcessTemplates folder to a location on your local disk.
- Create a new folder under the BuildProcessTemplates called Custom Assemblies
- In this new folder copy all the assemblies from the unzipped TFS Build Extensions and StyleCop folder (found in Program Files)
- From within Source Control Explorer add these new files to the new Custom Assemblies folder and check the files into TFS.
- Open Team Explorer, right-click on Builds and select Manage Build Controllers
- Select the controller to configure, and then select Properties
- Set the Version control path to custom assemblies to the location just created under version control containing your added assemblies
Get the custom activities into Visual Studio
Next we need to get the activity into Visual Studio so we can add it to build process
- Open Visual Studio 2010
- Create new Class Library project, this new project is only going to be used as a container for the editing of the build process template.
- Delete the Class1.cs file
- In Solution Explorer right click and select Properties., make sure the new project does not build in any configuration.
Now we have a project to work with, we need to get a process template into it, this can be done by branch the process template into the project for adding it as a link. In this example I used a link to one of the standard template in the BuildProcessTemplates folder, for simplicity, but I would normally recommend at least copying the process template in the BuildProcessTemplates folder prior to editing it.
- Make sure BuildProcessTemplates is mapped in you current workspace and get a local copy on your development PC
- In the new project select Add Existing Item and browse to local folder mapped to the BuildProcessTemplates, select the template your wish to edit. But, don’t just press the Add button, but use the drop down to select Add as link.
- On the added file set the build action property to none
- Select Add Reference and add references to all the assemblies from the unzipped TFS Build Extensions
- Open the newly added process template in VS2010, this can be slow, so wait…..
- Open the toolbox and you should see all the standard build activities
- Right click in the toolbox and select Choose Item, select browse and select the file TfsBuildExtensions.Activities.StyleCop.dll.
- The StyleCop activity should now be in the toolbox
Editing the Process template
Well at last we can start to edit the process template, that took a while didn’t it!
Using the standard DefaultBuild template find the ‘Compile the Project’ sequence and add the activities as show in a graphic below. I have chosen to add the sequence inside the compile the project so we can pickup the solution file’s location, but you could choose a different location of that meets you needs. The StyleCop activity does not require to be after the compile stage as it works against the .CS files, not the compiled assemblies.
Here are the breakdown
- Add a new sequence, I named it “Run StyleCop”
- Add the following variable with a scope of the “Run StyleCop” sequence
- StyleCopFiles – iEmumerable<string>
- StyleCopSettingsFile – string
- StyleCopResults – bool
- StyleCopViolations – int32
- SettingsFile = StyleCopSettingsFile
- SourceFiles = StyleCopFiles.ToArray()
- Succeeded = StyleCopResults
- TreatViolationsErrorASWarnings = True – setting this is down to how you want violations to appear in the log, for this example I wanted warnings
- ViolationCount = StyleCopViolations
Running the Build
If you have not done so already create a build using this process template. Queue a new build using the template. If it is all OK you should see a log similar to the graphic below
Notice that the FXCop (code analysis) results appear within the main build summary (green), but the StyleCop violations (red) appear in the Other Errors and Warnings section. Unfortunate this cannot be altered. You cannot add extra bits to the main build summary. However, you could choose to fail the build if there are StyleCop violations
So I hope this has made the use of the StylCop activity more obvious, so you can bolt it into your build process and trigger all the argument in your team as to which rules should be used.