Skip to content

Commit

Permalink
Merge pull request #72 from guitarrapc/feature/doc
Browse files Browse the repository at this point in the history
doc: update README to support .NET Tool
  • Loading branch information
guitarrapc committed Jun 27, 2023
2 parents bdd3fc6 + cf33780 commit c2d888f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 41 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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...]
Expand All @@ -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 <UnityPath>` to existing command, that's all.
Then, append `UnityBuildRunner --unity-path <UnityPath>` or `dotnet UnityBuildRunner --unity-path <UnityPath>` 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
Expand Down
56 changes: 22 additions & 34 deletions src/UnityBuildRunner.Core/DefaultBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ public interface IBuilder
/// </summary>
public int ExitCode { get; }
/// <summary>
/// Initialize Builder before build.
/// </summary>
/// <param name="logFilePath"></param>
/// <param name="ct"></param>
/// <returns></returns>
Task InitializeAsync(string logFilePath, CancellationToken ct);
/// <summary>
/// Run build.
/// </summary>
/// <param name="ct"></param>
Expand All @@ -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; }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -182,35 +175,30 @@ public async Task InitializeAsync(string logFilePath, CancellationToken ct)
/// <returns></returns>
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))
Expand All @@ -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));
}
}

0 comments on commit c2d888f

Please sign in to comment.