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 diff --git a/src/UnityBuildRunner.Core/DefaultBuilder.cs b/src/UnityBuildRunner.Core/DefaultBuilder.cs index 74637d1..faf6ba3 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); } @@ -182,35 +175,30 @@ public 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 ReadLog(StreamReader reader) + private void ReadAndFilterLog(StreamReader reader, IErrorFilter errorFilter) { var txt = reader.ReadToEnd(); if (string.IsNullOrEmpty(txt)) @@ -221,7 +209,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)); } }