Examples

To read issues from InspectCode log files the InspectCode issue provider needs to be imported:

build.cake
#addin nuget:?package=Cake.Issues&version=5.2.0
#addin nuget:?package=Cake.Issues.InspectCode&version=5.2.0

Note

In addition to the InspectCode issue provider the Cake.Issues core addin needs to be added.

Build.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Cake.Frosting" Version="5.0.0" />
    <PackageReference Include="Cake.Frosting.Issues.InspectCode" Version="5.2.0" />
  </ItemGroup>
</Project>

The following example contains a task which will run JetBrains InspectCode and write a log file and a task to read issues from the log file and write the number of warnings to the console. JetBrains InspectCode is installed using JetBrains.ReSharper.CommandLineTools:

build.cake
#tool "nuget:?package=JetBrains.ReSharper.CommandLineTools&version=2024.3.3"

var logPath = @"c:\build\inspectcode.xml";
var repoRootFolder = MakeAbsolute(Directory("./"));

Task("Analyze-Project").Does(() =>
{
    // Run InspectCode and enforce XML output.
    var settings = new InspectCodeSettings() {
        OutputFile = logPath,
        ArgumentCustomization = x => x.Append("-f=xml")
    };

    InspectCode(repoRootPath.CombineWithFilePath("MySolution.sln"), settings);
});

Task("Read-Issues")
    .IsDependentOn("Analyze-Project")
    .Does(() =>
    {
        // Read issues.
        var issues =
            ReadIssues(
                InspectCodeIssuesFromFilePath(logPath),
                repoRootPath);

        Information("{0} issues are found.", issues.Count());
});
Program.cs
using Cake.Common.Diagnostics;
using Cake.Core.IO;
using Cake.Core;
using Cake.Frosting;
using Cake.Common.Tools.InspectCode;

public static class Program
{
    public static int Main(string[] args)
    {
        return new CakeHost()
            .UseContext<BuildContext>()
            .InstallTool(
            new Uri(
                "nuget:?package=JetBrains.ReSharper.CommandLineTools&version=2024.3.3"))
            .Run(args);
    }
}

public class BuildContext(ICakeContext context) : FrostingContext(context)
{
    public FilePath LogPath { get; } = @"c:\build\inspectcode.xml";
    public DirectoryPath RepoRootPath { get; } =
        context.MakeAbsolute(context.Directory("./"));
}

[TaskName("Analyze-Project")]
public sealed class AnalyzeProjectTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
    {
        // Run InspectCode and enforce XML output.
        var settings = new InspectCodeSettings() {
            OutputFile = context.LogPath,
            ArgumentCustomization = x => x.Append("-f=xml")
        };

        context.InspectCode(
            context.RepoRootPath.CombineWithFilePath("MySolution.sln"),
            settings);
    }
}

[TaskName("Read-Issues")]
[IsDependentOn(typeof(AnalyzeProjectTask))]
public sealed class ReadIssuesTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
    {
        // Read issues.
        var issues =
            context.ReadIssues(
                context.InspectCodeIssuesFromFilePath(context.LogPath),
                context.RepoRootPath);

        context.Information("{0} issues are found.", issues.Count());
    }
}