From ca07b0734e88f636e21d5e279b1c583eb481dc89 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 14 Apr 2025 15:25:27 +0200 Subject: [PATCH] Improved the Rust compilation by showing the live output --- app/Build/Commands/UpdateMetadataCommands.cs | 45 ++++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/app/Build/Commands/UpdateMetadataCommands.cs b/app/Build/Commands/UpdateMetadataCommands.cs index 8a8aa9d3..64e05f71 100644 --- a/app/Build/Commands/UpdateMetadataCommands.cs +++ b/app/Build/Commands/UpdateMetadataCommands.cs @@ -5,6 +5,7 @@ using Build.Tools; namespace Build.Commands; +// ReSharper disable MemberCanBePrivate.Global // ReSharper disable ClassNeverInstantiated.Global // ReSharper disable UnusedType.Global // ReSharper disable UnusedMember.Global @@ -166,10 +167,10 @@ public sealed partial class UpdateMetadataCommands // Console.WriteLine("=============================="); - Console.Write("- Start building the Rust runtime ..."); + Console.WriteLine("- Start building the Rust runtime ..."); var pathRuntime = Environment.GetRustRuntimeDirectory(); - var rustBuildOutput = await this.ReadCommandOutput(pathRuntime, "cargo", "tauri build --bundles none"); + var rustBuildOutput = await this.ReadCommandOutput(pathRuntime, "cargo", "tauri build --bundles none", true); var rustBuildOutputLines = rustBuildOutput.Split([global::System.Environment.NewLine], StringSplitOptions.RemoveEmptyEntries); var foundRustIssue = false; foreach (var buildOutputLine in rustBuildOutputLines) @@ -192,7 +193,8 @@ public sealed partial class UpdateMetadataCommands Console.WriteLine(); else { - Console.WriteLine(" completed successfully."); + Console.WriteLine(); + Console.WriteLine("- Compilation completed successfully."); Console.WriteLine(); } } @@ -457,7 +459,7 @@ public sealed partial class UpdateMetadataCommands return matches; } - private async Task ReadCommandOutput(string workingDirectory, string program, string command) + private async Task ReadCommandOutput(string workingDirectory, string program, string command, bool showLiveOutput = false) { var processInfo = new ProcessStartInfo { @@ -467,21 +469,38 @@ public sealed partial class UpdateMetadataCommands RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, - CreateNoWindow = true + CreateNoWindow = true, }; - + + var sb = new StringBuilder(); using var process = new Process(); process.StartInfo = processInfo; process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + process.OutputDataReceived += (_, args) => + { + if(!string.IsNullOrWhiteSpace(args.Data)) + { + if(showLiveOutput) + Console.WriteLine(args.Data); + sb.AppendLine(args.Data); + } + }; + + process.ErrorDataReceived += (_, args) => + { + if(!string.IsNullOrWhiteSpace(args.Data)) + { + if(showLiveOutput) + Console.WriteLine(args.Data); + sb.AppendLine(args.Data); + } + }; - var output = await process.StandardOutput.ReadToEndAsync(); - var error = await process.StandardError.ReadToEndAsync(); await process.WaitForExitAsync(); - - return $""" - {output} - {error} - """; + return sb.ToString(); } private async Task IncreaseBuildNumber()