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

Add options to copy certain Addressables-related build logs to the output directory #23

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
78 changes: 77 additions & 1 deletion Options/OptionBuildAddressables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using sttz.Trimmer.BaseOptions;
using UnityEditor.Build.Reporting;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets;
using UnityEditor.Build;
using UnityEditor;
using UnityEngine.AddressableAssets;
using IOFile = System.IO.File;
using IOPath = System.IO.Path;

namespace sttz.Trimmer.Options
{
Expand Down Expand Up @@ -127,6 +130,55 @@ protected override void Configure()
}
}

/// <summary>
/// Option to copy the generated build timeline to the output directory
/// to simplify analysis later.
/// </summary>
/// <remarks>
/// Addressables exposes a detailed timeline of its build process through
/// a file located at <c>Library/com.unity.addressables/AddressablesBuildTEP.json</c>.
/// This file is overwritten with each Addressables build,
/// which can complicate analysis if your project involves multiple build profiles.
/// This option, if not empty, will copy the aforementioned file
/// to the directory given by the value,
/// as resolved by the profile.
/// </remarks>
/// <seealso href="https://docs.unity3d.com/Packages/com.unity.addressables@1.21/manual/BuildProfileLog"/>
public class OptionCopyBuildTimelineToOutputDirectory : OptionString
{
protected override void Configure()
{
DefaultValue = "[UnityEngine.AddressableAssets.Addressables.BuildPath]";
}
}

/// <summary>
/// Option to copy the generated build layout to the output directory
/// to simplify analysis later.
/// </summary>
/// <remarks>
/// <para>
/// Addressables provides a detailed layout of the asset bundles it produces in
/// a file located at <c>Library/com.unity.addressables/buildlayout.txt</c>.
/// This file is overwritten with each Addressables build,
/// which can complicate analysis if your project involves multiple build profiles.
/// This option, if not empty, will copy the aforementioned file
/// to the directory given by the value,
/// as resolved by the profile.
/// </para>
/// <para>
/// If build layout reports are disabled, this option will do nothing.
/// </para>
/// </remarks>
/// <seealso href="https://docs.unity3d.com/Packages/com.unity.addressables@1.21/manual/BuildLayoutReport"/>
public class OptionCopyBuildLayoutToOutputDirectory : OptionString
{
protected override void Configure()
{
DefaultValue = "[UnityEngine.AddressableAssets.Addressables.BuildPath]";
}
}

override public BuildPlayerOptions PrepareBuild(BuildPlayerOptions options, OptionInclusion inclusion)
{
options = base.PrepareBuild(options, inclusion);
Expand Down Expand Up @@ -182,6 +234,19 @@ void BuildAddressables()

// Build!
AddressableAssetSettings.BuildPlayerContent(out result);

// Copy relevant logs to the build directory for easier analysis, if requested
var buildTimelineDestinationExpression = GetChild<OptionCopyBuildTimelineToOutputDirectory>()?.Value;
if (!string.IsNullOrEmpty(buildTimelineDestinationExpression))
{
CopyBuildLogFile(settings, "AddressablesBuildTEP.json", buildTimelineDestinationExpression);
}

var buildLayoutDestinationExpression = GetChild<OptionCopyBuildLayoutToOutputDirectory>()?.Value;
if (!string.IsNullOrEmpty(buildLayoutDestinationExpression))
{
CopyBuildLogFile(settings, "buildlayout.txt", buildLayoutDestinationExpression);
}
} finally {
// Restore overrides
if (settings != null) {
Expand All @@ -201,6 +266,17 @@ void BuildAddressables()

Debug.Log($"Built {result.LocationCount} Addressable assets in {result.Duration}s to {result.OutputPath}");
}

void CopyBuildLogFile(AddressableAssetSettings settings, string fileName, string destination)
{
var sourcePath = IOPath.Combine(Addressables.LibraryPath, fileName);

if (IOFile.Exists(sourcePath))
{
var destinationPath = settings.profileSettings.EvaluateString(settings.activeProfileId, destination);
File.Copy(sourcePath, IOPath.Combine(destinationPath, fileName));
}
}
}

}
Expand Down