|
| 1 | +using System.Text; |
| 2 | + |
| 3 | +namespace Sigtran.NET.Core.Utilities; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Describes a rendered maintained external peer lab command script. |
| 7 | +/// </summary> |
| 8 | +public sealed class SigtranMaintainedPeerLabCommandScript |
| 9 | +{ |
| 10 | + /// <summary>Creates a maintained external peer lab command script.</summary> |
| 11 | + /// <param name="runManifest">The run manifest.</param> |
| 12 | + /// <param name="environmentFilePath">The environment file path sourced by the script.</param> |
| 13 | + public SigtranMaintainedPeerLabCommandScript( |
| 14 | + SigtranMaintainedPeerLabRunManifest runManifest, |
| 15 | + string environmentFilePath) |
| 16 | + { |
| 17 | + ArgumentNullException.ThrowIfNull(runManifest); |
| 18 | + RunManifest = runManifest; |
| 19 | + EnvironmentFilePath = string.IsNullOrWhiteSpace(environmentFilePath) |
| 20 | + ? throw new ArgumentException("Environment file path is required.", nameof(environmentFilePath)) |
| 21 | + : environmentFilePath; |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary>The run manifest.</summary> |
| 25 | + public SigtranMaintainedPeerLabRunManifest RunManifest { get; } |
| 26 | + |
| 27 | + /// <summary>The environment file path sourced by the script.</summary> |
| 28 | + public string EnvironmentFilePath { get; } |
| 29 | + |
| 30 | + /// <summary>Renders the command script content.</summary> |
| 31 | + /// <returns>The command script content.</returns> |
| 32 | + public string Render() |
| 33 | + { |
| 34 | + StringBuilder builder = new(); |
| 35 | + builder.AppendLine("#!/usr/bin/env bash"); |
| 36 | + builder.AppendLine("set -euo pipefail"); |
| 37 | + builder.AppendLine(); |
| 38 | + builder.Append("source "); |
| 39 | + builder.AppendLine(Quote(EnvironmentFilePath)); |
| 40 | + builder.AppendLine(); |
| 41 | + |
| 42 | + foreach (SigtranMaintainedPeerLabCommand command in RunManifest.CommandPlan.Commands) |
| 43 | + { |
| 44 | + builder.Append("# "); |
| 45 | + builder.AppendLine(command.Describe()); |
| 46 | + builder.AppendLine(command.CommandLine); |
| 47 | + builder.AppendLine(); |
| 48 | + } |
| 49 | + |
| 50 | + return builder.ToString(); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary>Whether the rendered script covers every command in the manifest.</summary> |
| 54 | + public bool CoversCommandPlan => RunManifest.CommandPlan.Commands.All(command => Render().Contains(command.CommandLine, StringComparison.Ordinal)); |
| 55 | + |
| 56 | + /// <summary>Formats a compact command script summary.</summary> |
| 57 | + /// <returns>The command script summary.</returns> |
| 58 | + public string Describe() |
| 59 | + { |
| 60 | + return $"run={RunManifest.RunId} env={EnvironmentFilePath} commands={RunManifest.CommandPlan.Commands.Count} rendered={Render().Length}"; |
| 61 | + } |
| 62 | + |
| 63 | + private static string Quote(string value) |
| 64 | + { |
| 65 | + return "'" + value.Replace("'", "'\"'\"'", StringComparison.Ordinal) + "'"; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// <summary> |
| 70 | +/// Provides maintained external peer lab command script helpers. |
| 71 | +/// </summary> |
| 72 | +public static class SigtranMaintainedPeerLabCommandScripts |
| 73 | +{ |
| 74 | + /// <summary>Creates the default command script for a maintained peer lab run.</summary> |
| 75 | + /// <param name="runManifest">The run manifest.</param> |
| 76 | + /// <returns>The rendered command script model.</returns> |
| 77 | + public static SigtranMaintainedPeerLabCommandScript CreateDefault(SigtranMaintainedPeerLabRunManifest runManifest) |
| 78 | + { |
| 79 | + ArgumentNullException.ThrowIfNull(runManifest); |
| 80 | + string environmentFilePath = $"{runManifest.ArtifactPlan.ArtifactRoot}/config/{runManifest.RunId}-peer.env"; |
| 81 | + return new(runManifest, environmentFilePath); |
| 82 | + } |
| 83 | +} |
0 commit comments