|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Statiq.App; |
| 7 | +using Statiq.Common; |
| 8 | +using Statiq.Core; |
| 9 | + |
| 10 | +namespace NetlifySharp.Build |
| 11 | +{ |
| 12 | + public class Program |
| 13 | + { |
| 14 | + // Pipeline names |
| 15 | + public const string Build = nameof(Build); |
| 16 | + public const string Test = nameof(Test); |
| 17 | + public const string Docs = nameof(Docs); |
| 18 | + public const string Pack = nameof(Pack); |
| 19 | + public const string Publish = nameof(Publish); |
| 20 | + |
| 21 | + public static async Task<int> Main(string[] args) => await Bootstrapper |
| 22 | + .CreateDefault(args, DefaultsToAdd.All & ~DefaultsToAdd.Commands) |
| 23 | + |
| 24 | + // Configure build settings for the correct version |
| 25 | + .ConfigureSettings(x => |
| 26 | + { |
| 27 | + string version = File.ReadAllLines("../../ReleaseNotes.md")[0].TrimStart('#').Trim(); |
| 28 | + if (x.ContainsKey("BUILD_BUILDNUMBER")) |
| 29 | + { |
| 30 | + version += "-build-" + x["BUILD_BUILDNUMBER"]; |
| 31 | + } |
| 32 | + x["BuildVersion"] = version; |
| 33 | + x["BuildProperties"] = $"-p:Version={version} -p:AssemblyVersion={version} -p:FileVersion={version}"; |
| 34 | + }) |
| 35 | + |
| 36 | + // Add build commands to the CLI |
| 37 | + .AddBuildCommand("build", "Builds all projects.", nameof(Build)) |
| 38 | + .AddBuildCommand("test", "Builds and tests all projects.", nameof(Test)) |
| 39 | + .AddBuildCommand("docs", "Previews the documentation.", nameof(Docs)) |
| 40 | + .AddBuildCommand("pack", "Packs the packages.", nameof(Pack)) |
| 41 | + .AddBuildCommand("publish", "Publishes the packages and documentation site.", nameof(Publish)) |
| 42 | + |
| 43 | + // Add pipelines |
| 44 | + .BuildPipeline(Build, pipeline => pipeline |
| 45 | + .ManuallyExecute() |
| 46 | + .WithProcessModules( |
| 47 | + new ReadFiles("../../../src/*/*.csproj"), |
| 48 | + new StartProcess( |
| 49 | + "dotnet", |
| 50 | + Config.FromDocument((doc, ctx) => $"build \"{doc.Source.FullPath}\" {ctx.Settings.GetString("BuildProperties")}")) |
| 51 | + .WithSequentialExecution() |
| 52 | + .LogOutput())) |
| 53 | + |
| 54 | + .BuildPipeline(Test, pipeline => pipeline |
| 55 | + .ManuallyExecute() |
| 56 | + .WithDependencies(Build) |
| 57 | + .WithProcessModules( |
| 58 | + new ReadFiles("../../../tests/*/*.csproj"), |
| 59 | + new StartProcess( |
| 60 | + "dotnet", |
| 61 | + Config.FromDocument((doc, ctx) => |
| 62 | + { |
| 63 | + StringBuilder builder = new StringBuilder($"test --no-build --no-restore \"{doc.Source.FullPath}\" {ctx.Settings.GetString("BuildProperties")} /p:CollectCoverage=true"); |
| 64 | + if (ctx.Settings.ContainsKey("BUILD_BUILDNUMBER")) |
| 65 | + { |
| 66 | + // We're in Azure Pipelines so add the test logger |
| 67 | + builder.Append(" --test-adapter-path:. --logger:AzurePipelines"); |
| 68 | + } |
| 69 | + return builder.ToString(); |
| 70 | + })) |
| 71 | + .WithSequentialExecution() |
| 72 | + .LogOutput())) |
| 73 | + |
| 74 | + .BuildPipeline(Pack, pipeline => pipeline |
| 75 | + .ManuallyExecute() |
| 76 | + .WithDependencies(Test) |
| 77 | + .WithProcessModules( |
| 78 | + new StartProcess( |
| 79 | + "dotnet", |
| 80 | + Config.FromContext(ctx => $"pack ../../src/NetlifySharp/NetlifySharp.csproj --no-build --no-restore -o \"{ctx.FileSystem.GetOutputDirectory("packages").Path}\" {ctx.Settings.GetString("BuildProperties")}")) |
| 81 | + .LogOutput(), |
| 82 | + new ReadFiles(Config.FromContext(ctx => $"{ctx.FileSystem.GetOutputDirectory("packages").Path}/*.nupkg")), |
| 83 | + new ExecuteIf(Config.FromContext(ctx => ctx.Settings.ContainsKey("DAVIDGLICK_CERTPASS") && ctx.FileSystem.GetRootFile("../../digicert-davidglick.pfx").Exists)) |
| 84 | + { |
| 85 | + new StartProcess( |
| 86 | + "nuget", |
| 87 | + Config.FromDocument((doc, ctx) => |
| 88 | + { |
| 89 | + string certPath = ctx.FileSystem.GetRootFile("../../digicert-davidglick.pfx").Path.FullPath; |
| 90 | + string password = ctx.Settings.GetString("DAVIDGLICK_CERTPASS"); |
| 91 | + return $"sign \"{doc.Source.FullPath}\" -CertificatePath \"{certPath}\" -CertificatePassword \"{password}\" -Timestamper \"http://timestamp.digicert.com\" -NonInteractive"; |
| 92 | + })) |
| 93 | + .LogOutput() |
| 94 | + })) |
| 95 | + |
| 96 | + // TODO: Generate zip |
| 97 | + // TODO: Generate GitHub release |
| 98 | + // TODO: Run docs generator with deployment |
| 99 | + .BuildPipeline(Publish, pipeline => pipeline |
| 100 | + .ManuallyExecute() |
| 101 | + .WithDependencies(Pack) |
| 102 | + .WithProcessModules( |
| 103 | + new ReadFiles(Config.FromContext(ctx => $"{ctx.FileSystem.GetOutputDirectory("packages").Path}/*.nupkg")), |
| 104 | + new StartProcess( |
| 105 | + "dotnet", |
| 106 | + Config.FromDocument((doc, ctx) => $"nuget push --api-key {ctx.Settings.GetString("NUGET_KEY")} --source \"https://api.nuget.org/v3/index.json\" \"{doc.Source.FullPath}\" ")) |
| 107 | + .WithSequentialExecution() |
| 108 | + .LogOutput())) |
| 109 | + |
| 110 | + // Run the app |
| 111 | + .RunAsync(); |
| 112 | + } |
| 113 | +} |
0 commit comments