From 78ecf5ce2c5d7c06a3dd1433eb8bacd6174a84dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 12 Aug 2025 15:22:18 +0200 Subject: [PATCH 1/7] Override boot config only when the content changes --- .../ArtifactWriter.cs | 56 +++++++++++++++++++ .../BootJsonBuilderHelper.cs | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs new file mode 100644 index 00000000000000..672fca50aea781 --- /dev/null +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Security.Cryptography; +using System.Text.Json; +using System.Text.Json.Serialization.Metadata; +using Microsoft.Build.Framework; + +namespace Microsoft.NET.Sdk.WebAssembly; + +public static class ArtifactWriter +{ + public static bool PersistFileIfChanged(this Task task, T manifest, string artifactPath, JsonTypeInfo serializer) + { + var data = JsonSerializer.SerializeToUtf8Bytes(manifest, serializer); + return PersistFileIfChanged(task, data, artifactPath); + } + + public static bool PersistFileIfChanged(this Task task, byte[] data, string artifactPath) + { + var newHash = ComputeHash(data); + var fileExists = File.Exists(artifactPath); + var existingManifestHash = fileExists ? ComputeHash(artifactPath) : null; + + if (!fileExists) + { + task.Log.LogMessage(MessageImportance.Low, $"Creating artifact because artifact file '{artifactPath}' does not exist."); + File.WriteAllBytes(artifactPath, data); + return true; + } + else if (!string.Equals(newHash, existingManifestHash, StringComparison.Ordinal)) + { + task.Log.LogMessage(MessageImportance.Low, $"Updating artifact because artifact version '{newHash}' is different from existing artifact hash '{existingManifestHash}'."); + File.WriteAllBytes(artifactPath, data); + return true; + } + else + { + task.Log.LogMessage(MessageImportance.Low, $"Skipping artifact updated because artifact version '{existingManifestHash}' has not changed."); + return false; + } + } + + private static string ComputeHash(string artifactPath) => ComputeHash(File.ReadAllBytes(artifactPath)); + + private static string ComputeHash(byte[] data) + { +#if NET6_0_OR_GREATER + var hash = SHA256.HashData(data); + return Convert.ToBase64String(hash); +#else + using var sha256 = SHA256.Create(); + return Convert.ToBase64String(sha256.ComputeHash(data)); +#endif + } +} diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs index 93ead55e48c113..0cae802cca9124 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs @@ -87,7 +87,7 @@ public void WriteConfigToFile(BootJsonData config, string outputPath, string? ou output = $"export const config = /*json-start*/{output}/*json-end*/;"; } - File.WriteAllText(outputPath, output); + ArtifactWriter.PersistFileIfChanged(Encoding.UTF8.GetBytes(output), outputPath); } private string ReplaceWithAssert(Regex regex, string content, string replacement, string errorMessage) From e24348acaef45cd4eeff831dbb8ee0dd18057ccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 12 Aug 2025 16:14:46 +0200 Subject: [PATCH 2/7] Fix --- .../Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs index 672fca50aea781..a52fdce8abd825 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs @@ -5,6 +5,7 @@ using System.Text.Json; using System.Text.Json.Serialization.Metadata; using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; namespace Microsoft.NET.Sdk.WebAssembly; From 47045bc806bd0436f9617a29bfc29ad8cde0c4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 12 Aug 2025 17:37:00 +0200 Subject: [PATCH 3/7] Fix --- .../Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs index a52fdce8abd825..113914ac5fc940 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.IO; using System.Security.Cryptography; using System.Text.Json; using System.Text.Json.Serialization.Metadata; From 3860a2ba50683298ee0486afa8139a22ae2171f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 12 Aug 2025 18:05:16 +0200 Subject: [PATCH 4/7] Fix --- .../Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs index 113914ac5fc940..5b043d5977d580 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.IO; using System.Security.Cryptography; using System.Text.Json; From 30856dd39aa1386b413435648969ba8678ecf0cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 12 Aug 2025 19:33:25 +0200 Subject: [PATCH 5/7] Fix --- .../ArtifactWriter.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs index 5b043d5977d580..331e8b28fb771f 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ArtifactWriter.cs @@ -13,13 +13,13 @@ namespace Microsoft.NET.Sdk.WebAssembly; public static class ArtifactWriter { - public static bool PersistFileIfChanged(this Task task, T manifest, string artifactPath, JsonTypeInfo serializer) + public static bool PersistFileIfChanged(TaskLoggingHelper log, T manifest, string artifactPath, JsonTypeInfo serializer) { var data = JsonSerializer.SerializeToUtf8Bytes(manifest, serializer); - return PersistFileIfChanged(task, data, artifactPath); + return PersistFileIfChanged(log, data, artifactPath); } - public static bool PersistFileIfChanged(this Task task, byte[] data, string artifactPath) + public static bool PersistFileIfChanged(TaskLoggingHelper log, byte[] data, string artifactPath) { var newHash = ComputeHash(data); var fileExists = File.Exists(artifactPath); @@ -27,19 +27,19 @@ public static bool PersistFileIfChanged(this Task task, byte[] data, string arti if (!fileExists) { - task.Log.LogMessage(MessageImportance.Low, $"Creating artifact because artifact file '{artifactPath}' does not exist."); + log.LogMessage(MessageImportance.Low, $"Creating artifact because artifact file '{artifactPath}' does not exist."); File.WriteAllBytes(artifactPath, data); return true; } else if (!string.Equals(newHash, existingManifestHash, StringComparison.Ordinal)) { - task.Log.LogMessage(MessageImportance.Low, $"Updating artifact because artifact version '{newHash}' is different from existing artifact hash '{existingManifestHash}'."); + log.LogMessage(MessageImportance.Low, $"Updating artifact because artifact version '{newHash}' is different from existing artifact hash '{existingManifestHash}'."); File.WriteAllBytes(artifactPath, data); return true; } else { - task.Log.LogMessage(MessageImportance.Low, $"Skipping artifact updated because artifact version '{existingManifestHash}' has not changed."); + log.LogMessage(MessageImportance.Low, $"Skipping artifact updated because artifact version '{existingManifestHash}' has not changed."); return false; } } From c8be7135baf929871ae5457978ce7866037e799b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 12 Aug 2025 17:43:20 +0000 Subject: [PATCH 6/7] Fix --- .../BootJsonBuilderHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs index 0cae802cca9124..86b52178de5ccb 100644 --- a/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs +++ b/src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs @@ -87,7 +87,7 @@ public void WriteConfigToFile(BootJsonData config, string outputPath, string? ou output = $"export const config = /*json-start*/{output}/*json-end*/;"; } - ArtifactWriter.PersistFileIfChanged(Encoding.UTF8.GetBytes(output), outputPath); + ArtifactWriter.PersistFileIfChanged(Log, Encoding.UTF8.GetBytes(output), outputPath); } private string ReplaceWithAssert(Regex regex, string content, string replacement, string errorMessage) From b1c01d99192ab2d5ae258211ccee20f48d752323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Wed, 13 Aug 2025 10:35:57 +0200 Subject: [PATCH 7/7] Fix --- src/tasks/WasmAppBuilder/WasmAppBuilder.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.csproj b/src/tasks/WasmAppBuilder/WasmAppBuilder.csproj index addcf203c6a2eb..77bdf6cafb6789 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.csproj +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.csproj @@ -21,6 +21,7 @@ +