From 00d86457701894c25e0181be3a7158d2b8db4130 Mon Sep 17 00:00:00 2001 From: Ikiru Yoshizaki <3856350+guitarrapc@users.noreply.github.com> Date: Tue, 27 Jun 2023 12:37:31 +0900 Subject: [PATCH 1/3] doc: update README --- README.md | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 83aacb4..06a35f6 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,14 @@ Windows Unity BatchBuild not provide Unity Build log StdOut option. This small t # Installation -You can install as .NET Global Tool. +You can install as .NET Global Tool or .NET Tool. ```sh +# install as global tool dotnet tool install -g UnityBuildRunner + +# install to project's .config +dotnet tool install UnityBuildRunner ``` Or, you can install as nuget package library. @@ -31,6 +35,8 @@ Install-Package UnityBuildRunner.Core ## CLI (Help) +You can run installed tool via `UnityBuildRunner` (.NET Global Tool) or `dotnet UnityBuildRunner` (.NET Tool) command. + ``` $ UnityBuildRunner --help Usage: UnityBuildRunner [options...] @@ -53,25 +59,35 @@ All you need to do is pass unity's path as `-u UnityPath` and leave other argmen If you are running Unity batch build like this. ```sh - "C:\Program Files\Unity\Hub\Editor\2022.3.3f1\Editor\Unity.exe" -quit -batchmode -buildTarget "WindowsStoreApps" -projectPath "C:\git\MRTKSample\Unity" -logfile "log.log" -executeMethod HoloToolkit.Unity.HoloToolkitCommands.BuildSLN" + $ "C:\Program Files\Unity\Hub\Editor\2022.3.3f1\Editor\Unity.exe" -quit -batchmode -buildTarget "WindowsStoreApps" -projectPath "C:\git\MRTKSample\Unity" -logfile "log.log" -executeMethod "HoloToolkit.Unity.HoloToolkitCommands.BuildSLN" ``` -Then, append `UnityBuildRunner --unity-path ` to existing command, that's all. +Then, append `UnityBuildRunner --unity-path ` or `dotnet UnityBuildRunner --unity-path ` to existing command, that's all. ```sh - UnityBuildRunner --unity-path "C:\Program Files\Unity\Hub\Editor\2022.3.3f1\Editor\Unity.exe" -quit -batchmode -buildTarget "WindowsStoreApps" -projectPath "C:\git\MRTKSample\Unity" -logfile "log.log" -executeMethod HoloToolkit.Unity.HoloToolkitCommands.BuildSLN" + # .NET Global Tool + $ UnityBuildRunner --unity-path "C:\Program Files\Unity\Hub\Editor\2022.3.3f1\Editor\Unity.exe" -quit -batchmode -buildTarget "WindowsStoreApps" -projectPath "C:\git\MRTKSample\Unity" -logfile "log.log" -executeMethod "HoloToolkit.Unity.HoloToolkitCommands.BuildSLN" + + # .NET Tool + $ UnityBuildRunner --unity-path "C:\Program Files\Unity\Hub\Editor\2022.3.3f1\Editor\Unity.exe" -quit -batchmode -buildTarget "WindowsStoreApps" -projectPath "C:\git\MRTKSample\Unity" -logfile "log.log" -executeMethod "HoloToolkit.Unity.HoloToolkitCommands.BuildSLN" ``` > **Note**: Another way to specifying UnityPath is via Environment Variable `UnityPath`. ```sh - set UnityPath=C:\Program Files\Unity\Hub\Editor\2022.3.3f1\Editor\Unity.exe - UnityBuildRunner -quit -batchmode -buildTarget "WindowsStoreApps" -projectPath "C:\git\MRTKSample\Unity" -logfile "log.log" -executeMethod "HoloToolkit.Unity.HoloToolkitCommands.BuildSLN" + # Environment Variables + $ set UnityPath=C:\Program Files\Unity\Hub\Editor\2022.3.3f1\Editor\Unity.exe + + # .NET Global Tool + $ UnityBuildRunner -quit -batchmode -buildTarget "WindowsStoreApps" -projectPath "C:\git\MRTKSample\Unity" -logfile "log.log" -executeMethod "HoloToolkit.Unity.HoloToolkitCommands.BuildSLN" + + # .NET Tool + $ dotnet UnityBuildRunner -quit -batchmode -buildTarget "WindowsStoreApps" -projectPath "C:\git\MRTKSample\Unity" -logfile "log.log" -executeMethod "HoloToolkit.Unity.HoloToolkitCommands.BuildSLN" ``` ## Library (Basic) -You can use this library as your tool chain. +You can use as Library as well. This is sample code to run Unity Build. ```csharp // Parse settings from argument From 7440d83eafcb0f20a4262255bea06e506e15a481 Mon Sep 17 00:00:00 2001 From: Ikiru Yoshizaki <3856350+guitarrapc@users.noreply.github.com> Date: Tue, 27 Jun 2023 12:45:51 +0900 Subject: [PATCH 2/3] feat: remove IBuilder.InitializeAsync --- src/UnityBuildRunner.Core/DefaultBuilder.cs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/UnityBuildRunner.Core/DefaultBuilder.cs b/src/UnityBuildRunner.Core/DefaultBuilder.cs index 74637d1..a7b8218 100644 --- a/src/UnityBuildRunner.Core/DefaultBuilder.cs +++ b/src/UnityBuildRunner.Core/DefaultBuilder.cs @@ -14,13 +14,6 @@ public interface IBuilder /// public int ExitCode { get; } /// - /// Initialize Builder before build. - /// - /// - /// - /// - Task InitializeAsync(string logFilePath, CancellationToken ct); - /// /// Run build. /// /// @@ -32,7 +25,7 @@ public class DefaultBuilder : IBuilder { private readonly ISettings settings; private readonly ILogger logger; - private readonly IErrorFilter? errorFilter; + private readonly IErrorFilter errorFilter; private BuildErrorCode buildErrorCode = BuildErrorCode.Success; public int ExitCode { get; private set; } @@ -106,13 +99,13 @@ public async Task BuildAsync(CancellationToken ct = default) // read logs and redirect to stdout while (!process.HasExited) { - ReadLog(reader); + ReadAndFilterLog(reader, errorFilter); await Task.Delay(TimeSpan.FromMilliseconds(500), ct).ConfigureAwait(false); } // read last log await Task.Delay(TimeSpan.FromMilliseconds(500), ct).ConfigureAwait(false); - ReadLog(reader); + ReadAndFilterLog(reader, errorFilter); } // respect unity's exitcode @@ -170,7 +163,7 @@ public async Task BuildAsync(CancellationToken ct = default) } } - public async Task InitializeAsync(string logFilePath, CancellationToken ct) + private async Task InitializeAsync(string logFilePath, CancellationToken ct) { await AssumeLogFileInitialized(logFilePath, ct).ConfigureAwait(false); } @@ -210,7 +203,7 @@ private async Task AssumeLogFileInitialized(string logFilePath, CancellationToke } } - private void ReadLog(StreamReader reader) + private void ReadAndFilterLog(StreamReader reader, IErrorFilter errorFilter) { var txt = reader.ReadToEnd(); if (string.IsNullOrEmpty(txt)) @@ -221,7 +214,7 @@ private void ReadLog(StreamReader reader) // Output current log. logger.LogInformation(txt); - // Cancel when error message found. - errorFilter?.Filter(txt, result => throw new BuildErrorFoundException($"Error filter caught error.", result.MatchPattern, result.MatchPattern)); + // Exception when error message found. + errorFilter.Filter(txt, result => throw new BuildErrorFoundException($"Error filter caught error.", result.MatchPattern, result.MatchPattern)); } } From cf3378076eb2930dfaa1c424b319c92270b78ad7 Mon Sep 17 00:00:00 2001 From: Ikiru Yoshizaki <3856350+guitarrapc@users.noreply.github.com> Date: Tue, 27 Jun 2023 12:48:49 +0900 Subject: [PATCH 3/3] fix: Propagate OperationCancelledException on InitializeAsync --- src/UnityBuildRunner.Core/DefaultBuilder.cs | 35 +++++++++------------ 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/UnityBuildRunner.Core/DefaultBuilder.cs b/src/UnityBuildRunner.Core/DefaultBuilder.cs index a7b8218..faf6ba3 100644 --- a/src/UnityBuildRunner.Core/DefaultBuilder.cs +++ b/src/UnityBuildRunner.Core/DefaultBuilder.cs @@ -175,32 +175,27 @@ private async Task InitializeAsync(string logFilePath, CancellationToken ct) /// private async Task AssumeLogFileInitialized(string logFilePath, CancellationToken ct) { - try + if (!File.Exists(logFilePath)) + { + return; + } + var retry = 10; // retry 10 times + for (var i = 1; i <= retry; i++) { - if (!File.Exists(logFilePath)) + ct.ThrowIfCancellationRequested(); + + try { - return; + File.Delete(logFilePath); + break; } - var retry = 10; // retry 10 times - for (var i = 1; i <= retry; i++) + catch (IOException) when (i < retry + 1) { - try - { - File.Delete(logFilePath); - break; - } - catch (IOException) when (i < retry + 1) - { - logger.LogWarning($"Couldn't delete file {logFilePath}, retrying... ({i + 1}/{retry})"); - await Task.Delay(TimeSpan.FromSeconds(1), ct).ConfigureAwait(false); - continue; - } + logger.LogWarning($"Couldn't delete file {logFilePath}, retrying... ({i + 1}/{retry})"); + await Task.Delay(TimeSpan.FromSeconds(1), ct).ConfigureAwait(false); + continue; } } - catch (OperationCanceledException) - { - // no error on CancellationToken cancel. - } } private void ReadAndFilterLog(StreamReader reader, IErrorFilter errorFilter)