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.
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");
}
}
和HelloTask类WorldTask可以删除。
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!