Running SPCAF from PowerShell

Running SPCAF from PowerShell

Having build automation is a good thing, and having code analysis running from these builds is even better and will help you achieve way better releases. So when I heard of the SharePoint Code Analysis Application (you should download the SPCAF BETA). I made sure we got it implemented in our nightly- and release builds. Since SPCAF will analyze assemblies and all the XML for your SharePoint packages (wsp’s), and all other controls and masterpages that are present in your package, it will help you improve your code or at least make it easier to check for errors.

Implementing it is pretty straight forward if you are running Team Foundation Server, but if you are running anything else (CruiseControl  in my case) it might be a little more hassle since you have two options. Either you use the command line, or an MSBuild task. The MSBuild task is probably the easiest one to implement, but if you want to run it from PowerShell you should use the command line option. And that will look something like the following:

spcaf.exe  -i mywsp.wsp -r xml 

You can find all options and some more background information in the online documentation.

The advantage of using the command line version (or in my case using PowerShell) is that you can implement it in deployment scripts as well. So whenever you deploy something (through PowerShell) you can use the same script to make sure the packages you would like to deploy will have no errors (or warnings). That way the IT-pro’s out there have some more knowledge of the code they are installing on their farms.

Now if you would like to use this from PowerShell you would need to extend it a bit, and in our case we came up with the following piece of script that will allow you to analyze an package, and specify the ruleset you would like to use (you could of course leave that blank to use the default one, but that will be an SharePoint 2013 version and for this project we were still running on 2010). Finally the script will render the amount of errors and warnings it encountered in executing SPCAF by analyzing the SPCAF output. You could of course extend that to give more feedback to the user, but since SPCAF will generate XML that will be pretty straight forward.

$spcafdir = "";
$outpuDir = ""; 
$projectName = "";

$rulesetPath = "$spcafdir\SPCAF\v4\RuleSets\MyRuleset.spruleset"
$spcafCommand = "$spcafdir\SPCAF\v4\SPCAF.exe -i ""$wspPath"" -r ""XML"" -s ""$rulesetPath"" -o ""$outputDir"""

Invoke-Expression -Command $spcafCommand | Out-Null    
$reportFile = "$outputDir\SPCAFResult_Rules.xml"

    $rulesOutput = Get-Content $reportFile -ErrorAction Stop;
    Write-Warning "Could not find SPCAF file. Ensure that a file called $reportFile exists. Exception: $_" -ErrorAction stop

$errorCount = $rulesOutput.SPCopReport.Errors;
$warningCount = $rulesOutput.SPCopReport.Warnings;

if($errorCount -gt 0)
    Write-Host -ForegroundColor Red "SPCAF reported $errorCount errors and $warningCount warnings in $projectName"
else {
    Write-Host "No SPCAF errors ($warningCount warnings) found in $projectName - good job!" -ForegroundColor DarkGray

Leave a Reply