Editor/Core/ImportCompletion.cs

Record type ImportCompletion used by the Editor import tool. It summarizes an import run (asset count, warnings, errors, cancelled state), computes display strings (Title, Message, Icon, Status), builds instances from ImportResult, and can append a JSON report with timings and completion data.

File Access
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace ImportUnityPackage;

/// <summary>Short completion text, with detailed warnings and errors retained in the report.</summary>
public sealed record ImportCompletion( int AssetCount, string[] Warnings, string[] Errors, bool Cancelled )
{
	/// <summary>Files written to the project, and outputs left as they were (identical, or kept by choice); -1 when unknown.</summary>
	public int Written { get; init; } = -1;
	public int Unchanged { get; init; }
	public string Title => Cancelled ? "Import preparation cancelled" : Errors.Length > 0 ? "Import completed with errors" : "Import complete";
	public string Counts => $"Warnings: {Warnings.Length} · Errors: {Errors.Length}";
	public string Message => Cancelled ? $"Imported files kept. Resource preparation stopped.\n{Counts}" :
		$"{AssetCount} assets imported{(Written < 0 ? "" : $": {Written} files written, {Unchanged} left as they were")}.\n{Counts}";
	/// <summary>The completion dialog's icon: errors, then warnings or a cancelled preparation, else a clean import.</summary>
	public string Icon => Errors.Length > 0 ? "❌" : Warnings.Length > 0 || Cancelled ? "⚠️" : "✅";
	public string Status => $"{(Errors.Length == 0 && !Cancelled ? "✓ " : "")}{Title}\n{Message}";

	public static ImportCompletion Create( ImportResult result, IEnumerable<string> preparationWarnings, IEnumerable<string> preparationErrors, bool cancelled )
	{
		var errors = result.Unresolved.Where( i => i.Severity == "Error" ).Select( i => $"{i.Asset}: {i.Message}" )
			.Concat( preparationErrors ?? Array.Empty<string>() ).Distinct().ToArray();
		var warnings = result.Warnings.Concat( result.Unresolved.Where( i => i.Severity == "Warning" ).Select( i => $"{i.Asset}: {i.Message}" ) )
			.Concat( preparationWarnings ?? Array.Empty<string>() ).Except( errors ).Distinct().ToArray();
		var written = result.ChangedFiles.Length;
		return new( result.AssetCount, warnings, errors, cancelled ) { Written = written, Unchanged = Math.Max( 0, result.Files.Length - written ) };
	}

	/// <summary>The report with the completion summary, and the engine preparation time when resources were prepared.</summary>
	public string AppendReport( string json, long? preparationMilliseconds = null )
	{
		if ( string.IsNullOrWhiteSpace( json ) ) json = "{}";
		var report = JsonNode.Parse( json );
		if ( preparationMilliseconds is long prepared )
			(report["Timings"] as JsonArray)?.Add( JsonSerializer.SerializeToNode( new { Stage = "Preparing s&box resources", Milliseconds = prepared } ) );
		report["Completion"] = JsonSerializer.SerializeToNode( new { Title, AssetCount, Written, Unchanged, Cancelled, Warnings, Errors } );
		return report.ToJsonString( new JsonSerializerOptions { WriteIndented = true } );
	}
}