cake-build -. NET Core Cross-platform build automation system

cake-build -. NET Core Cross-platform build automation system

Cake (C#Make) is a build automation system with C#DSL used to perform operations such as compiling code, copying files/folders, running unit tests, compressing files, and building NuGet packages.

最后更新 7/12/2022 8:13 PM
黑哥聊dotNet
预计阅读 4 分钟
分类
.NET
标签
.NET C# NuGet

introduced

Cake (C#Make) is a build automation system with C#DSL used to perform operations such as compiling code, copying files/folders, running unit tests, compressing files, and building NuGet packages.

地址:https://cakebuild.net/docs

build

This tutorial uses Cake Frosting, which allows you to build and write a standard console application as part of the solution. Other possibilities for how to run a Cake build.

以下说明需要在 .NET Core 3.1.301 或更高版本上运行 Cake Frosting 1.0.0 或更高版本。您可以在https://dotnet.microsoft.com/download找到 .NET SDK

To create a new Cake Frosting project, you need to install the Frosting template:

dotnet new --install Cake.Frosting.Template

Create a new Frosting project:

dotnet new cakefrosting

This will create the Cake Frosting project and boot script.

Initial build project

This type of Program contains code to configure and run Cake:

public static class Program
{
    public static int Main(string[] args)
    {
        return new CakeHost()
            .UseContext<BuildContext>()
            .Run(args);
    }
}

该类BuildContext可用于添加其他自定义属性。Delay默认模板包含一个可以通过参数设置的示例属性--delay。您可以删除此属性并根据您的特定需求自定义属性。

public class BuildContext : FrostingContext
{
    public bool Delay { get; set; }

    public BuildContext(ICakeContext context)
        : base(context)
    {
        Delay = context.Arguments.HasArgument("delay");
    }
}

The file also contains three task classes:

[TaskName("Hello")]
public sealed class HelloTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
    {
        context.Log.Information("Hello");
    }
}

[TaskName("World")]
[IsDependentOn(typeof(HelloTask))]
public sealed class WorldTask : AsyncFrostingTask<BuildContext>
{
    // Tasks can be asynchronous
    public override async Task RunAsync(BuildContext context)
    {
        if (context.Delay)
        {
            context.Log.Information("Waiting...");
            await Task.Delay(1500);
        }

        context.Log.Information("World");
    }
}

[TaskName("Default")]
[IsDependentOn(typeof(WorldTask))]
public class DefaultTask : FrostingTask
{
}

Default任务对World有依赖性。该World任务是一个异步任务Delay,如果设置了属性,则等待一秒半。

Example Build Pipeline

The following example creates a simple build pipeline that contains a task, a task to compile the MsBuild solution, and a task to test the solution.

The following example requires src/Example.s in the root folder of the Visual Studio solution.

Add the required using statements:

using Cake.Common;
using Cake.Common.IO;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Build;
using Cake.Common.Tools.DotNet.Test;

从类中删除Delay属性BuildContext并添加一个属性MsBuildConfiguration,它存储应该构建的解决方案的配置:

public class BuildContext : FrostingContext
{
    public string MsBuildConfiguration { get; set; }

    public BuildContext(ICakeContext context)
        : base(context)
    {
        MsBuildConfiguration = context.Argument("configuration", "Release");
    }
}

HelloTaskWorldTask可以删除。

CleanTask为清理目录的任务创建一个新类:

[TaskName("Clean")]
public sealed class CleanTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
    {
        context.CleanDirectory($"../src/Example/bin/{context.MsBuildConfiguration}");
    }
}

Create a new class that BuildTask uses to build solutions:

[TaskName("Build")]
[IsDependentOn(typeof(CleanTask))]
public sealed class BuildTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
{
        context.DotNetBuild("../src/Example.sln", new DotNetBuildSettings
        {
            Configuration = context.MsBuildConfiguration,
        });
    }
}

Create a new class for TestTask to use to test solutions:

[TaskName("Test")]
[IsDependentOn(typeof(BuildTask))]
public sealed class TestTask : FrostingTask<BuildContext>
{
    public override void Run(BuildContext context)
{
        context.DotNetTest("../src/Example.sln", new DotNetTestSettings
        {
            Configuration = context.MsBuildConfiguration,
            NoBuild = true,
        });
    }
}

更新DefaultTask类以调用新任务:

[IsDependentOn(typeof(TestTask))]
public sealed class Default : FrostingTask
{
}

Run the build script

Run the build script

./build.ps1

更多文档请前往 cake-build 官网:https://cakebuild.net

Finally, if you like my article, please pay attention to it. I hope that the. NET ecosystem will get better and better!

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 5/24/2025

Hello. NET run file, bye csproj

This article introduces the new file-based program feature of the. NET CLI, which allows developers to run C#source files directly without creating project files. This feature makes it easy to develop scripts and simple applications by generating virtual project files in memory and supporting NuGet dependency packages and project property settings. The article also looks forward to the future of this feature, including target path extensions, unified command line parameters, performance improvements, and more file-based program command support.

继续阅读
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读