|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.Collections.Generic;
|
5 | 6 | using System.IO;
|
6 | 7 | using System.Reflection;
|
7 |
| -using System.Text.Json; |
| 8 | +using System.Runtime.Serialization.Json; |
| 9 | +using System.Text; |
8 | 10 | using Microsoft.Build.Framework;
|
9 | 11 | using Microsoft.Build.Utilities;
|
10 | 12 |
|
@@ -32,67 +34,132 @@ public class GenerateBlazorBootJson : Task
|
32 | 34 |
|
33 | 35 | public override bool Execute()
|
34 | 36 | {
|
| 37 | + using var fileStream = File.Create(OutputPath); |
| 38 | + var entryAssemblyName = AssemblyName.GetAssemblyName(AssemblyPath).Name; |
| 39 | + WriteBootJson(fileStream, entryAssemblyName); |
| 40 | + |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + // Internal for tests |
| 45 | + internal void WriteBootJson(Stream output, string entryAssemblyName) |
| 46 | + { |
| 47 | + var result = new BootJsonData |
| 48 | + { |
| 49 | + entryAssembly = entryAssemblyName, |
| 50 | + cacheBootResources = CacheBootResources, |
| 51 | + debugBuild = DebugBuild, |
| 52 | + linkerEnabled = LinkerEnabled, |
| 53 | + resources = new Dictionary<ResourceType, ResourceList>() |
| 54 | + }; |
| 55 | + |
35 | 56 | // Build a two-level dictionary of the form:
|
36 | 57 | // - BootResourceType (e.g., "assembly")
|
37 | 58 | // - UriPath (e.g., "System.Text.Json.dll")
|
38 | 59 | // - ContentHash (e.g., "4548fa2e9cf52986")
|
39 |
| - var resourcesByGroup = new Dictionary<string, Dictionary<string, string>>(); |
40 |
| - foreach (var resource in Resources) |
| 60 | + if (Resources != null) |
41 | 61 | {
|
42 |
| - var resourceType = resource.GetMetadata("BootResourceType"); |
43 |
| - if (string.IsNullOrEmpty(resourceType)) |
| 62 | + foreach (var resource in Resources) |
44 | 63 | {
|
45 |
| - continue; |
46 |
| - } |
| 64 | + var resourceTypeMetadata = resource.GetMetadata("BootResourceType"); |
| 65 | + if (!Enum.TryParse<ResourceType>(resourceTypeMetadata, out var resourceType)) |
| 66 | + { |
| 67 | + throw new NotSupportedException($"Unsupported BootResourceType metadata value: {resourceTypeMetadata}"); |
| 68 | + } |
47 | 69 |
|
48 |
| - if (!resourcesByGroup.TryGetValue(resourceType, out var group)) |
49 |
| - { |
50 |
| - group = new Dictionary<string, string>(); |
51 |
| - resourcesByGroup.Add(resourceType, group); |
52 |
| - } |
| 70 | + if (!result.resources.TryGetValue(resourceType, out var resourceList)) |
| 71 | + { |
| 72 | + resourceList = new ResourceList(); |
| 73 | + result.resources.Add(resourceType, resourceList); |
| 74 | + } |
53 | 75 |
|
54 |
| - var uriPath = GetUriPath(resource); |
55 |
| - if (!group.ContainsKey(uriPath)) |
56 |
| - { |
57 |
| - // It's safe to truncate to a fairly short string, since the hash is not used for any |
58 |
| - // security purpose - the developer produces these files themselves, and the hash is |
59 |
| - // only used to check whether an earlier cached copy is up-to-date. |
60 |
| - // This truncation halves the size of blazor.boot.json in typical cases. |
61 |
| - group.Add(uriPath, resource.GetMetadata("FileHash").Substring(0, 16).ToLowerInvariant()); |
| 76 | + var resourceFileRelativePath = GetResourceFileRelativePath(resource); |
| 77 | + if (!resourceList.ContainsKey(resourceFileRelativePath)) |
| 78 | + { |
| 79 | + // It's safe to truncate to a fairly short string, since the hash is not used for any |
| 80 | + // security purpose - the developer produces these files themselves, and the hash is |
| 81 | + // only used to check whether an earlier cached copy is up-to-date. |
| 82 | + // This truncation halves the size of blazor.boot.json in typical cases. |
| 83 | + resourceList.Add(resourceFileRelativePath, resource.GetMetadata("FileHash").Substring(0, 16).ToLowerInvariant()); |
| 84 | + } |
62 | 85 | }
|
63 | 86 | }
|
64 | 87 |
|
65 |
| - var bootJsonData = new |
| 88 | + var serializer = new DataContractJsonSerializer(typeof(BootJsonData), new DataContractJsonSerializerSettings |
66 | 89 | {
|
67 |
| - EntryAssembly = AssemblyName.GetAssemblyName(AssemblyPath).Name, |
68 |
| - Resources = resourcesByGroup, |
69 |
| - DebugBuild, |
70 |
| - LinkerEnabled, |
71 |
| - CacheBootResources, |
72 |
| - }; |
73 |
| - |
74 |
| - using (var fileStream = File.Create(OutputPath)) |
75 |
| - using (var utf8Writer = new Utf8JsonWriter(fileStream, new JsonWriterOptions { Indented = true })) |
76 |
| - { |
77 |
| - JsonSerializer.Serialize(utf8Writer, bootJsonData, new JsonSerializerOptions |
78 |
| - { |
79 |
| - PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
80 |
| - }); |
81 |
| - utf8Writer.Flush(); |
82 |
| - } |
| 90 | + UseSimpleDictionaryFormat = true |
| 91 | + }); |
83 | 92 |
|
84 |
| - return true; |
| 93 | + using var writer = JsonReaderWriterFactory.CreateJsonWriter(output, Encoding.UTF8, ownsStream: false, indent: true); |
| 94 | + serializer.WriteObject(writer, result); |
85 | 95 | }
|
86 | 96 |
|
87 |
| - private static string GetUriPath(ITaskItem item) |
| 97 | + private static string GetResourceFileRelativePath(ITaskItem item) |
88 | 98 | {
|
| 99 | + // The build targets use RelativeOutputPath in the case of satellite assemblies, which |
| 100 | + // will have relative paths like "fr\\SomeAssembly.resources.dll". If RelativeOutputPath |
| 101 | + // is specified, we want to use all of it. |
89 | 102 | var outputPath = item.GetMetadata("RelativeOutputPath");
|
| 103 | + |
90 | 104 | if (string.IsNullOrEmpty(outputPath))
|
91 | 105 | {
|
| 106 | + // If RelativeOutputPath was not specified, we assume the item will be placed at the |
| 107 | + // root of whatever directory is used for its resource type (e.g., assemblies go in _bin) |
92 | 108 | outputPath = Path.GetFileName(item.ItemSpec);
|
93 | 109 | }
|
94 | 110 |
|
95 | 111 | return outputPath.Replace('\\', '/');
|
96 | 112 | }
|
| 113 | + |
| 114 | +#pragma warning disable IDE1006 // Naming Styles |
| 115 | + /// <summary> |
| 116 | + /// Defines the structure of a Blazor boot JSON file |
| 117 | + /// </summary> |
| 118 | + public class BootJsonData |
| 119 | + { |
| 120 | + /// <summary> |
| 121 | + /// Gets the name of the assembly with the application entry point |
| 122 | + /// </summary> |
| 123 | + public string entryAssembly { get; set; } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Gets the set of resources needed to boot the application. This includes the transitive |
| 127 | + /// closure of .NET assemblies (including the entrypoint assembly), the dotnet.wasm file, |
| 128 | + /// and any PDBs to be loaded. |
| 129 | + /// </summary> |
| 130 | + public Dictionary<ResourceType, ResourceList> resources { get; set; } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Gets a value that determines whether to enable caching of the <see cref="resources"/> |
| 134 | + /// inside a CacheStorage instance within the browser. |
| 135 | + /// </summary> |
| 136 | + public bool cacheBootResources { get; set; } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Gets a value that determines if this is a debug build. |
| 140 | + /// </summary> |
| 141 | + public bool debugBuild { get; set; } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Gets a value that determines if the linker is enabled. |
| 145 | + /// </summary> |
| 146 | + public bool linkerEnabled { get; set; } |
| 147 | + } |
| 148 | + |
| 149 | + public enum ResourceType |
| 150 | + { |
| 151 | + assembly, |
| 152 | + pdb, |
| 153 | + wasm |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Represents a set of resources used when booting a Blazor WebAssembly application. |
| 158 | + /// The dictionary keys are the resource names; values are SHA-256 hashes of the corresponding files. |
| 159 | + /// </summary> |
| 160 | + public class ResourceList : Dictionary<string, string> |
| 161 | + { |
| 162 | + } |
| 163 | +#pragma warning restore IDE1006 // Naming Styles |
97 | 164 | }
|
98 | 165 | }
|
0 commit comments