[Cake] 0.c#make Automated Construction-Introduction

Source: Internet
Author: User
Tags xunit

0. What is cake?

Cake is the abbreviation for C # make and is an automated build system based on C # DSL. It can be used to compile code, copy files and folders, run unit tests, compress files, build nuget packages, and more.

Familiar with the famous make of small partners, should already know cake is roughly what kind of tool, cake has the following several characteristics:

    1. Easy to write: Using C #-based DSL, it is easy to write automated scripts.
    2. Cross-platform: based on Roslyn and mono to compile our written automation script, so that it can run on the Windows,linux,mac.
    3. Reliable: can be built on their own machines, can also be built on a CI system like appveyor,teamcity,tfs,vsts or Jenkins, can be run in the same way.
    4. Rich toolset: Supports Msbuild,mstest,xunit,nunit,nuget,ilmerge,wix and SignTool and more, as well as support for rich plugins (Cake Addins).
    5. OpenSource: Based on MIT open Source (Cake on Github) and is a project supported by the. NET Foundation (Cake on Dotnet Foundation).
1. A simple Demo

I created a simple demo (Cake.demo) on GitHub, the following simple introduction, the project is very simple, a class library project, a test project, as follows:

1.1 Download the boot script

First download a PowerShell-based boot script file Build.ps1, this file is not required, you can directly call the cake script file), in the directory where the project is running: invoke-webrequest/http Cakebuild.net/download/bootstrapper/windows-outfile Build.ps1

Note: The current author is based on the Windows platform, the development environment is vs. Community:

Linux: CURL-LSFO build. SH http://Cakebuild.net/download/bootstrapper/linux

Mac: CURL-LSFO build. SH http://CAKEBUILD.NET/DOWNLOAD/BOOTSTRAPPER/OSX

1.2 Creating a cake script

Add a text file Build.cake (the file name is arbitrary, I am lazy to adjust the build.ps1 specified in the cake script file only.) Full file: Https://github.com/linianhui/cake.demo/blob/master/build.cake)

This step we use cake to build below our above Cake.demo this project. The contents of the file are as follows:

1///Args2var target = Argument ("Target","Default");345///Build task6 Task ("Build")7. Does (() =8{9 MSBuild ("./cake.demo.sln",Newmsbuildsettings{Ten verbosity = Verbosity.minimal11 12 }); 13 14 15 Task ( "default ) 16. Isdependenton ( "build "); 17 18 19 /// run Task20 runtarget (target);     

The script can be said to be very simple to understand, I believe that there is a C # foundation should be very easily understood.

    1. First, a parameter target is created (the name is the same as the parameter name specified in BUILD.PS1), and the default value is "Default".
    2. Second, a task named build was created, and the task called MSBuild to build our project.
    3. Then, a task named default was created, and it did nothing, just to identify that I relied on a task for build, meaning that the build task would run before the task of default was run.
    4. Finally, use Runtarget to run the task with the specified name.

OK, let's run BUILD.PS1 (for the first time it will download some of the files needed for cake, these files will be stored in the folder specified in Build.ps1, the Tools folder by default, you can take a look at what the BUILD.PS1 is doing). Hey, it's running wrong!

The reason is one of my demo.tests this project refers to the Xunit package, MSBuild did not correctly restore the NuGet package, this does not know that the calculation is not a bug, the author used cake MSBuild to build vs2015 project when there is no problem, The current environment only installs VS2017. But just as well, we'll use cake to write a task to restore the NuGet package.

< Span style= "color: #000000;" >1 /// NuGet task" restore-nuget-packages ) 3. Does (() =>4 {5 Nugetrestore ( "./cake.demo.sln  "); 6});             

Run once more:

This time it's ready.

Then we add a few more tasks in turn to clean up the build file:

1 Task ("clean")2     . Does (() =3{4     cleandirectories ("./src/*/bin") ); 5     Cleandirectories ("./test/*/bin"); 6 });

To run unit tests:

1 /// unit-test Task 2 Task ("unit-test")3     . Isdependenton ("build")4     . Does (() =5{6     XUnit2 ("./test/*/bin/*/*. Tests.dll"); 7 });

Then run it:

1.3 Build.ps1

In the previous we mentioned that this file is not a required file, but rather it is used to download the relevant files required by cake and to set the default cake configuration. Furthermore, we can use it to simplify the invocation of cake, such as several of the tasks listed above, and we can run our specified task by passing parameters to BUILD.PS1 (default is running the defaults). Like what:

Only the files generated by the build of the project are cleaned.

2. Summary

The C # developers are very easy to get started, thanks to cake-based C#DSL to write scripts that automate the build compared to writing PowerShell or other scripts.

The second cake is based on Roslyn and Mono, enabling it to provide cross-platform automation and to run on a variety of commonly used CI systems.

Furthermore, Cake provides a rich set of built-in tools and plugins (Cake Addins) to meet most of our automated build requirements, such as compiling, testing, packaging, deployment, and more.

For example, some days ago, one of my web demo projects (Https://github.com/linianhui/Ids3.demo) uses cake to deploy IIS sites.

3. References

Make

C # make

Cake Addins

Cake on Github

Cake on Dotnet Foundation

Cake.demo

http://cakebuild.net/docs/tutorials/getting-started

Http://cakebuild.net/docs/resources/videos

Https://github.com/cake-build/example

[Cake] 0.c#make Automated Construction-Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.