From bfcf72dfdfc6c755ec37a0025671cae4ecdb394c Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Tue, 25 Feb 2020 11:48:31 +0000 Subject: [PATCH] Fix boot json on mac/linux (#19332) --- .../Build/src/Tasks/GenerateBlazorBootJson.cs | 25 +++++++--------- .../src/targets/Blazor.MonoRuntime.targets | 29 ++++++++++--------- .../Build/test/GenerateBlazorBootJsonTest.cs | 24 ++++++--------- 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/Components/WebAssembly/Build/src/Tasks/GenerateBlazorBootJson.cs b/src/Components/WebAssembly/Build/src/Tasks/GenerateBlazorBootJson.cs index 889d9de236..75f4a566c8 100644 --- a/src/Components/WebAssembly/Build/src/Tasks/GenerateBlazorBootJson.cs +++ b/src/Components/WebAssembly/Build/src/Tasks/GenerateBlazorBootJson.cs @@ -70,10 +70,10 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Build { foreach (var resource in Resources) { - var resourceTypeMetadata = resource.GetMetadata("BootResourceType"); + var resourceTypeMetadata = resource.GetMetadata("BootManifestResourceType"); if (!Enum.TryParse(resourceTypeMetadata, out var resourceType)) { - throw new NotSupportedException($"Unsupported BootResourceType metadata value: {resourceTypeMetadata}"); + throw new NotSupportedException($"Unsupported BootManifestResourceType metadata value: {resourceTypeMetadata}"); } if (!result.resources.TryGetValue(resourceType, out var resourceList)) @@ -82,10 +82,10 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Build result.resources.Add(resourceType, resourceList); } - var resourceFileRelativePath = GetResourceFileRelativePath(resource); - if (!resourceList.ContainsKey(resourceFileRelativePath)) + var resourceName = GetResourceName(resource); + if (!resourceList.ContainsKey(resourceName)) { - resourceList.Add(resourceFileRelativePath, $"sha256-{resource.GetMetadata("FileHash")}"); + resourceList.Add(resourceName, $"sha256-{resource.GetMetadata("FileHash")}"); } } } @@ -99,21 +99,16 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Build serializer.WriteObject(writer, result); } - private static string GetResourceFileRelativePath(ITaskItem item) + private static string GetResourceName(ITaskItem item) { - // The build targets use RelativeOutputPath in the case of satellite assemblies, which - // will have relative paths like "fr\\SomeAssembly.resources.dll". If RelativeOutputPath - // is specified, we want to use all of it. - var outputPath = item.GetMetadata("RelativeOutputPath"); + var name = item.GetMetadata("BootManifestResourceName"); - if (string.IsNullOrEmpty(outputPath)) + if (string.IsNullOrEmpty(name)) { - // If RelativeOutputPath was not specified, we assume the item will be placed at the - // root of whatever directory is used for its resource type (e.g., assemblies go in _bin) - outputPath = Path.GetFileName(item.GetMetadata("TargetOutputPath")); + throw new Exception($"No BootManifestResourceName was specified for item '{item.ItemSpec}'"); } - return outputPath.Replace('\\', '/'); + return name.Replace('\\', '/'); } #pragma warning disable IDE1006 // Naming Styles diff --git a/src/Components/WebAssembly/Build/src/targets/Blazor.MonoRuntime.targets b/src/Components/WebAssembly/Build/src/targets/Blazor.MonoRuntime.targets index cca30dfa70..4066581197 100644 --- a/src/Components/WebAssembly/Build/src/targets/Blazor.MonoRuntime.targets +++ b/src/Components/WebAssembly/Build/src/targets/Blazor.MonoRuntime.targets @@ -83,17 +83,18 @@ <_BlazorCopyLocalPaths Remove="@(_BlazorManagedRuntimeAssemby)" /> <_BlazorOutputWithTargetPath Include="@(_BlazorCopyLocalPaths)"> - assembly - pdb + + assembly + pdb + %(_BlazorCopyLocalPaths.DestinationSubDirectory)%(FileName)%(Extension) $(_BlazorRuntimeBinOutputPath)%(_BlazorCopyLocalPaths.DestinationSubDirectory)%(FileName)%(Extension) - %(_BlazorCopyLocalPaths.DestinationSubDirectory)%(FileName)%(Extension) <_BlazorOutputWithTargetPath Include="@(_BlazorResolvedAssembly)"> - assembly - pdb + assembly + pdb + %(FileName)%(Extension) $(_BlazorRuntimeBinOutputPath)%(FileName)%(Extension) - %(FileName)%(Extension) @@ -120,10 +121,10 @@ <_BlazorCopyLocalPaths Remove="@(_BlazorManagedRuntimeAssemby)" Condition="'%(Extension)' == '.dll'" /> <_BlazorOutputWithTargetPath Include="@(_BlazorCopyLocalPaths)"> - assembly - pdb + assembly + pdb + %(_BlazorCopyLocalPaths.DestinationSubDirectory)%(FileName)%(Extension) $(_BlazorRuntimeBinOutputPath)%(_BlazorCopyLocalPaths.DestinationSubDirectory)%(FileName)%(Extension) - %(_BlazorCopyLocalPaths.DestinationSubDirectory)%(FileName)%(Extension) @@ -144,13 +145,15 @@ <_DotNetWasmRuntimeFile Include="$(ComponentsWebAssemblyRuntimePath)*.wasm" /> <_BlazorOutputWithTargetPath Include="@(_DotNetWasmRuntimeFile)"> $(_BlazorRuntimeWasmOutputPath)%(FileName)%(Extension) - runtime + runtime + %(FileName)%(Extension) <_DotNetWasmJsFile Include="$(ComponentsWebAssemblyRuntimePath)*.js" /> <_BlazorOutputWithTargetPath Include="@(_DotNetWasmJsFile)"> $(_BlazorRuntimeWasmOutputPath)%(FileName).$(TemporaryDotNetJsFileVersion)%(Extension) - runtime + runtime + %(FileName).$(TemporaryDotNetJsFileVersion)%(Extension) <_BlazorJSFile Include="$(_BlazorJSPath)" /> @@ -329,9 +332,7 @@ Inputs="$(MSBuildAllProjects);@(_BlazorOutputWithTargetPath)" Outputs="$(_BlazorBootJsonIntermediateOutputPath)"> - <_BlazorBootResource Include="@(_BlazorOutputWithTargetPath->HasMetadata('BlazorBootManifestResourceType'))"> - %(_BlazorOutputWithTargetPath.BlazorBootManifestResourceType) - + <_BlazorBootResource Include="@(_BlazorOutputWithTargetPath->HasMetadata('BootManifestResourceType'))" /> diff --git a/src/Components/WebAssembly/Build/test/GenerateBlazorBootJsonTest.cs b/src/Components/WebAssembly/Build/test/GenerateBlazorBootJsonTest.cs index dee7b38958..1429030b30 100644 --- a/src/Components/WebAssembly/Build/test/GenerateBlazorBootJsonTest.cs +++ b/src/Components/WebAssembly/Build/test/GenerateBlazorBootJsonTest.cs @@ -24,26 +24,22 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Build { CreateResourceTaskItem( ResourceType.assembly, - itemSpec: Path.Combine("dir", "My.Assembly1.ext"), // Can specify item spec - relativeOutputPath: null, + name: "My.Assembly1.ext", // Can specify filename with no dir fileHash: "abcdefghikjlmnopqrstuvwxyz"), CreateResourceTaskItem( ResourceType.assembly, - itemSpec: "Ignored", - relativeOutputPath: Path.Combine("dir", "My.Assembly2.ext2"), // Can specify relative path + name: "dir\\My.Assembly2.ext2", // Can specify Windows-style path fileHash: "012345678901234567890123456789"), CreateResourceTaskItem( ResourceType.pdb, - itemSpec: "SomePdb.pdb", - relativeOutputPath: null, + name: "otherdir/SomePdb.pdb", // Can specify Linux-style path fileHash: "pdbhashpdbhashpdbhash"), CreateResourceTaskItem( ResourceType.runtime, - itemSpec: "some-runtime-file", - relativeOutputPath: null, + name: "some-runtime-file", // Can specify path with no extension fileHash: "runtimehashruntimehash") } }; @@ -63,14 +59,14 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Build Assert.Equal(ResourceType.assembly, resourceListKey); Assert.Equal(2, resources.Count); Assert.Equal("sha256-abcdefghikjlmnopqrstuvwxyz", resources["My.Assembly1.ext"]); - Assert.Equal("sha256-012345678901234567890123456789", resources["dir/My.Assembly2.ext2"]); // For relative paths, we preserve the whole relative path, but use URL-style separators + Assert.Equal("sha256-012345678901234567890123456789", resources["dir/My.Assembly2.ext2"]); // Paths are converted to use URL-style separators }, resourceListKey => { var resources = parsedContent.resources[resourceListKey]; Assert.Equal(ResourceType.pdb, resourceListKey); Assert.Single(resources); - Assert.Equal("sha256-pdbhashpdbhashpdbhash", resources["SomePdb.pdb"]); + Assert.Equal("sha256-pdbhashpdbhashpdbhash", resources["otherdir/SomePdb.pdb"]); }, resourceListKey => { @@ -141,13 +137,11 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Build return (BootJsonData)serializer.ReadObject(stream); } - private static ITaskItem CreateResourceTaskItem(ResourceType type, string itemSpec, string relativeOutputPath, string fileHash) + private static ITaskItem CreateResourceTaskItem(ResourceType type, string name, string fileHash) { var mock = new Mock(); - mock.Setup(m => m.ItemSpec).Returns(itemSpec); - mock.Setup(m => m.GetMetadata("TargetOutputPath")).Returns(itemSpec); - mock.Setup(m => m.GetMetadata("BootResourceType")).Returns(type.ToString()); - mock.Setup(m => m.GetMetadata("RelativeOutputPath")).Returns(relativeOutputPath); + mock.Setup(m => m.GetMetadata("BootManifestResourceType")).Returns(type.ToString()); + mock.Setup(m => m.GetMetadata("BootManifestResourceName")).Returns(name); mock.Setup(m => m.GetMetadata("FileHash")).Returns(fileHash); return mock.Object; }