Skip to content

Commit

Permalink
Changed file write process to asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
VeyronSakai committed Apr 27, 2024
1 parent 7ae3935 commit 02ca0c0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 62 deletions.
7 changes: 5 additions & 2 deletions Assets/GhaUnityBuildReporter/Editor/EditorQuitEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using System.Threading;
using System.Threading.Tasks;
using UnityEditor;

namespace GhaUnityBuildReporter.Editor
Expand All @@ -26,7 +28,7 @@ static EditorQuitEntryPoint()
EditorApplication.quitting += Quit;
}

private static void Quit()
private static async void Quit()
{
if (!ExecutesUnityBuild)
{
Expand All @@ -42,7 +44,8 @@ private static void Quit()

var jobSummaryRepository = new GitHubJobSummaryRepository(s_gitHubStepSummaryPath);
var useCase = new ReportUnityBuildUseCase(jobSummaryRepository, buildReport);
useCase.WriteAll();
var cancellationTokenSource = new CancellationTokenSource();
await useCase.WriteAllAsync(cancellationTokenSource.Token);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This software is released under the MIT License.

using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace GhaUnityBuildReporter.Editor
{
Expand All @@ -14,7 +16,7 @@ internal GitHubJobSummaryRepository(string gitHubStepSummaryPath)
_gitHubStepSummaryPath = gitHubStepSummaryPath;
}

public void AppendText(string text)
public async ValueTask AppendTextAsync(string text, CancellationToken cancellationToken)
{
var dir = Path.GetDirectoryName(_gitHubStepSummaryPath);
if (string.IsNullOrEmpty(dir))
Expand All @@ -27,7 +29,7 @@ public void AppendText(string text)
Directory.CreateDirectory(dir);
}

File.AppendAllText(_gitHubStepSummaryPath, text);
await File.AppendAllTextAsync(_gitHubStepSummaryPath, text, cancellationToken);
}
}
}
5 changes: 4 additions & 1 deletion Assets/GhaUnityBuildReporter/Editor/IJobSummaryRepository.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using System.Threading;
using System.Threading.Tasks;

namespace GhaUnityBuildReporter.Editor
{
internal interface IJobSummaryRepository
{
void AppendText(string text);
ValueTask AppendTextAsync(string text, CancellationToken cancellationToken);
}
}
122 changes: 65 additions & 57 deletions Assets/GhaUnityBuildReporter/Editor/ReportUnityBuildUseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UnityEditor.Build.Reporting;
using UnityEngine;

Expand All @@ -20,24 +22,25 @@ public ReportUnityBuildUseCase(IJobSummaryRepository jobSummaryRepository, Build
_buildReport = buildReport;
}

public void WriteAll()
public async ValueTask WriteAllAsync(CancellationToken cancellationToken)
{
WriteTitle();
WriteSummary();
WriteBuildStepsInfo();
WriteSourceAssetsInfo();
WriteOutputFilesInfo();
WriteIncludedModulesInfo();
await WriteTitleAsync(cancellationToken);
await WriteSummaryAsync(cancellationToken);
await WriteBuildStepsInfoAsync(cancellationToken);
await WriteSourceAssetsInfoAsync(cancellationToken);
await WriteOutputFilesInfoAsync(cancellationToken);
await WriteIncludedModulesInfoAsync(cancellationToken);
}

private void WriteTitle()
private async ValueTask WriteTitleAsync(CancellationToken cancellationToken)
{
_jobSummaryRepository.AppendText($"# Unity Build Report{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"# Unity Build Report{Environment.NewLine}",
cancellationToken);
}

private void WriteSummary()
private async ValueTask WriteSummaryAsync(CancellationToken cancellationToken)
{
_jobSummaryRepository.AppendText($"## Basic Info{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"## Basic Info{Environment.NewLine}", cancellationToken);

var summary = _buildReport.summary;

Expand All @@ -51,38 +54,35 @@ private void WriteSummary()
+ $"| Total Errors | {summary.totalErrors} |{Environment.NewLine}"
+ $"| Total Warnings | {summary.totalWarnings} |{Environment.NewLine}";

_jobSummaryRepository.AppendText(basicInfo);
await _jobSummaryRepository.AppendTextAsync(basicInfo, cancellationToken);
}

private void WriteBuildStepsInfo()
private async ValueTask WriteBuildStepsInfoAsync(CancellationToken cancellationToken)
{
if (_buildReport.steps.Length <= 0)
{
return;
}

_jobSummaryRepository.AppendText($"## Build Steps{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"## Build Steps{Environment.NewLine}", cancellationToken);

_jobSummaryRepository.AppendText(
$"<details><summary>Details</summary>{Environment.NewLine}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$"<details><summary>Details</summary>{Environment.NewLine}{Environment.NewLine}", cancellationToken);

foreach (var step in _buildReport.steps)
{
switch (step.depth)
{
case 0:
_jobSummaryRepository.AppendText(
$@"### {step.name} ({step.duration:hh\:mm\:ss\.fff}){Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$@"### {step.name} ({step.duration:hh\:mm\:ss\.fff}){Environment.NewLine}",
cancellationToken);
break;
case >= 1:
{
_jobSummaryRepository.AppendText(
$@"{new string(' ', (step.depth - 1) * 2)}- **{step.name}** ({step.duration:hh\:mm\:ss\.fff}){Environment.NewLine}");

if (step.messages.Length <= 0)
{
continue;
}
await _jobSummaryRepository.AppendTextAsync(
$@"{new string(' ', (step.depth - 1) * 2)}- **{step.name}** ({step.duration:hh\:mm\:ss\.fff}){Environment.NewLine}",
cancellationToken);

foreach (var message in step.messages)
{
Expand All @@ -96,26 +96,28 @@ private void WriteBuildStepsInfo()
_ => ":question:"
};

_jobSummaryRepository.AppendText(
$@"{new string(' ', step.depth * 2)}- {emoji} {message.content}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$@"{new string(' ', step.depth * 2)}- {emoji} {message.content}{Environment.NewLine}",
cancellationToken);
}

break;
}
}
}

_jobSummaryRepository.AppendText($"</details>{Environment.NewLine}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"</details>{Environment.NewLine}{Environment.NewLine}",
cancellationToken);
}

private void WriteSourceAssetsInfo()
private async ValueTask WriteSourceAssetsInfoAsync(CancellationToken cancellationToken)
{
if (!_buildReport.packedAssets.Any())
{
return;
}

_jobSummaryRepository.AppendText($"## Source Assets{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"## Source Assets{Environment.NewLine}", cancellationToken);

foreach (var packedAsset in _buildReport.packedAssets)
{
Expand All @@ -124,14 +126,16 @@ private void WriteSourceAssetsInfo()

var topAssets = packedAsset.contents.OrderByDescending(x => x.packedSize);

_jobSummaryRepository.AppendText(
$"### {packedAsset.shortPath} ({GetFormattedSize(totalSize)}){Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$"### {packedAsset.shortPath} ({GetFormattedSize(totalSize)}){Environment.NewLine}",
cancellationToken);

_jobSummaryRepository.AppendText(
$"<details><summary>Details</summary>{Environment.NewLine}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$"<details><summary>Details</summary>{Environment.NewLine}{Environment.NewLine}",
cancellationToken);

_jobSummaryRepository.AppendText(
$"| Asset | Size |{Environment.NewLine}| --- | --- |{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$"| Asset | Size |{Environment.NewLine}| --- | --- |{Environment.NewLine}", cancellationToken);

foreach (var assetInfo in topAssets)
{
Expand All @@ -142,71 +146,75 @@ private void WriteSourceAssetsInfo()
var assetDetails =
$"| {assetPath} | {GetFormattedSize(assetInfo.packedSize)} |{Environment.NewLine}";

_jobSummaryRepository.AppendText(assetDetails);
await _jobSummaryRepository.AppendTextAsync(assetDetails, cancellationToken);
}

_jobSummaryRepository.AppendText($"</details>{Environment.NewLine}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"</details>{Environment.NewLine}{Environment.NewLine}",
cancellationToken);
}
}

private void WriteOutputFilesInfo()
private async ValueTask WriteOutputFilesInfoAsync(CancellationToken cancellationToken)
{
var files = GetBuildFiles();
if (files.Length == 0)
{
return;
}

_jobSummaryRepository.AppendText($"## Output Files{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"## Output Files{Environment.NewLine}", cancellationToken);

_jobSummaryRepository.AppendText(
$"<details><summary>Details</summary>{Environment.NewLine}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$"<details><summary>Details</summary>{Environment.NewLine}{Environment.NewLine}", cancellationToken);

_jobSummaryRepository.AppendText($"| File | Size |{Environment.NewLine}"
+ $"| --- | --- |{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"| File | Size |{Environment.NewLine}"
+ $"| --- | --- |{Environment.NewLine}", cancellationToken);

var projectRootPath = Directory.GetParent(Application.dataPath)?.FullName;

foreach (var file in files)
{
var relativePath = Path.GetRelativePath(projectRootPath, file.path);
_jobSummaryRepository.AppendText(
$"| {relativePath} | {GetFormattedSize(file.size)} |{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$"| {relativePath} | {GetFormattedSize(file.size)} |{Environment.NewLine}", cancellationToken);
}

_jobSummaryRepository.AppendText($"</details>{Environment.NewLine}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"</details>{Environment.NewLine}{Environment.NewLine}",
cancellationToken);
}

private void WriteIncludedModulesInfo()
private async ValueTask WriteIncludedModulesInfoAsync(CancellationToken cancellationToken)
{
if (_buildReport.strippingInfo == null)
{
return;
}

_jobSummaryRepository.AppendText($"## Included Modules{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"## Included Modules{Environment.NewLine}", cancellationToken);

_jobSummaryRepository.AppendText(
$"<details><summary>Details</summary>{Environment.NewLine}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync(
$"<details><summary>Details</summary>{Environment.NewLine}{Environment.NewLine}", cancellationToken);

foreach (var item in _buildReport.strippingInfo.includedModules)
{
WriteIncludedModuleInfoInternal(item, 0);
await WriteIncludedModuleInfoInternalAsync(item, 0, cancellationToken);
}

_jobSummaryRepository.AppendText($"</details>{Environment.NewLine}{Environment.NewLine}");
await _jobSummaryRepository.AppendTextAsync($"</details>{Environment.NewLine}{Environment.NewLine}",
cancellationToken);
}

private void WriteIncludedModuleInfoInternal(string item, uint depth)
private async ValueTask WriteIncludedModuleInfoInternalAsync(string item, uint depth,
CancellationToken cancellationToken)
{
_jobSummaryRepository.AppendText(depth == 0
await _jobSummaryRepository.AppendTextAsync(depth == 0
? $@"- **{item}**{Environment.NewLine}"
: $@"{new string(' ', (int)(depth * 2))} - {item}{Environment.NewLine}");
: $@"{new string(' ', (int)(depth * 2))} - {item}{Environment.NewLine}", cancellationToken);

var reasons = _buildReport.strippingInfo.GetReasonsForIncluding(item);
foreach (var reason in reasons)
{
WriteIncludedModuleInfoInternal(reason, depth + 1);
await WriteIncludedModuleInfoInternalAsync(reason, depth + 1, cancellationToken);
}
}

Expand Down

0 comments on commit 02ca0c0

Please sign in to comment.