|
| 1 | +using System.Text; |
| 2 | + |
| 3 | +namespace Sigtran.NET.Core.Utilities; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Describes one maintained external peer lab runner command manifest entry. |
| 7 | +/// </summary> |
| 8 | +public sealed class SigtranMaintainedPeerLabRunnerCommandEntry |
| 9 | +{ |
| 10 | + private readonly string[] _expectedOutputPaths; |
| 11 | + |
| 12 | + /// <summary>Creates a maintained peer lab runner command entry.</summary> |
| 13 | + /// <param name="sequence">The one-based execution sequence.</param> |
| 14 | + /// <param name="command">The command contract.</param> |
| 15 | + /// <param name="expectedOutputPaths">The expected output paths.</param> |
| 16 | + public SigtranMaintainedPeerLabRunnerCommandEntry( |
| 17 | + int sequence, |
| 18 | + SigtranMaintainedPeerLabCommand command, |
| 19 | + IReadOnlyList<string> expectedOutputPaths) |
| 20 | + { |
| 21 | + ArgumentNullException.ThrowIfNull(command); |
| 22 | + ArgumentNullException.ThrowIfNull(expectedOutputPaths); |
| 23 | + Sequence = sequence <= 0 ? throw new ArgumentOutOfRangeException(nameof(sequence)) : sequence; |
| 24 | + Command = command; |
| 25 | + _expectedOutputPaths = expectedOutputPaths.ToArray(); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary>The one-based execution sequence.</summary> |
| 29 | + public int Sequence { get; } |
| 30 | + |
| 31 | + /// <summary>The command contract.</summary> |
| 32 | + public SigtranMaintainedPeerLabCommand Command { get; } |
| 33 | + |
| 34 | + /// <summary>The expected output paths.</summary> |
| 35 | + public IReadOnlyList<string> ExpectedOutputPaths => _expectedOutputPaths.ToArray(); |
| 36 | + |
| 37 | + /// <summary>Whether this entry has output mappings for every expected artifact kind.</summary> |
| 38 | + public bool CoversExpectedArtifacts => Command.ExpectedArtifactKinds.Count == _expectedOutputPaths.Length; |
| 39 | + |
| 40 | + /// <summary>Formats a compact command entry summary.</summary> |
| 41 | + /// <returns>The command entry summary.</returns> |
| 42 | + public string Describe() |
| 43 | + { |
| 44 | + return $"sequence={Sequence} kind={Command.Kind} outputs={_expectedOutputPaths.Length}"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// <summary> |
| 49 | +/// Describes an executable command manifest for a maintained external peer lab runner. |
| 50 | +/// </summary> |
| 51 | +public sealed class SigtranMaintainedPeerLabRunnerCommandManifest |
| 52 | +{ |
| 53 | + private readonly SigtranMaintainedPeerLabRunnerCommandEntry[] _commands; |
| 54 | + |
| 55 | + /// <summary>Creates a maintained peer lab runner command manifest.</summary> |
| 56 | + /// <param name="inputBundle">The runner input bundle.</param> |
| 57 | + /// <param name="artifactPlan">The runner artifact materialization plan.</param> |
| 58 | + /// <param name="preflightReport">The runner preflight report.</param> |
| 59 | + /// <param name="commands">The ordered command entries.</param> |
| 60 | + public SigtranMaintainedPeerLabRunnerCommandManifest( |
| 61 | + SigtranMaintainedPeerLabRunnerInputBundle inputBundle, |
| 62 | + SigtranMaintainedPeerLabRunnerArtifactMaterializationPlan artifactPlan, |
| 63 | + SigtranMaintainedPeerLabRunnerPreflightReport preflightReport, |
| 64 | + IReadOnlyList<SigtranMaintainedPeerLabRunnerCommandEntry> commands) |
| 65 | + { |
| 66 | + ArgumentNullException.ThrowIfNull(inputBundle); |
| 67 | + ArgumentNullException.ThrowIfNull(artifactPlan); |
| 68 | + ArgumentNullException.ThrowIfNull(preflightReport); |
| 69 | + ArgumentNullException.ThrowIfNull(commands); |
| 70 | + |
| 71 | + InputBundle = inputBundle; |
| 72 | + ArtifactPlan = artifactPlan; |
| 73 | + PreflightReport = preflightReport; |
| 74 | + _commands = commands.Count == 0 ? throw new ArgumentException("At least one runner command is required.", nameof(commands)) : commands.ToArray(); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary>The runner input bundle.</summary> |
| 78 | + public SigtranMaintainedPeerLabRunnerInputBundle InputBundle { get; } |
| 79 | + |
| 80 | + /// <summary>The runner artifact materialization plan.</summary> |
| 81 | + public SigtranMaintainedPeerLabRunnerArtifactMaterializationPlan ArtifactPlan { get; } |
| 82 | + |
| 83 | + /// <summary>The runner preflight report.</summary> |
| 84 | + public SigtranMaintainedPeerLabRunnerPreflightReport PreflightReport { get; } |
| 85 | + |
| 86 | + /// <summary>The ordered command entries.</summary> |
| 87 | + public IReadOnlyList<SigtranMaintainedPeerLabRunnerCommandEntry> Commands => _commands.ToArray(); |
| 88 | + |
| 89 | + /// <summary>Whether command entries follow a contiguous one-based sequence.</summary> |
| 90 | + public bool HasContiguousSequence => _commands.Select(static command => command.Sequence).Order().SequenceEqual(Enumerable.Range(1, _commands.Length)); |
| 91 | + |
| 92 | + /// <summary>Whether every command entry has expected artifact mappings.</summary> |
| 93 | + public bool CoversExpectedArtifacts => _commands.All(static command => command.CoversExpectedArtifacts); |
| 94 | + |
| 95 | + /// <summary>Whether the command manifest is ready for runner execution.</summary> |
| 96 | + public bool IsExecutionReady => InputBundle.IsMaterializationReady |
| 97 | + && ArtifactPlan.IsMaterializationReady |
| 98 | + && PreflightReport.Ready |
| 99 | + && HasContiguousSequence |
| 100 | + && CoversExpectedArtifacts; |
| 101 | + |
| 102 | + /// <summary>Renders a Markdown command manifest.</summary> |
| 103 | + /// <returns>The Markdown command manifest.</returns> |
| 104 | + public string RenderMarkdown() |
| 105 | + { |
| 106 | + StringBuilder builder = new(); |
| 107 | + builder.AppendLine("# Maintained Peer Lab Runner Command Manifest"); |
| 108 | + builder.AppendLine(); |
| 109 | + builder.AppendLine($"Run: `{InputBundle.Workspace.RunManifest.RunId}`"); |
| 110 | + builder.AppendLine($"Ready: `{IsExecutionReady}`"); |
| 111 | + builder.AppendLine(); |
| 112 | + builder.AppendLine("## Commands"); |
| 113 | + |
| 114 | + foreach (SigtranMaintainedPeerLabRunnerCommandEntry entry in _commands.OrderBy(static command => command.Sequence)) |
| 115 | + { |
| 116 | + builder.Append("- "); |
| 117 | + builder.Append(entry.Sequence); |
| 118 | + builder.Append(". "); |
| 119 | + builder.Append(entry.Command.Name); |
| 120 | + builder.Append(" -> "); |
| 121 | + builder.AppendLine(string.Join(", ", entry.ExpectedOutputPaths)); |
| 122 | + } |
| 123 | + |
| 124 | + return builder.ToString(); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary>Formats a compact command manifest summary.</summary> |
| 128 | + /// <returns>The command manifest summary.</returns> |
| 129 | + public string Describe() |
| 130 | + { |
| 131 | + return $"run={InputBundle.Workspace.RunManifest.RunId} commands={_commands.Length} sequence={HasContiguousSequence} artifacts={CoversExpectedArtifacts} ready={IsExecutionReady}"; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/// <summary> |
| 136 | +/// Provides maintained external peer lab runner command manifest helpers. |
| 137 | +/// </summary> |
| 138 | +public static class SigtranMaintainedPeerLabRunnerCommandManifests |
| 139 | +{ |
| 140 | + /// <summary>Creates a command manifest from runner input, artifact, and preflight contracts.</summary> |
| 141 | + /// <param name="inputBundle">The runner input bundle.</param> |
| 142 | + /// <param name="artifactPlan">The runner artifact materialization plan.</param> |
| 143 | + /// <param name="preflightReport">The runner preflight report.</param> |
| 144 | + /// <returns>The runner command manifest.</returns> |
| 145 | + public static SigtranMaintainedPeerLabRunnerCommandManifest Create( |
| 146 | + SigtranMaintainedPeerLabRunnerInputBundle inputBundle, |
| 147 | + SigtranMaintainedPeerLabRunnerArtifactMaterializationPlan artifactPlan, |
| 148 | + SigtranMaintainedPeerLabRunnerPreflightReport preflightReport) |
| 149 | + { |
| 150 | + ArgumentNullException.ThrowIfNull(inputBundle); |
| 151 | + ArgumentNullException.ThrowIfNull(artifactPlan); |
| 152 | + ArgumentNullException.ThrowIfNull(preflightReport); |
| 153 | + |
| 154 | + int sequence = 1; |
| 155 | + List<SigtranMaintainedPeerLabRunnerCommandEntry> commands = []; |
| 156 | + foreach (SigtranMaintainedPeerLabCommand command in inputBundle.Workspace.RunManifest.CommandPlan.Commands) |
| 157 | + { |
| 158 | + IReadOnlyList<string> outputs = artifactPlan.Outputs |
| 159 | + .Where(output => command.ExpectedArtifactKinds.Contains(output.Kind)) |
| 160 | + .Select(static output => output.Path) |
| 161 | + .ToArray(); |
| 162 | + commands.Add(new(sequence, command, outputs)); |
| 163 | + sequence++; |
| 164 | + } |
| 165 | + |
| 166 | + return new(inputBundle, artifactPlan, preflightReport, commands); |
| 167 | + } |
| 168 | +} |
0 commit comments