-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GenerateJSModuleManifest.cs
61 lines (51 loc) · 2.37 KB
/
GenerateJSModuleManifest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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.Encodings.Web;
using System.Text.Json;
using Microsoft.Build.Framework;
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks
{
public class GenerateJSModuleManifest : Task
{
private static readonly JsonSerializerOptions ManifestSerializationOptions = new()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true
};
[Required]
public string OutputFile { get; set; }
[Required]
public ITaskItem[] JsModules { get; set; }
public override bool Execute()
{
var modules = JsModules.Select(StaticWebAsset.FromTaskItem).Select(s => s.ComputeTargetPath("", '/')).ToArray();
Array.Sort(modules, StringComparer.Ordinal);
PersistModules(modules);
return !Log.HasLoggedErrors;
}
private void PersistModules(string[] modules)
{
var data = JsonSerializer.SerializeToUtf8Bytes(modules, ManifestSerializationOptions);
using var sha256 = SHA256.Create();
var currentHash = sha256.ComputeHash(data);
Directory.CreateDirectory(Path.GetDirectoryName(OutputFile));
var fileExists = File.Exists(OutputFile);
var existingManifestHash = fileExists ? sha256.ComputeHash(File.ReadAllBytes(OutputFile)) : Array.Empty<byte>();
if (!fileExists)
{
Log.LogMessage(MessageImportance.Low, $"Creating manifest because manifest file '{OutputFile}' does not exist.");
File.WriteAllBytes(OutputFile, data);
}
else if (!currentHash.SequenceEqual(existingManifestHash))
{
Log.LogMessage(MessageImportance.Low, $"Updating manifest because manifest version '{Convert.ToBase64String(currentHash)}' is different from existing manifest hash '{Convert.ToBase64String(existingManifestHash)}'.");
File.WriteAllBytes(OutputFile, data);
}
else
{
Log.LogMessage(MessageImportance.Low, $"Skipping manifest updated because manifest version '{Convert.ToBase64String(currentHash)}' has not changed.");
}
}
}
}