Introduction cake
Cake is an automated under the .net platform build tool, you can compile for .net project, package, run unit testing, integration testing and even publish the project, and so on. If some features Cake
do not realize, we can also easily be extended by Cake to achieve our desired functionality.
Cake
It has the following characteristics
1) using c # language, can realize the function we want to achieve using the C # language in Cake script.
2) cross-platform, it can run on windows, linux and macos.
3) easy to expand, c # developers to easily use existing c#
knowledge of the language to Cake
be extended, and even allows Cake
to build support for Java, Python and other languages
4) easy and common CI / CD platform combination, Cake
it is easy and common CI / CD platforms, such as Jenkins
, AppVeyor
, TeamCity
, TFS
, VSTS
, Azure PipeLine
and other combination
5) Plug-rich, Cake official website and third-party developers to provide a rich Cake extension tool, easy out of the box.
Cake HelloWorld example of a new
In this section we explain by example how to create a HelloWorld Cake file, download startup scripts and how to install plug-ins Cake visualstudio
A. Create a new file Cake
We create a new .net project (can be a library, console or web project), where we create a console project, then the project sln
a new name under the directory build.cake
file.
Readers can create a text file, then the suffix to cake, the file name to build, so that can complete a
build.cake
creation of course can also executePowershell
commandsNew-Item build.cake
when build.cake to create a file using powershell create must pay attention open powershell project in the current directory, or file jointly built by the above command is not our path might expect.
Some readers may doubt this document is not necessarily terribly named
build.cake
, the answer is no, in fact, this file can take command, but as a first experience, it is strongly recommended that the reader should not command, but do follow explain, to avoid unnecessary trouble
II. Edit build.cake
File
After we create build.cake, the wording of this (or any other familiar editor to open it), add the following code
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
Setup(ctx =>
{
// Executed BEFORE the first task.
Information("Running tasks...");
});
Teardown(ctx =>
{
// Executed AFTER the last task.
Information("Finished running tasks.");
});
Task("Default")
.Does(() => {
Information("Hello World!");
});
RunTarget(target);
III. Download Startup Script
Cake
In the startup script for Windows build.ps1
(of course also be other names), is under linux and macosbuild.sh
Note that although the Cake startup script under linux macos and all
build.sh
but not exactly the same, different platforms to download the startup script for this platform.
Since I was running in the windows, so download windows platform corresponding startup scriptbuild.ps1
In the project build.cake
under the directory, we open powershell, and then execute the following command Invoke-WebRequest http://cakebuild.net/download/bootstrapper/windows -OutFile build.ps1
to download a startup script
linux platform to download: curl -Lsfo build.sh http://cakebuild.net/download/bootstrapper/linux
mac platform to download -Lsfo build.sh curl http://cakebuild.net/download/bootstrapper/osx
Note that must be in the current directory (the directory sln or other directory which you specify), if it is launched from the Start menu powershell, be sure to enter the target directory by cd command.
windows 10 can be in the top left corner click the folder
文件
to find powershell in the pop-up menu, so start powershell directory is in the current folder directory
IV. Implementation of building
We open in the current directory powershell, through powershell, powershell command window enter the .\build.ps1
building information ps1 script execution just downloaded, over a minute, we can see the output of
Five .build.cake file parsing
Early contact with build.cake
us it may be very strange, but do not be afraid, it is entirely written with c # language, but the method is customizable.
build.cake in a total of three tasks, Setup, TearDown and Default, which the Setup
task will be executed before the execution of all tasks, TearDown
the task will be executed after the execution of all tasks, much like Nunit
in the Setup and TearDown methods. There are times when we all want perform some initialization before the method execute, perform some tools after finishing all tasks executed, we can write the code in the appropriate Setup
and TearDown
where
Setup
, TearDown
And Default
where the reception is a commission, we can write your own code in the commission, the commission was due to the current parameters we did not use to, I will not explain. We only know its format on the line.
Setup
,TearDown
Two special tasks, called automatically by the cake, and Default tasks need to explicitly call to perform, we write other tasks that can mimic Default task.
Here, some children's shoes may be in doubt, these tasks can be completed by the script, why bother to use tool does, in fact, there are many disadvantages of using a script, we've introduced a complete .net web project in a test environment continuous integration scheme in which the dozens of scripts and tools, if our scripting is not precise enough often causes loss of production environment. and from the practical point of view, the number of script too often get out of hand. deployment script in the server many times become a hot potato, unfamiliar people neither want to see, do not want to change, more importantly, is not easily changed. it would not easily removed or deleted. Another problem is that the script can not be ignored is often behind the call tools, tool and have to rely on the operating environment. we may run well on a well-configured server, but change to replace the other servers often because of problems caused by various conditions is not satisfied, in the case of shortage so the problem is particularly prominent. development and operation and maintenance are miserable, the original intention of developing scripts tend to be in order to improve efficiency, knot Due to maintenance problems deepen the contradiction between development and operation and maintenance. The cake itself by managing all kinds of dependency, to a large extent solve the above problem.