|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Cake.Core; |
| 4 | +using Cake.Core.Diagnostics; |
| 5 | +using Cake.Frosting; |
| 6 | +using Cake.Common; |
| 7 | +using Cake.Common.IO; |
| 8 | +using Cake.Common.Tools.DotNet; |
| 9 | +using Cake.Common.Tools.DotNet.Restore; |
| 10 | +using System.Threading; |
| 11 | +using System.Diagnostics; |
| 12 | +using Cake.Common.Tools.DotNet.Test; |
| 13 | +using Cake.Common.Tools.DotNet.Build; |
| 14 | + |
| 15 | +public static class Program |
| 16 | +{ |
| 17 | + public static int Main(string[] args) |
| 18 | + { |
| 19 | + return new CakeHost() |
| 20 | + .UseContext<BuildContext>() |
| 21 | + .Run(args); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +public class BuildContext : FrostingContext |
| 26 | +{ |
| 27 | + public const string BuildConfiguration = "Release"; |
| 28 | + |
| 29 | + public string Target { get; } |
| 30 | + public string SrcDirectoryPath { get; } |
| 31 | + public string NugetVersion { get; } |
| 32 | + public bool PushNuget { get; } |
| 33 | + public string NuGetPushToken { get; } |
| 34 | + public ProjectPaths ProjectPaths { get; } |
| 35 | + |
| 36 | + public BuildContext(ICakeContext context) |
| 37 | + : base(context) |
| 38 | + { |
| 39 | + Target = context.Argument("target", "Default"); |
| 40 | + SrcDirectoryPath = LoadParameter(context, "srcDirectoryPath"); |
| 41 | + NugetVersion = LoadParameter(context, "nugetVersion"); |
| 42 | + NuGetPushToken = LoadParameter(context, "nuGetPushToken"); |
| 43 | + PushNuget = context.Argument<bool>("pushNuget", false); |
| 44 | + |
| 45 | + ProjectPaths = ProjectPaths.LoadFromContext(context, BuildConfiguration, SrcDirectoryPath, NugetVersion); |
| 46 | + } |
| 47 | + |
| 48 | + private string LoadParameter(ICakeContext context, string parameterName) |
| 49 | + { |
| 50 | + return context.Arguments.GetArgument(parameterName) ?? throw new Exception($"Missing parameter '{parameterName}'"); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +[TaskName(nameof(OutputParametersTask))] |
| 55 | +public sealed class OutputParametersTask : FrostingTask<BuildContext> |
| 56 | +{ |
| 57 | + public override void Run(BuildContext context) |
| 58 | + { |
| 59 | + context.Log.Information($"INFO: Current Working Directory: {context.Environment.WorkingDirectory}"); |
| 60 | + |
| 61 | + context.Log.Information($"INFO: {nameof(context.SrcDirectoryPath)}: {context.SrcDirectoryPath}"); |
| 62 | + context.Log.Information($"INFO: {nameof(context.ProjectPaths)}.{nameof(context.ProjectPaths.ProjectName)}: {context.ProjectPaths.ProjectName}"); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +[IsDependentOn(typeof(OutputParametersTask))] |
| 67 | +[TaskName(nameof(BuildTask))] |
| 68 | +public sealed class BuildTask : FrostingTask<BuildContext> |
| 69 | +{ |
| 70 | + public override void Run(BuildContext context) |
| 71 | + { |
| 72 | + context.CleanDirectory(context.ProjectPaths.OutDir); |
| 73 | + |
| 74 | + BuildDotnetApp(context, context.ProjectPaths.PathToSln); |
| 75 | + TestDotnetApp(context, context.ProjectPaths.UnitTestProj); |
| 76 | + PackNugetPackage(context, context.ProjectPaths.OutDir, context.ProjectPaths.CsprojFile); |
| 77 | + } |
| 78 | + |
| 79 | + private void BuildDotnetApp(BuildContext context, string pathToSln) |
| 80 | + { |
| 81 | + context.DotNetRestore(pathToSln, new DotNetRestoreSettings { }); |
| 82 | + |
| 83 | + context.DotNetBuild(pathToSln, new DotNetBuildSettings |
| 84 | + { |
| 85 | + NoRestore = true, |
| 86 | + Configuration = BuildContext.BuildConfiguration |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + private void TestDotnetApp(BuildContext context, string pathToUnitTestProj) |
| 91 | + { |
| 92 | + var testSettings = new DotNetTestSettings() |
| 93 | + { |
| 94 | + Configuration = BuildContext.BuildConfiguration, |
| 95 | + NoBuild = true, |
| 96 | + ArgumentCustomization = (args) => args.Append("/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --logger trx") |
| 97 | + }; |
| 98 | + |
| 99 | + context.DotNetTest(pathToUnitTestProj, testSettings); |
| 100 | + } |
| 101 | + |
| 102 | + private void PackNugetPackage(BuildContext context, string outDir, string csprojFile) |
| 103 | + { |
| 104 | + context.DotNetPack(csprojFile, new Cake.Common.Tools.DotNet.Pack.DotNetPackSettings |
| 105 | + { |
| 106 | + IncludeSymbols = false, |
| 107 | + IncludeSource = false, |
| 108 | + NoBuild = true, |
| 109 | + Configuration = BuildContext.BuildConfiguration, |
| 110 | + OutputDirectory = outDir, |
| 111 | + VersionSuffix = context.NugetVersion, |
| 112 | + ArgumentCustomization = (args) => args.Append($"-p:PackageVersion={context.NugetVersion}") |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +[IsDependentOn(typeof(BuildTask))] |
| 118 | +[TaskName(nameof(NugetPushTask))] |
| 119 | +public sealed class NugetPushTask : FrostingTask<BuildContext> |
| 120 | +{ |
| 121 | + public override void Run(BuildContext context) |
| 122 | + { |
| 123 | + if (!context.PushNuget) |
| 124 | + { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + context.DotNetNuGetPush(context.ProjectPaths.NuGetFilePath, new Cake.Common.Tools.DotNet.NuGet.Push.DotNetNuGetPushSettings |
| 129 | + { |
| 130 | + Source = "https://api.nuget.org/v3/index.json", |
| 131 | + ApiKey = context.NuGetPushToken |
| 132 | + }); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +[TaskName("Default")] |
| 137 | +[IsDependentOn(typeof(NugetPushTask))] |
| 138 | +public class DefaultTask : FrostingTask |
| 139 | +{ |
| 140 | +} |
0 commit comments