| 1 | using System.Text.Json; |
| 2 | using DotNetBuildTest.Core; |
| 3 | |
| 4 | namespace CascadeIDE.BuildVerifyWorker; |
| 5 | |
| 6 | internal static class BuildVerifyWorkerOneShot |
| 7 | { |
| 8 | internal static async Task<int> RunAsync(string[] args) |
| 9 | { |
| 10 | if (args.Length < 2) |
| 11 | { |
| 12 | Console.Error.WriteLine( |
| 13 | "Usage: CascadeIDE.BuildVerifyWorker build <path> | test <path> [--filter <xunit filter>] | serve"); |
| 14 | return 2; |
| 15 | } |
| 16 | |
| 17 | var mode = args[0].Trim().ToLowerInvariant(); |
| 18 | var path = Path.GetFullPath(args[1].Trim()); |
| 19 | if (!File.Exists(path)) |
| 20 | { |
| 21 | Console.Error.WriteLine($"File not found: {path}"); |
| 22 | return 2; |
| 23 | } |
| 24 | |
| 25 | string? filter = null; |
| 26 | for (var i = 2; i < args.Length - 1; i++) |
| 27 | { |
| 28 | if (string.Equals(args[i], "--filter", StringComparison.OrdinalIgnoreCase)) |
| 29 | filter = args[i + 1]; |
| 30 | } |
| 31 | |
| 32 | var coordinator = new BuildTestJobCoordinator(); |
| 33 | |
| 34 | var dotnetOptions = mode == "test" && !string.IsNullOrWhiteSpace(filter) |
| 35 | ? DotnetExecutionOptions.Empty with { Filter = filter.Trim() } |
| 36 | : DotnetExecutionOptions.Empty; |
| 37 | |
| 38 | var kind = mode switch |
| 39 | { |
| 40 | "build" => BuildTestJobKind.BuildStructured, |
| 41 | "test" => BuildTestJobKind.RunTests, |
| 42 | _ => (BuildTestJobKind?)null, |
| 43 | }; |
| 44 | |
| 45 | if (kind is null) |
| 46 | { |
| 47 | Console.Error.WriteLine("Mode must be \"build\" or \"test\"."); |
| 48 | return 2; |
| 49 | } |
| 50 | |
| 51 | var timeout = kind == BuildTestJobKind.RunTests |
| 52 | ? BuildTestToolRequestParser.DefaultTestTimeoutSeconds |
| 53 | : BuildTestToolRequestParser.DefaultBuildTimeoutSeconds; |
| 54 | |
| 55 | var enqueued = coordinator.TryEnqueue(kind.Value, path, includeRawOutput: true, timeout, dotnetOptions); |
| 56 | if (!enqueued.Accepted) |
| 57 | { |
| 58 | Console.WriteLine( |
| 59 | BuildTestJson.Serialize(new |
| 60 | { |
| 61 | success = false, |
| 62 | message = "busy", |
| 63 | retry_after_seconds = enqueued.RetryAfterSeconds, |
| 64 | })); |
| 65 | return 11; |
| 66 | } |
| 67 | |
| 68 | var result = await coordinator.WaitForCompletionAsync(enqueued.JobId!, CancellationToken.None).ConfigureAwait(false) |
| 69 | ?? BuildTestJson.Serialize(new { success = false, message = "cancelled_or_missing" }); |
| 70 | |
| 71 | Console.WriteLine(result); |
| 72 | return JsonTryGetBool(result, "success", out var ok) && ok ? 0 : 1; |
| 73 | } |
| 74 | |
| 75 | private static bool JsonTryGetBool(string json, string prop, out bool value) |
| 76 | { |
| 77 | value = false; |
| 78 | try |
| 79 | { |
| 80 | using var doc = JsonDocument.Parse(json); |
| 81 | if (doc.RootElement.TryGetProperty(prop, out var el) |
| 82 | && (el.ValueKind == JsonValueKind.True || el.ValueKind == JsonValueKind.False)) |
| 83 | { |
| 84 | value = el.GetBoolean(); |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | catch (JsonException) |
| 89 | { |
| 90 | // |
| 91 | } |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |