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