首页 > 系统相关 >FlubuCore 用C#写cmd/shell脚本

FlubuCore 用C#写cmd/shell脚本

时间:2024-02-05 18:24:37浏览次数:29  
标签:shell FlubuCore C# dependTargets cmd ITarget context using public

1.在解决方案下新建项目BuildScript

2.在BuildScript项目下新建BuildScript.cs文件

 3.Nuget安装FlubuCore包

 4.BuildScript.cs内容

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using FlubuCore.Context;
using FlubuCore.Context.Attributes.BuildProperties;
using FlubuCore.Context.FluentInterface.Interfaces;
using FlubuCore.IO;
using FlubuCore.Scripting;
using FlubuCore.Tasks;

namespace BuildScript
{
    public class BuildScript : DefaultBuildScript
    {
        /// <summary>
        /// 解决方案文件名
        /// </summary>
        [SolutionFileName]
        public string SolutionFileName { get; set; } = "Navy.sln";
        /// <summary>
        /// 构建配置
        /// </summary>
        [FromArg("c|configuration")]
        [BuildConfiguration]
        public string BuildConfiguration { get; set; } = "Release";
        /// <summary>
        /// Nuget推送地址
        /// </summary>
        [FromArg("nugetUrl")]
        public string NugetUrl { get; set; } = "http://xxx:xxx/v3/index.json";
        /// <summary>
        /// Nuget密钥
        /// </summary>
        [FromArg("nugetKey", "Nuget api key for publishing nuget packages.")]
        public string NugetApiKey { get; set; } = "xxxxxxxxxxxxxxxxxxxxxxxx";
        /// <summary>
        /// 输出目录
        /// </summary>
        public FullPath PublishDir => RootDirectory.CombineWith("../publish");
        /// <summary>
        /// 项目文件列表
        /// </summary>
        public List<FileFullPath>? Projects { get; set; }
        /// <summary>
        /// 忽略的项目
        /// </summary>
        public List<string> IgnoreProjects = new List<string> { "BuildScript", "Navy.ConsoleTest" };
        /// <summary>
        /// 默认打印的日志级别
        /// </summary>
        public LogLevel DefaultLogLevel { get; set; } = LogLevel.Error;
        /// <summary>
        /// 构建前操作
        /// </summary>
        /// <param name="context">构建任务上下文</param>
        protected override void BeforeBuildExecution(ITaskContext context)
        {
            Projects = context.GetFiles(RootDirectory, "*/*.csproj");
            Projects.RemoveAll(p => IgnoreProjects.Contains(Path.GetFileNameWithoutExtension(p.FileName)));
            Projects.ForEach((project) =>
            {
                Console.WriteLine(Path.GetFileNameWithoutExtension(project.FileName));
            });
        }

        /// <summary>
        /// 配置任务项
        /// </summary>
        /// <param name="context"></param>
        protected override void ConfigureTargets(ITaskContext context)
        {
            var clean = Clean(context);
            var restore = Restore(context);
            var build = Build(context, clean, restore);
            var pack = Pack(context, build);
            PublishNuGetPackage(context, pack);
        }

        /// <summary>
        /// 清理解决方案
        /// </summary>
        private ITarget Clean(ITaskContext context)
        {
            return context.CreateTarget("clean")
                .SetDescription("Clean the solution.")
                .AddCoreTask(t => t.Clean().WithLogLevel(DefaultLogLevel));
        }

        /// <summary>
        /// 还原包
        /// </summary>
        private ITarget Restore(ITaskContext context, params ITarget[] dependTargets)
        {
            return context.CreateTarget("restore")
                .SetDescription("Restore the solution.")
                .DependsOn(dependTargets)
                .AddCoreTask(t => t.Restore().WithLogLevel(DefaultLogLevel));
        }

        /// <summary>
        /// 编译解决方案
        /// </summary>
        private ITarget Build(ITaskContext context, params ITarget[] dependTargets)
        {
            return context.CreateTarget("compile")
                .SetDescription("Compiles the solution.")
                .DependsOn(dependTargets)
                .AddCoreTask(t => t.Build().WithLogLevel(DefaultLogLevel));
        }

        /// <summary>
        /// 创建nuget包
        /// </summary>
        private ITarget Pack(ITaskContext context, params ITarget[] dependTargets)
        {
            return context.CreateTarget("pack")
                .SetDescription("Create nuget packages.")
                .DependsOn(dependTargets)
                .AddTask(x => x.DeleteDirectoryTask(PublishDir, false)) //删除发布目录
                .ForEach(Projects, (project, target) =>
                {
                    target.AddCoreTask(t => t.Pack()
                        .WithLogLevel(DefaultLogLevel)
                        .Project(project)
                        .OutputDirectory(PublishDir));
                });
        }

        /// <summary>
        /// 发布nuget包
        /// </summary>
        private void PublishNuGetPackage(ITaskContext context, params ITarget[] dependTargets)
        {
            context.CreateTarget("nuget.publish")
                .SetDescription("Publishes nuget packages")
                .DependsOn(dependTargets)
                .Do(t =>
                {
                    var packages = Directory.GetFiles(PublishDir, "*.nupkg");
                    foreach (var package in packages)
                    {
                        if (package.EndsWith("symbols.nupkg", StringComparison.OrdinalIgnoreCase))
                            continue;
                        context.CoreTasks().NugetPush(package)
                            .DoNotFailOnError(ex => { Console.WriteLine($"Failed to publish {package}.exception: {ex.Message}"); })
                            .ServerUrl(NugetUrl)
                            .ApiKey(NugetApiKey)
                            .Execute(context);
                    }
                });
        }
    }
}

5.安装全局命令行工具

dotnet tool install --global FlubuCore.GlobalTool

6.在根目录打开PowerShell执行

flubu nuget.publish

运行结果如下:

 

标签:shell,FlubuCore,C#,dependTargets,cmd,ITarget,context,using,public
From: https://www.cnblogs.com/whjblog/p/18008604

相关文章

  • C#中的checked和unchecked
    C#中checked和unchecked关键字checked在checked代码段中,如果里面执行的是比较危险的代码,则会引发相关异常或者错误,例如:staticvoidMain(string[]args){checked{intn=int.MaxValue;Console.WriteLine(n+1);}}Unhandledexception.S......
  • openWrt使用rclone挂载webDav
    前言觉得路由器(linux)硬盘太小,又不好扩展(x86机器可以插硬盘、但arm机器的硬盘是焊死的无法扩展)。这个时候,我们可以通过davfs或者rclone将外部资源如webDav挂载到本机上用来作为自己的硬盘。安装rclone#新版的rclone依赖fuse3,所系需要安装(尽管rclone的子依赖包含fuse但那是旧版......
  • C#中Thread和Task的区别
    https://blog.csdn.net/happyjava2/article/details/131411791Thread和Task是.NET框架中用于实现多线程编程的两个重要概念。它们的主要区别如下:1、基于不同的.NET框架:Thread是基于Windows操作系统提供的API实现,而Task则是基于.NET框架提供的TPL(TaskParallelL......
  • [BJDCTF2020]ZJCTF,不过如此
    [BJDCTF2020]ZJCTF,不过如此代码审计,在符合if的条件后,我们可以通过include函数包含想要查看的文件因此,首先需要传入两个参数text和file:text参数必须不为空,内容需要为Ihaveadream,file参数的内容则在后面的注释里提示我们为next.php,并且file参数里面不能包含/flag/,否则程序会......
  • 稳定编号系统 - 我的世界OCO指令系列
    最近在搞一些我的世界指令。其中有这么一个指令,需要编号系统,也就是uid。本指令需要引用的模块:玩家进入退出检测意义通常的编号系统中,当玩家进入或退出时,编号会从头重新分配。然而这样会导致编号不是很稳定。比如编号的一个用途是用来传送特定玩家。按照通常的编号分配......
  • C#多线程编程的Task(任务全面解析)
    原文链接:https://www.cnblogs.com/xietianjiao/p/7429742.htmlTask是.NET4.0加入的,跟线程池ThreadPool的功能类似,用Task开启新任务时,会从线程池中调用线程,而Thread每次实例化都会创建一个新的线程。 我们可以说Task是一种基于任务的编程模型。它与thread的主要区别是,它更加方便......
  • docker 启动jar包
    1、编写Dockerfile文件FROMopenjdk:8ENVTZ="Asia/Shanghai"ADDcim-gisportal.jarcim-gisportal.jarADDnohup.outnohup.outENTRYPOINT["nohup","java","-jar","/cim-gisportal.jar",">nohup.out&&quo......
  • Cucumber步骤中传Data Table作为参数
    引用链接:http://cukes.info/step-definitions.htmlDataTablesDataTablesarehandyforspecifyingalargerpieceofdata:Giventhefollowingusersexist:|name|email|phone||Aslak|[email protected]|123||Matt|[email protected]|2......
  • Cesium常用知识点
    构建四元数//地理位置坐标(三维)constposition=Cesium.Cartesian3.fromDegrees(-123.0744619,44.0503706,height);constheading=Cesium.Math.toRadians(135);//航向constpitch=0;//俯仰constroll=0;//横滚角consthpr=newCesium.Headi......
  • 【板子】网络流(Dinic)
    #include<bits/stdc++.h>usingnamespacestd;constintN=205;constintM=205;constintINF=0x3f3f3f3f;intedgeid=2;inthead[N];structedge{intv,w,nxt;}e[M*2];inlinevoidaddedge(intu,intv,intw){e[edgeid].v=v;e[ed......