Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed file write process to asynchronous #14

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -11,6 +13,8 @@ internal sealed class EditorQuitEntryPoint
internal static bool ExecutesUnityBuild;
private static readonly string s_gitHubStepSummaryPath;

private static readonly CancellationTokenSource cts = new();

// The 'report' argument passed to IPolsstprocessBuildWithReport.OnPostprocessBuild() contains incorrect information, so read Library/LastBuild.buildreport instead.
// see: https://issuetracker.unity3d.com/issues/buildreport-report-in-ipostprocessbuildwithreport-provides-incorrect-information
static EditorQuitEntryPoint()
Expand Down Expand Up @@ -42,7 +46,7 @@ private static void Quit()

var jobSummaryRepository = new GitHubJobSummaryRepository(s_gitHubStepSummaryPath);
var useCase = new ReportUnityBuildUseCase(jobSummaryRepository, buildReport);
useCase.WriteAll();
useCase.WriteAllAsync();
}
}
}
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 Task 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);
}
}
}
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);
Task AppendTextAsync(string text, CancellationToken cancellationToken);
}
}
125 changes: 68 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 @@ -14,30 +16,34 @@ internal sealed class ReportUnityBuildUseCase
private readonly IJobSummaryRepository _jobSummaryRepository;
private readonly BuildReport _buildReport;

private CancellationTokenSource _cts = new();

public ReportUnityBuildUseCase(IJobSummaryRepository jobSummaryRepository, BuildReport buildReport)
{
_jobSummaryRepository = jobSummaryRepository;
_buildReport = buildReport;
}

public void WriteAll()
public async Task WriteAllAsync()
{
WriteTitle();
WriteSummary();
WriteBuildStepsInfo();
WriteSourceAssetsInfo();
WriteOutputFilesInfo();
WriteIncludedModulesInfo();
var token = _cts.Token;
await WriteTitleAsync(token);
await WriteSummaryAsync(token);
await WriteBuildStepsInfoAsync(token);
await WriteSourceAssetsInfoAsync(token);
await WriteOutputFilesInfoAsync(token);
await WriteIncludedModulesInfoAsync(token);
}

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

private void WriteSummary()
private async Task 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 +57,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 Task 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 +99,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 Task 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 +129,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 +149,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 Task 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 Task 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 Task 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
Loading