• Cake编译.Net项目


    介绍

    Cake 是.net平台下的一款自动化构建工具,可以完成对.net项目的编译,打包,运行单元测试,集成测试甚至发布项目等等.如果有些特征Cake没有实现,我们还可以很容易地通过扩展Cake来实现我们想要的功能.

    特点

    • 使用c#语言编写,可以在Cake脚本里使用C#语言来实现我们想要达到的功能,
    • 跨平台,可以运行在windows,linux 和macos上
    • 易于扩展,c#开发者很容易使用已有的c#语言知识对Cake进行扩展,甚至可以让Cake支持Java,Python等语言的构建
    • 易于和常见CI/CD平台结合,Cake很容易和常见的CI/CD平台,例如 Jenkins,AppVeyor, TeamCity, TFS, VSTS,Azure PipeLine等结合
    • 插件丰富,Cake官网和第三方开发者提供了丰富的Cake扩展工具,方便开箱即用

    官网

    https://cakebuild.net/

    示例演示

    1. 随意新建一个winform项目
    2. 在.sln所在目录新建文件build.cake
    #r "tools/CakeCommon.dll"  //引用自定义dll
    #tool "nuget:?package=7-Zip.CommandLine"
    #addin "nuget:?package=Cake.7zip" //插件
    #addin nuget:https://myget.org/f/Cake/?package=Cake.Incubator
    
    var Model = Argument("Model", "Release");//字段定义及默认值
    
    Setup(ctx =>
    {
        output=GetConfigurationValue("app_outputfolder");//自定义扩展方法CakeCommon.dll,获取配置文件信息
        EnsureDirectoryExists(new DirectoryPath(output));//判定文件夹是否存在,如果没有则创建
        Information($"开始构建{appName}");//打印输出
    });
    Teardown(ctx =>
    {
    	Information("构建完成!!!");
    });
    
    //编译winform
    Task("task1")
    .Does(() => {
    var appfile=GetConfigurationValue("app_projectpath");
    var buildSet1=new MSBuildSettings {
        Verbosity = Verbosity.Minimal,
        ToolVersion = MSBuildToolVersion.VS2019,
        Configuration = Model,
        PlatformTarget = PlatformTarget.x86
        };//编译配置
      	buildSet1.WithProperty("OutDir", output);//编译程序输出目录
    	MSBuild(appfile,buildSet1); 
    });
    //打包成一个压缩文件
    Task("task2")
    .IsDependentOn("task1")
    .Does(()=>{
        SevenZip(new SevenZipSettings
        {
            Command = new AddCommand
            {
                Files = new FilePathCollection(clientfiles),
                Archive = new FilePath("打包输出文件全路径")       
            }
        });
    });
    RunTarget("task2");
    
    1. 下载启动脚本build.ps1

    在项目build.cake所在目录下,打开powershell,然后执行以下命令Invoke-WebRequest http://cakebuild.net/download/bootstrapper/windows -OutFile build.ps1

    4.新建自定义Class Library,nuget Cake.core等相关包

    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Diagnostics;
    
    using Cake.Common.IO;
    using System.Runtime.Remoting.Contexts;
    using System.Threading.Tasks;
    
    namespace CakeCommon
    {
        public static class Client
        {
            #region 公共方法
    
            [CakeMethodAlias]
            public static string GetConfigurationValue(this ICakeContext content, string key)
            {
                return content.Configuration.GetValue(key);
            }
            #endregion 公共方法      
        }
    }
    
    

    5.在.sln同目录增加配置文件cake.config

    [app]
    name=这是一个测试
    outputfolder=D:cakebuild
    

    6.Powershell 执行build.ps1即可

    努力到无能为力,拼搏到感动自己
  • 相关阅读:
    java面向对象3
    java面向对象2
    java面向对象1
    java基础5
    java基础4
    java基础3
    递归之汉诺塔问题
    自定义 strcpy函数
    自定义strcmp函数
    自定义strcat函数
  • 原文地址:https://www.cnblogs.com/tianbang/p/15179164.html
Copyright © 2020-2023  润新知