| 1 | #nullable enable |
| 2 | using System.Diagnostics; |
| 3 | using CascadeIDE.Contracts; |
| 4 | |
| 5 | namespace CascadeIDE.Features.Build.DataAcquisition; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// DAL: однократный запуск <c>dotnet --version</c> для индикаторов готовности окружения. |
| 9 | /// </summary> |
| 10 | [IoBoundary] |
| 11 | public static class DotnetSdkVersionProbe |
| 12 | { |
| 13 | public static async Task<DotnetSdkVersionProbeResult> RunAsync(CancellationToken cancellationToken = default) |
| 14 | { |
| 15 | try |
| 16 | { |
| 17 | var psi = new ProcessStartInfo("dotnet") |
| 18 | { |
| 19 | Arguments = "--version", |
| 20 | RedirectStandardOutput = true, |
| 21 | RedirectStandardError = true, |
| 22 | UseShellExecute = false, |
| 23 | CreateNoWindow = true, |
| 24 | }; |
| 25 | |
| 26 | using var process = Process.Start(psi); |
| 27 | if (process is null) |
| 28 | return new DotnetSdkVersionProbeResult(DotnetSdkVersionProbeOutcome.ProcessNull, null, 0, "", null); |
| 29 | |
| 30 | var outTask = process.StandardOutput.ReadToEndAsync(cancellationToken); |
| 31 | var errTask = process.StandardError.ReadToEndAsync(cancellationToken); |
| 32 | await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); |
| 33 | var ver = (await outTask.ConfigureAwait(false)).Trim(); |
| 34 | var err = (await errTask.ConfigureAwait(false)).Trim(); |
| 35 | |
| 36 | if (process.ExitCode == 0 && !string.IsNullOrWhiteSpace(ver)) |
| 37 | return new DotnetSdkVersionProbeResult(DotnetSdkVersionProbeOutcome.Success, ver, 0, err, null); |
| 38 | |
| 39 | return new DotnetSdkVersionProbeResult( |
| 40 | DotnetSdkVersionProbeOutcome.NonZeroExit, |
| 41 | null, |
| 42 | process.ExitCode, |
| 43 | err, |
| 44 | null); |
| 45 | } |
| 46 | catch (OperationCanceledException) |
| 47 | { |
| 48 | throw; |
| 49 | } |
| 50 | catch (Exception ex) |
| 51 | { |
| 52 | return new DotnetSdkVersionProbeResult(DotnetSdkVersionProbeOutcome.Exception, null, 0, "", ex.Message); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public enum DotnetSdkVersionProbeOutcome |
| 58 | { |
| 59 | ProcessNull, |
| 60 | Success, |
| 61 | NonZeroExit, |
| 62 | Exception, |
| 63 | } |
| 64 | |
| 65 | public readonly record struct DotnetSdkVersionProbeResult( |
| 66 | DotnetSdkVersionProbeOutcome Outcome, |
| 67 | string? Version, |
| 68 | int ExitCode, |
| 69 | string StdErr, |
| 70 | string? ExceptionMessage); |
| 71 | |