|
| 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 System.IO; |
| 9 | +using System.IO.Compression; |
| 10 | +using Cake.Common.Tools.DotNet; |
| 11 | +using Cake.Common.Tools.DotNet.Restore; |
| 12 | +using Cake.Npm; |
| 13 | +using Cake.Npm.RunScript; |
| 14 | +using System.Threading; |
| 15 | +using System.Linq; |
| 16 | +using Cake.Common.Tools.DotNet.Build; |
| 17 | +using Cake.Common.Tools.DotNet.Test; |
| 18 | +using Cake.Common.Tools.DotNet.Publish; |
| 19 | +using NuGet.Packaging; |
| 20 | +using System.Collections.Generic; |
| 21 | + |
| 22 | +public static class Program |
| 23 | +{ |
| 24 | + public static int Main(string[] args) |
| 25 | + { |
| 26 | + return new CakeHost() |
| 27 | + .UseContext<BuildContext>() |
| 28 | + .Run(args); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +public class BuildContext : FrostingContext |
| 33 | +{ |
| 34 | + public string Target { get; } |
| 35 | + public string BuildConfiguration { get; } |
| 36 | + public string SrcDirectoryPath { get; } |
| 37 | + public string BuildArtifactsPath { get; } |
| 38 | + public WebsitePaths WebClientPaths { get; } |
| 39 | + |
| 40 | + public BuildContext(ICakeContext context) |
| 41 | + : base(context) |
| 42 | + { |
| 43 | + Target = context.Argument("target", "Default"); |
| 44 | + BuildConfiguration = context.Argument<string>("configuration"); |
| 45 | + SrcDirectoryPath = context.Argument<string>("srcDirectoryPath"); |
| 46 | + BuildArtifactsPath = context.Argument<string>("BuildArtifactsPath"); |
| 47 | + |
| 48 | + WebClientPaths = WebsitePaths.LoadFromContext(context, BuildConfiguration, SrcDirectoryPath, BuildArtifactsPath); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +[TaskName(nameof(OutputParametersTask))] |
| 53 | +public sealed class OutputParametersTask : FrostingTask<BuildContext> |
| 54 | +{ |
| 55 | + public override void Run(BuildContext context) |
| 56 | + { |
| 57 | + context.Log.Information($"INFO: Current Working Directory: {context.Environment.WorkingDirectory}"); |
| 58 | + |
| 59 | + context.Log.Information($"INFO: {nameof(context.BuildConfiguration)}: {context.BuildConfiguration}"); |
| 60 | + context.Log.Information($"INFO: {nameof(context.SrcDirectoryPath)}: {context.SrcDirectoryPath}"); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +[IsDependentOn(typeof(OutputParametersTask))] |
| 65 | +[TaskName(nameof(CleanTask))] |
| 66 | +public sealed class CleanTask : FrostingTask<BuildContext> |
| 67 | +{ |
| 68 | + public override void Run(BuildContext context) |
| 69 | + { |
| 70 | + context.CleanDirectory(context.WebClientPaths.OutDir); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +[IsDependentOn(typeof(CleanTask))] |
| 75 | +[TaskName(nameof(BuildTask))] |
| 76 | +public sealed class BuildTask : FrostingTask<BuildContext> |
| 77 | +{ |
| 78 | + public override void Run(BuildContext context) |
| 79 | + { |
| 80 | + //Tasks to build custom NPM packages for public web app |
| 81 | + var buildCustomJsModulesWebAppFuncs = Directory |
| 82 | + .EnumerateDirectories(context.WebClientPaths.CustomJsModulesDir, "*", SearchOption.TopDirectoryOnly) |
| 83 | + .Select(x => new Action(() => PublishCustomJsModule(context, x))); |
| 84 | + |
| 85 | + //.NET Build tasks |
| 86 | + var buildDotnetAppFuncs = new[] |
| 87 | + { |
| 88 | + () => BuildDotnetApp(context, context.WebClientPaths.PathToSln), |
| 89 | + }; |
| 90 | + |
| 91 | + var buildFuncs = buildDotnetAppFuncs.Concat(buildCustomJsModulesWebAppFuncs).ToArray(); |
| 92 | + |
| 93 | + var runner = Parallel.ForEach(buildFuncs, func => func()); |
| 94 | + while (!runner.IsCompleted) |
| 95 | + { |
| 96 | + Thread.Sleep(100); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void BuildDotnetApp(BuildContext context, string pathToSln) |
| 101 | + { |
| 102 | + context.DotNetRestore(pathToSln); |
| 103 | + |
| 104 | + context.DotNetBuild(pathToSln, new DotNetBuildSettings |
| 105 | + { |
| 106 | + NoRestore = true, |
| 107 | + Configuration = context.BuildConfiguration |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + private void PublishCustomJsModule(BuildContext context, string workingDirectory) |
| 112 | + { |
| 113 | + var settings = new NpmRunScriptSettings |
| 114 | + { |
| 115 | + WorkingDirectory = workingDirectory, |
| 116 | + ScriptName = "publish" |
| 117 | + }; |
| 118 | + |
| 119 | + NpmRunScriptAliases.NpmRunScript(context, settings); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +[IsDependentOn(typeof(BuildTask))] |
| 124 | +[TaskName(nameof(RunUnitTestsTask))] |
| 125 | +public sealed class RunUnitTestsTask : FrostingTask<BuildContext> |
| 126 | +{ |
| 127 | + public override void Run(BuildContext context) |
| 128 | + { |
| 129 | + var testSettings = new DotNetTestSettings() |
| 130 | + { |
| 131 | + Configuration = context.BuildConfiguration, |
| 132 | + NoBuild = true, |
| 133 | + ArgumentCustomization = (args) => args.Append("/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --logger trx") |
| 134 | + }; |
| 135 | + |
| 136 | + var runTestsFuncs = new[] |
| 137 | + { |
| 138 | + () => context.DotNetTest(context.WebClientPaths.UnitTestProj, testSettings), |
| 139 | + }; |
| 140 | + |
| 141 | + var runner = Parallel.ForEach(runTestsFuncs, func => func()); |
| 142 | + while (!runner.IsCompleted) |
| 143 | + { |
| 144 | + Thread.Sleep(100); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +[IsDependentOn(typeof(RunUnitTestsTask))] |
| 150 | +[TaskName(nameof(PublishApplicationsTask))] |
| 151 | +public sealed class PublishApplicationsTask : FrostingTask<BuildContext> |
| 152 | +{ |
| 153 | + public override void Run(BuildContext context) |
| 154 | + { |
| 155 | + var buildFuncs = new List<Action> |
| 156 | + { |
| 157 | + () => PublishWebClient(context), |
| 158 | + }; |
| 159 | + |
| 160 | + var runner = Parallel.ForEach(buildFuncs, func => func()); |
| 161 | + while (!runner.IsCompleted) |
| 162 | + { |
| 163 | + Thread.Sleep(100); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private void PublishWebClient(BuildContext context) |
| 168 | + { |
| 169 | + var settings = new DotNetPublishSettings() |
| 170 | + { |
| 171 | + NoRestore = true, |
| 172 | + NoBuild = true, |
| 173 | + Configuration = context.BuildConfiguration, |
| 174 | + OutputDirectory = context.WebClientPaths.OutDir, |
| 175 | + }; |
| 176 | + |
| 177 | + context.DotNetPublish(context.WebClientPaths.CsprojFile, settings); |
| 178 | + |
| 179 | + //Now that the code is published, create the compressed folder |
| 180 | + if (!Directory.Exists(context.WebClientPaths.ZipOutDir)) |
| 181 | + { |
| 182 | + _ = Directory.CreateDirectory(context.WebClientPaths.ZipOutDir); |
| 183 | + } |
| 184 | + |
| 185 | + if (File.Exists(context.WebClientPaths.ZipOutFilePath)) |
| 186 | + { |
| 187 | + File.Delete(context.WebClientPaths.ZipOutFilePath); |
| 188 | + } |
| 189 | + |
| 190 | + ZipFile.CreateFromDirectory(context.WebClientPaths.OutDir, context.WebClientPaths.ZipOutFilePath); |
| 191 | + context.Log.Information($"Output client web app zip file to: {context.WebClientPaths.ZipOutFilePath}"); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +[IsDependentOn(typeof(PublishApplicationsTask))] |
| 196 | +[TaskName("Default")] |
| 197 | +public class DefaultTask : FrostingTask |
| 198 | +{ |
| 199 | +} |
0 commit comments