Search code examples
c#cakebuild

Skipping CAKE dependencies


I use CAKE 0.22.0.

Whenever I set Target to RunTests, I want the task CleanUpTempFiles to execute once all unit tests are finished. According to the CAKE documentation, I can simply write the following:

Task("CleanUpTempFiles")
    .IsDependentOn("RunTests")
    .Does(() => { etc. });

However, this won't work for me, because sometimes I would like to run CleanUpTempFiles without actually running any tests. For example, I would like to add a task called RunJetBrainsDotCover, which is dependent on CleanUpTempFiles but not dependent on RunTests.

I thought of creating a method called CleanUpTempFiles, which I will then invoke as the final step within the Does clause of RunTests, and also as the first step within RunJetBrainsDotCover. However, I am not entirely satisfied with this approach -- I much prefer having CleanUpTempFiles as a task, because then it becomes more explicit what steps are involved in running each target.

Any advice?


Solution

  • There is no out of the box mechanism to do exactly what you want just now. There is some discussion about extending the functionality of Cake to allow skipping of dependencies when invoking a Target. This is being discussed here.

    For now, one thing you could do would be to have two entry points, i.e.

    Task("CleanUpTempFiles")
      .IsDependentOn("RunTests")
      .Does(() => { etc. });
    

    and then:

    Task("OnlyCleanUpTempFiles")
      .Does(() => { etc. });
    

    This is not perfect, as there is some duplication here, but it is a viable option.

    Another solution would be to alter the dependency graph at run time. This is done extensively within Cake.Recipe, and allows controlling of the graph, based on some higher level information. For example, here:

    https://github.com/cake-contrib/Cake.Recipe/blob/develop/Cake.Recipe/Content/build.cake#L459

    private static void SetupTasks(bool isDotNetCoreBuild)
    {
        var prefix = isDotNetCoreBuild ? "DotNetCore-" : "";
        BuildParameters.Tasks.CreateNuGetPackagesTask.IsDependentOn(prefix + "Build");
        BuildParameters.Tasks.CreateChocolateyPackagesTask.IsDependentOn(prefix + "Build");
        BuildParameters.Tasks.TestTask.IsDependentOn(prefix + "Build");
        BuildParameters.Tasks.DupFinderTask.IsDependentOn(prefix + "Build");
        BuildParameters.Tasks.InspectCodeTask.IsDependentOn(prefix + "Build");
        BuildParameters.Tasks.PackageTask.IsDependentOn("Analyze");
        BuildParameters.Tasks.PackageTask.IsDependentOn("Test");
        BuildParameters.Tasks.PackageTask.IsDependentOn("Create-NuGet-Packages");
        BuildParameters.Tasks.PackageTask.IsDependentOn("Create-Chocolatey-Packages");
        BuildParameters.Tasks.UploadCodecovReportTask.IsDependentOn("Test");
        BuildParameters.Tasks.UploadCoverallsReportTask.IsDependentOn("Test");
        BuildParameters.Tasks.AppVeyorTask.IsDependentOn("Upload-Coverage-Report");
        BuildParameters.Tasks.AppVeyorTask.IsDependentOn("Publish-Chocolatey-Packages");
        BuildParameters.Tasks.InstallReportGeneratorTask.IsDependentOn(prefix + "Build");
    
        if (!isDotNetCoreBuild)
        {
            BuildParameters.Tasks.TestTask.IsDependentOn("Test-NUnit");
            BuildParameters.Tasks.TestTask.IsDependentOn("Test-xUnit");
            BuildParameters.Tasks.TestTask.IsDependentOn("Test-MSTest");
            BuildParameters.Tasks.TestTask.IsDependentOn("Test-VSTest");
            BuildParameters.Tasks.TestTask.IsDependentOn("Test-Fixie");
            BuildParameters.Tasks.InstallOpenCoverTask.IsDependentOn("Install-ReportUnit");
        }
        else
        {
            BuildParameters.Tasks.TestTask.IsDependentOn(prefix + "Test");
            BuildParameters.Tasks.InstallOpenCoverTask.IsDependentOn("Install-ReportGenerator");
            BuildParameters.Tasks.PackageTask.IsDependentOn(prefix + "Pack");
        }
    }
    

    I am altering the graph based on whether it is a DotNetCore Build, or whether it is a normal .net Framework build.