Skip to content

Commit 8cc92aa

Browse files
authored
[browser] Override boot config only when the content changes (#118637)
1 parent 38aa769 commit 8cc92aa

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.IO;
6+
using System.Security.Cryptography;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization.Metadata;
9+
using Microsoft.Build.Framework;
10+
using Microsoft.Build.Utilities;
11+
12+
namespace Microsoft.NET.Sdk.WebAssembly;
13+
14+
public static class ArtifactWriter
15+
{
16+
public static bool PersistFileIfChanged<T>(TaskLoggingHelper log, T manifest, string artifactPath, JsonTypeInfo<T> serializer)
17+
{
18+
var data = JsonSerializer.SerializeToUtf8Bytes(manifest, serializer);
19+
return PersistFileIfChanged(log, data, artifactPath);
20+
}
21+
22+
public static bool PersistFileIfChanged(TaskLoggingHelper log, byte[] data, string artifactPath)
23+
{
24+
var newHash = ComputeHash(data);
25+
var fileExists = File.Exists(artifactPath);
26+
var existingManifestHash = fileExists ? ComputeHash(artifactPath) : null;
27+
28+
if (!fileExists)
29+
{
30+
log.LogMessage(MessageImportance.Low, $"Creating artifact because artifact file '{artifactPath}' does not exist.");
31+
File.WriteAllBytes(artifactPath, data);
32+
return true;
33+
}
34+
else if (!string.Equals(newHash, existingManifestHash, StringComparison.Ordinal))
35+
{
36+
log.LogMessage(MessageImportance.Low, $"Updating artifact because artifact version '{newHash}' is different from existing artifact hash '{existingManifestHash}'.");
37+
File.WriteAllBytes(artifactPath, data);
38+
return true;
39+
}
40+
else
41+
{
42+
log.LogMessage(MessageImportance.Low, $"Skipping artifact updated because artifact version '{existingManifestHash}' has not changed.");
43+
return false;
44+
}
45+
}
46+
47+
private static string ComputeHash(string artifactPath) => ComputeHash(File.ReadAllBytes(artifactPath));
48+
49+
private static string ComputeHash(byte[] data)
50+
{
51+
#if NET6_0_OR_GREATER
52+
var hash = SHA256.HashData(data);
53+
return Convert.ToBase64String(hash);
54+
#else
55+
using var sha256 = SHA256.Create();
56+
return Convert.ToBase64String(sha256.ComputeHash(data));
57+
#endif
58+
}
59+
}

src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void WriteConfigToFile(BootJsonData config, string outputPath, string? ou
8787
output = $"export const config = /*json-start*/{output}/*json-end*/;";
8888
}
8989

90-
File.WriteAllText(outputPath, output);
90+
ArtifactWriter.PersistFileIfChanged(Log, Encoding.UTF8.GetBytes(output), outputPath);
9191
}
9292

9393
private string ReplaceWithAssert(Regex regex, string content, string replacement, string errorMessage)

src/tasks/WasmAppBuilder/WasmAppBuilder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="..\Common\LogAsErrorException.cs" />
2222
<Compile Include="..\Common\TempFileName.cs" />
2323
<Compile Include="..\Common\JoinedString.cs" />
24+
<Compile Include="..\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks\ArtifactWriter.cs" />
2425
<Compile Include="..\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks\BootJsonData.cs" />
2526
<Compile Include="..\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks\BootJsonBuilderHelper.cs" />
2627
</ItemGroup>

0 commit comments

Comments
 (0)