Use CompositeMountedFileProvider to simplify build logic
This commit is contained in:
parent
a81ad1830f
commit
85cc7aee34
|
|
@ -12,14 +12,6 @@ namespace Microsoft.Blazor.BuildTools.Core
|
|||
var clientFileSystem = ClientFileSystem.Instantiate(assemblyPath, webRootPath);
|
||||
var distDirPath = Path.Combine(Path.GetDirectoryName(assemblyPath), "dist");
|
||||
FileUtil.WriteFileProviderToDisk(clientFileSystem, distDirPath, clean: true);
|
||||
|
||||
// Temporary hack until ClientFileSystem can mount the subdirs in the correct place
|
||||
var frameworkPath = Path.Combine(distDirPath, "_framework");
|
||||
Directory.CreateDirectory(frameworkPath);
|
||||
Directory.Move(Path.Combine(distDirPath, "_bin"), Path.Combine(frameworkPath, "_bin"));
|
||||
Directory.Move(Path.Combine(distDirPath, "asmjs"), Path.Combine(frameworkPath, "asmjs"));
|
||||
Directory.Move(Path.Combine(distDirPath, "wasm"), Path.Combine(frameworkPath, "wasm"));
|
||||
File.Move(Path.Combine(distDirPath, "blazor.js"), Path.Combine(frameworkPath, "blazor.js"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
using Microsoft.Blazor.BuildTools.Core.FrameworkFiles;
|
||||
using Microsoft.Blazor.BuildTools.Core.WebRootFiles;
|
||||
using Microsoft.Blazor.Internal.Common.FileProviders;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
|
@ -13,7 +14,7 @@ namespace Microsoft.Blazor.BuildTools.Core
|
|||
{
|
||||
public static IFileProvider Instantiate(string clientAssemblyPath, string webRootPath)
|
||||
{
|
||||
var fileProviders = new List<IFileProvider>();
|
||||
var fileProviders = new List<(string, IFileProvider)>();
|
||||
|
||||
if (!File.Exists(clientAssemblyPath))
|
||||
{
|
||||
|
|
@ -22,7 +23,7 @@ namespace Microsoft.Blazor.BuildTools.Core
|
|||
|
||||
var frameworkFileProvider = FrameworkFileProvider.Instantiate(
|
||||
clientAssemblyPath);
|
||||
fileProviders.Add(frameworkFileProvider);
|
||||
fileProviders.Add(("/_framework", frameworkFileProvider));
|
||||
|
||||
if (!string.IsNullOrEmpty(webRootPath))
|
||||
{
|
||||
|
|
@ -35,10 +36,10 @@ namespace Microsoft.Blazor.BuildTools.Core
|
|||
webRootPath,
|
||||
Path.GetFileNameWithoutExtension(clientAssemblyPath),
|
||||
frameworkFileProvider.GetDirectoryContents("/_bin"));
|
||||
fileProviders.Add(webRootFileProvider);
|
||||
fileProviders.Add(("/", webRootFileProvider));
|
||||
}
|
||||
|
||||
return new CompositeFileProvider(fileProviders);
|
||||
return new CompositeMountedFileProvider(fileProviders.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.Blazor.Browser;
|
||||
using Microsoft.Blazor.Internal.Common.FileProviders;
|
||||
using Microsoft.Blazor.Mono;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using System.IO;
|
||||
|
|
@ -11,10 +12,10 @@ namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
|
|||
internal static class FrameworkFileProvider
|
||||
{
|
||||
public static IFileProvider Instantiate(string clientAssemblyPath)
|
||||
=> new CompositeFileProvider(
|
||||
MonoStaticFileProvider.JsFiles, // /_framework/wasm/*, /framework/asmjs/*
|
||||
BlazorBrowserFileProvider.Instance, // /_framework/blazor.js
|
||||
BinDirFileProvider(clientAssemblyPath)); // /_framework/_bin/*
|
||||
=> new CompositeMountedFileProvider(
|
||||
("/", MonoStaticFileProvider.JsFiles),
|
||||
("/", BlazorBrowserFileProvider.Instance),
|
||||
("/_bin", BinDirFileProvider(clientAssemblyPath)));
|
||||
|
||||
private static IFileProvider BinDirFileProvider(string clientAssemblyPath)
|
||||
=> new ReferencedAssemblyFileProvider(
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@ namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
|
|||
{
|
||||
internal class ReferencedAssemblyFileProvider : InMemoryFileProvider
|
||||
{
|
||||
private const string ClientBinDir = "_bin";
|
||||
|
||||
public ReferencedAssemblyFileProvider(string rootAssemblyName, ReferencedAssemblyResolver resolver)
|
||||
: base(ComputeContents(rootAssemblyName, resolver))
|
||||
{
|
||||
|
|
@ -26,7 +24,7 @@ namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
|
|||
AddWithReferencesRecursive(rootAssemblyName, resolver, foundAssemblies);
|
||||
|
||||
return foundAssemblies.Values.Select(assembly => (
|
||||
$"/{ClientBinDir}/{assembly.Definition.Name.Name}.dll",
|
||||
$"/{assembly.Definition.Name.Name}.dll",
|
||||
(Stream)new MemoryStream(assembly.Data)));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
|
|||
// so that all subsequent reads are "O(dictionary lookup)" time
|
||||
public class CompositeMountedFileProvider : InMemoryFileProvider
|
||||
{
|
||||
public CompositeMountedFileProvider(IEnumerable<(string, IFileProvider)> providers)
|
||||
public CompositeMountedFileProvider(params (string, IFileProvider)[] providers)
|
||||
: base(GetCompositeContents(providers))
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,16 +14,16 @@ namespace Microsoft.Blazor.Server.Test
|
|||
public class ReferencedAssemblyFileProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public void RootDirContainsOnlyBinDir()
|
||||
public void RootDirContainsOnlyDlls()
|
||||
{
|
||||
var provider = new ReferencedAssemblyFileProvider(
|
||||
"mscorlib",
|
||||
new ReferencedAssemblyResolver(MonoStaticFileProvider.BclFiles, string.Empty));
|
||||
Assert.Collection(provider.GetDirectoryContents("/"), item =>
|
||||
foreach (var item in provider.GetDirectoryContents("/"))
|
||||
{
|
||||
Assert.Equal("/_bin", item.PhysicalPath);
|
||||
Assert.True(item.IsDirectory);
|
||||
});
|
||||
Assert.False(item.IsDirectory);
|
||||
Assert.EndsWith(".dll", item.Name);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -32,12 +32,12 @@ namespace Microsoft.Blazor.Server.Test
|
|||
var provider = new ReferencedAssemblyFileProvider(
|
||||
"System.Linq.Expressions",
|
||||
new ReferencedAssemblyResolver(MonoStaticFileProvider.BclFiles, string.Empty));
|
||||
var contents = provider.GetDirectoryContents("/_bin").OrderBy(i => i.Name).ToList();
|
||||
var contents = provider.GetDirectoryContents("").OrderBy(i => i.Name).ToList();
|
||||
Assert.Collection(contents,
|
||||
item => { Assert.Equal("/_bin/mscorlib.dll", item.PhysicalPath); },
|
||||
item => { Assert.Equal("/_bin/System.Core.dll", item.PhysicalPath); },
|
||||
item => { Assert.Equal("/_bin/System.dll", item.PhysicalPath); },
|
||||
item => { Assert.Equal("/_bin/System.Linq.Expressions.dll", item.PhysicalPath); });
|
||||
item => { Assert.Equal("/mscorlib.dll", item.PhysicalPath); },
|
||||
item => { Assert.Equal("/System.Core.dll", item.PhysicalPath); },
|
||||
item => { Assert.Equal("/System.dll", item.PhysicalPath); },
|
||||
item => { Assert.Equal("/System.Linq.Expressions.dll", item.PhysicalPath); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -68,26 +68,26 @@ namespace Microsoft.Blazor.Server.Test
|
|||
fewer assemblies from the server, and during publishing, illink would remove all the
|
||||
uncalled implementation code from mscorlib.dll anyway.
|
||||
*/
|
||||
"/_bin/Microsoft.Blazor.dll",
|
||||
"/_bin/mscorlib.dll",
|
||||
"/_bin/netstandard.dll",
|
||||
"/_bin/StandaloneApp.dll",
|
||||
"/_bin/System.Console.dll",
|
||||
"/_bin/System.Core.dll",
|
||||
"/_bin/System.Diagnostics.StackTrace.dll",
|
||||
"/_bin/System.dll",
|
||||
"/_bin/System.Globalization.Extensions.dll",
|
||||
"/_bin/System.Runtime.dll",
|
||||
"/_bin/System.Runtime.InteropServices.RuntimeInformation.dll",
|
||||
"/_bin/System.Runtime.Serialization.Primitives.dll",
|
||||
"/_bin/System.Runtime.Serialization.Xml.dll",
|
||||
"/_bin/System.Security.Cryptography.Algorithms.dll",
|
||||
"/_bin/System.Security.SecureString.dll",
|
||||
"/_bin/System.Xml.XPath.XDocument.dll",
|
||||
"/Microsoft.Blazor.dll",
|
||||
"/mscorlib.dll",
|
||||
"/netstandard.dll",
|
||||
"/StandaloneApp.dll",
|
||||
"/System.Console.dll",
|
||||
"/System.Core.dll",
|
||||
"/System.Diagnostics.StackTrace.dll",
|
||||
"/System.dll",
|
||||
"/System.Globalization.Extensions.dll",
|
||||
"/System.Runtime.dll",
|
||||
"/System.Runtime.InteropServices.RuntimeInformation.dll",
|
||||
"/System.Runtime.Serialization.Primitives.dll",
|
||||
"/System.Runtime.Serialization.Xml.dll",
|
||||
"/System.Security.Cryptography.Algorithms.dll",
|
||||
"/System.Security.SecureString.dll",
|
||||
"/System.Xml.XPath.XDocument.dll",
|
||||
};
|
||||
|
||||
// Act
|
||||
var contents = provider.GetDirectoryContents("/_bin")
|
||||
var contents = provider.GetDirectoryContents("")
|
||||
.OrderBy(i => i.Name, StringComparer.InvariantCulture).ToList();
|
||||
|
||||
// Assert
|
||||
|
|
|
|||
|
|
@ -23,10 +23,8 @@ namespace Microsoft.Blazor.Common.Test
|
|||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
new CompositeMountedFileProvider(new[]
|
||||
{
|
||||
("test", TestFileProvider("/something.txt"))
|
||||
});
|
||||
new CompositeMountedFileProvider(
|
||||
("test", TestFileProvider("/something.txt")));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -35,10 +33,8 @@ namespace Microsoft.Blazor.Common.Test
|
|||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
new CompositeMountedFileProvider(new[]
|
||||
{
|
||||
("/test/", TestFileProvider("/something.txt"))
|
||||
});
|
||||
new CompositeMountedFileProvider(
|
||||
("/test/", TestFileProvider("/something.txt")));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -47,10 +43,8 @@ namespace Microsoft.Blazor.Common.Test
|
|||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
new CompositeMountedFileProvider(new[]
|
||||
{
|
||||
("/test", TestFileProvider("something.txt"))
|
||||
});
|
||||
new CompositeMountedFileProvider(
|
||||
("/test", TestFileProvider("something.txt")));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -63,10 +57,7 @@ namespace Microsoft.Blazor.Common.Test
|
|||
TestItem("/rootitem.txt", "Root item contents"),
|
||||
TestItem("/subdir/another", "Another test item"),
|
||||
});
|
||||
var instance = new CompositeMountedFileProvider(new[]
|
||||
{
|
||||
("/", childProvider)
|
||||
});
|
||||
var instance = new CompositeMountedFileProvider(("/", childProvider));
|
||||
|
||||
// Act
|
||||
var rootContents = instance.GetDirectoryContents(string.Empty);
|
||||
|
|
@ -99,12 +90,10 @@ namespace Microsoft.Blazor.Common.Test
|
|||
public void CanMountFileProvidersAtSubPaths()
|
||||
{
|
||||
// Arrange
|
||||
var instance = new CompositeMountedFileProvider(new[]
|
||||
{
|
||||
var instance = new CompositeMountedFileProvider(
|
||||
("/dir", TestFileProvider("/first", "/A/second", "/A/third")),
|
||||
("/dir/sub", TestFileProvider("/X", "/B/Y", "/B/Z")),
|
||||
("/other", TestFileProvider("/final")),
|
||||
});
|
||||
("/other", TestFileProvider("/final")));
|
||||
|
||||
// Act
|
||||
var rootContents = instance.GetDirectoryContents("/");
|
||||
|
|
@ -139,11 +128,9 @@ namespace Microsoft.Blazor.Common.Test
|
|||
public void CanMountMultipleFileProvidersAtSameLocation()
|
||||
{
|
||||
// Arrange
|
||||
var instance = new CompositeMountedFileProvider(new[]
|
||||
{
|
||||
var instance = new CompositeMountedFileProvider(
|
||||
("/dir", TestFileProvider("/first")),
|
||||
("/dir", TestFileProvider("/second"))
|
||||
});
|
||||
("/dir", TestFileProvider("/second")));
|
||||
|
||||
// Act
|
||||
var contents = instance.GetDirectoryContents("/dir");
|
||||
|
|
@ -159,11 +146,9 @@ namespace Microsoft.Blazor.Common.Test
|
|||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
new CompositeMountedFileProvider(new[]
|
||||
{
|
||||
new CompositeMountedFileProvider(
|
||||
("/dir", TestFileProvider("/file")),
|
||||
("/", TestFileProvider("/dir/file"))
|
||||
});
|
||||
("/", TestFileProvider("/dir/file")));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue