Search code examples
c#msbuildcakebuild

Build all solutions within a tree using Cake (C# make)?


I have multiple VS solutions within the same directory tree and would like to build all of them using Cake. Is there a way to build all of them without putting them one by one into the build script?

Thanks for any ideas


Solution

  • Yes that's certainly possible using the built-in globber features, example:

    var solutions           = GetFiles("./**/*.sln");
    
    Task("Build")
        .IsDependentOn("Clean")
        .IsDependentOn("Restore")
        .Does(() =>
    {
        // Build all solutions.
        foreach(var solution in solutions)
        {
            Information("Building {0}", solution);
            MSBuild(solution, settings =>
                settings.SetPlatformTarget(PlatformTarget.MSIL)
                    .WithProperty("TreatWarningsAsErrors","true")
                    .WithTarget("Build")
                    .SetConfiguration(configuration));
        }
    });
    

    Similarly you can do the same before build with nuget restore, example

    Task("Restore")
        .Does(() =>
    {
        // Restore all NuGet packages.
        foreach(var solution in solutions)
        {
            Information("Restoring {0}...", solution);
            NuGetRestore(solution);
        }
    });
    

    And a clean task could be adapted like this

    var solutionPaths       = solutions.Select(solution => solution.GetDirectory());
    
    Task("Clean")
        .Does(() =>
    {
        // Clean solution directories.
        foreach(var path in solutionPaths)
        {
            Information("Cleaning {0}", path);
            CleanDirectories(path + "/**/bin/" + configuration);
            CleanDirectories(path + "/**/obj/" + configuration);
        }
    });