Use CompositeMountedFileProvider to simplify build logic

This commit is contained in:
Steve Sanderson 2017-12-14 14:48:36 +00:00
parent a81ad1830f
commit 85cc7aee34
7 changed files with 52 additions and 75 deletions

View File

@ -12,14 +12,6 @@ namespace Microsoft.Blazor.BuildTools.Core
var clientFileSystem = ClientFileSystem.Instantiate(assemblyPath, webRootPath); var clientFileSystem = ClientFileSystem.Instantiate(assemblyPath, webRootPath);
var distDirPath = Path.Combine(Path.GetDirectoryName(assemblyPath), "dist"); var distDirPath = Path.Combine(Path.GetDirectoryName(assemblyPath), "dist");
FileUtil.WriteFileProviderToDisk(clientFileSystem, distDirPath, clean: true); 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"));
} }
} }
} }

View File

@ -3,6 +3,7 @@
using Microsoft.Blazor.BuildTools.Core.FrameworkFiles; using Microsoft.Blazor.BuildTools.Core.FrameworkFiles;
using Microsoft.Blazor.BuildTools.Core.WebRootFiles; using Microsoft.Blazor.BuildTools.Core.WebRootFiles;
using Microsoft.Blazor.Internal.Common.FileProviders;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -13,7 +14,7 @@ namespace Microsoft.Blazor.BuildTools.Core
{ {
public static IFileProvider Instantiate(string clientAssemblyPath, string webRootPath) public static IFileProvider Instantiate(string clientAssemblyPath, string webRootPath)
{ {
var fileProviders = new List<IFileProvider>(); var fileProviders = new List<(string, IFileProvider)>();
if (!File.Exists(clientAssemblyPath)) if (!File.Exists(clientAssemblyPath))
{ {
@ -22,7 +23,7 @@ namespace Microsoft.Blazor.BuildTools.Core
var frameworkFileProvider = FrameworkFileProvider.Instantiate( var frameworkFileProvider = FrameworkFileProvider.Instantiate(
clientAssemblyPath); clientAssemblyPath);
fileProviders.Add(frameworkFileProvider); fileProviders.Add(("/_framework", frameworkFileProvider));
if (!string.IsNullOrEmpty(webRootPath)) if (!string.IsNullOrEmpty(webRootPath))
{ {
@ -35,10 +36,10 @@ namespace Microsoft.Blazor.BuildTools.Core
webRootPath, webRootPath,
Path.GetFileNameWithoutExtension(clientAssemblyPath), Path.GetFileNameWithoutExtension(clientAssemblyPath),
frameworkFileProvider.GetDirectoryContents("/_bin")); frameworkFileProvider.GetDirectoryContents("/_bin"));
fileProviders.Add(webRootFileProvider); fileProviders.Add(("/", webRootFileProvider));
} }
return new CompositeFileProvider(fileProviders); return new CompositeMountedFileProvider(fileProviders.ToArray());
} }
} }
} }

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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.Browser;
using Microsoft.Blazor.Internal.Common.FileProviders;
using Microsoft.Blazor.Mono; using Microsoft.Blazor.Mono;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using System.IO; using System.IO;
@ -11,10 +12,10 @@ namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
internal static class FrameworkFileProvider internal static class FrameworkFileProvider
{ {
public static IFileProvider Instantiate(string clientAssemblyPath) public static IFileProvider Instantiate(string clientAssemblyPath)
=> new CompositeFileProvider( => new CompositeMountedFileProvider(
MonoStaticFileProvider.JsFiles, // /_framework/wasm/*, /framework/asmjs/* ("/", MonoStaticFileProvider.JsFiles),
BlazorBrowserFileProvider.Instance, // /_framework/blazor.js ("/", BlazorBrowserFileProvider.Instance),
BinDirFileProvider(clientAssemblyPath)); // /_framework/_bin/* ("/_bin", BinDirFileProvider(clientAssemblyPath)));
private static IFileProvider BinDirFileProvider(string clientAssemblyPath) private static IFileProvider BinDirFileProvider(string clientAssemblyPath)
=> new ReferencedAssemblyFileProvider( => new ReferencedAssemblyFileProvider(

View File

@ -11,8 +11,6 @@ namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
{ {
internal class ReferencedAssemblyFileProvider : InMemoryFileProvider internal class ReferencedAssemblyFileProvider : InMemoryFileProvider
{ {
private const string ClientBinDir = "_bin";
public ReferencedAssemblyFileProvider(string rootAssemblyName, ReferencedAssemblyResolver resolver) public ReferencedAssemblyFileProvider(string rootAssemblyName, ReferencedAssemblyResolver resolver)
: base(ComputeContents(rootAssemblyName, resolver)) : base(ComputeContents(rootAssemblyName, resolver))
{ {
@ -26,7 +24,7 @@ namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
AddWithReferencesRecursive(rootAssemblyName, resolver, foundAssemblies); AddWithReferencesRecursive(rootAssemblyName, resolver, foundAssemblies);
return foundAssemblies.Values.Select(assembly => ( return foundAssemblies.Values.Select(assembly => (
$"/{ClientBinDir}/{assembly.Definition.Name.Name}.dll", $"/{assembly.Definition.Name.Name}.dll",
(Stream)new MemoryStream(assembly.Data))); (Stream)new MemoryStream(assembly.Data)));
} }

View File

@ -15,7 +15,7 @@ namespace Microsoft.Blazor.Internal.Common.FileProviders
// so that all subsequent reads are "O(dictionary lookup)" time // so that all subsequent reads are "O(dictionary lookup)" time
public class CompositeMountedFileProvider : InMemoryFileProvider public class CompositeMountedFileProvider : InMemoryFileProvider
{ {
public CompositeMountedFileProvider(IEnumerable<(string, IFileProvider)> providers) public CompositeMountedFileProvider(params (string, IFileProvider)[] providers)
: base(GetCompositeContents(providers)) : base(GetCompositeContents(providers))
{ {
} }

View File

@ -14,16 +14,16 @@ namespace Microsoft.Blazor.Server.Test
public class ReferencedAssemblyFileProviderTest public class ReferencedAssemblyFileProviderTest
{ {
[Fact] [Fact]
public void RootDirContainsOnlyBinDir() public void RootDirContainsOnlyDlls()
{ {
var provider = new ReferencedAssemblyFileProvider( var provider = new ReferencedAssemblyFileProvider(
"mscorlib", "mscorlib",
new ReferencedAssemblyResolver(MonoStaticFileProvider.BclFiles, string.Empty)); new ReferencedAssemblyResolver(MonoStaticFileProvider.BclFiles, string.Empty));
Assert.Collection(provider.GetDirectoryContents("/"), item => foreach (var item in provider.GetDirectoryContents("/"))
{ {
Assert.Equal("/_bin", item.PhysicalPath); Assert.False(item.IsDirectory);
Assert.True(item.IsDirectory); Assert.EndsWith(".dll", item.Name);
}); }
} }
[Fact] [Fact]
@ -32,12 +32,12 @@ namespace Microsoft.Blazor.Server.Test
var provider = new ReferencedAssemblyFileProvider( var provider = new ReferencedAssemblyFileProvider(
"System.Linq.Expressions", "System.Linq.Expressions",
new ReferencedAssemblyResolver(MonoStaticFileProvider.BclFiles, string.Empty)); 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, Assert.Collection(contents,
item => { Assert.Equal("/_bin/mscorlib.dll", item.PhysicalPath); }, item => { Assert.Equal("/mscorlib.dll", item.PhysicalPath); },
item => { Assert.Equal("/_bin/System.Core.dll", item.PhysicalPath); }, item => { Assert.Equal("/System.Core.dll", item.PhysicalPath); },
item => { Assert.Equal("/_bin/System.dll", item.PhysicalPath); }, item => { Assert.Equal("/System.dll", item.PhysicalPath); },
item => { Assert.Equal("/_bin/System.Linq.Expressions.dll", item.PhysicalPath); }); item => { Assert.Equal("/System.Linq.Expressions.dll", item.PhysicalPath); });
} }
[Fact] [Fact]
@ -68,26 +68,26 @@ namespace Microsoft.Blazor.Server.Test
fewer assemblies from the server, and during publishing, illink would remove all the fewer assemblies from the server, and during publishing, illink would remove all the
uncalled implementation code from mscorlib.dll anyway. uncalled implementation code from mscorlib.dll anyway.
*/ */
"/_bin/Microsoft.Blazor.dll", "/Microsoft.Blazor.dll",
"/_bin/mscorlib.dll", "/mscorlib.dll",
"/_bin/netstandard.dll", "/netstandard.dll",
"/_bin/StandaloneApp.dll", "/StandaloneApp.dll",
"/_bin/System.Console.dll", "/System.Console.dll",
"/_bin/System.Core.dll", "/System.Core.dll",
"/_bin/System.Diagnostics.StackTrace.dll", "/System.Diagnostics.StackTrace.dll",
"/_bin/System.dll", "/System.dll",
"/_bin/System.Globalization.Extensions.dll", "/System.Globalization.Extensions.dll",
"/_bin/System.Runtime.dll", "/System.Runtime.dll",
"/_bin/System.Runtime.InteropServices.RuntimeInformation.dll", "/System.Runtime.InteropServices.RuntimeInformation.dll",
"/_bin/System.Runtime.Serialization.Primitives.dll", "/System.Runtime.Serialization.Primitives.dll",
"/_bin/System.Runtime.Serialization.Xml.dll", "/System.Runtime.Serialization.Xml.dll",
"/_bin/System.Security.Cryptography.Algorithms.dll", "/System.Security.Cryptography.Algorithms.dll",
"/_bin/System.Security.SecureString.dll", "/System.Security.SecureString.dll",
"/_bin/System.Xml.XPath.XDocument.dll", "/System.Xml.XPath.XDocument.dll",
}; };
// Act // Act
var contents = provider.GetDirectoryContents("/_bin") var contents = provider.GetDirectoryContents("")
.OrderBy(i => i.Name, StringComparer.InvariantCulture).ToList(); .OrderBy(i => i.Name, StringComparer.InvariantCulture).ToList();
// Assert // Assert

View File

@ -23,10 +23,8 @@ namespace Microsoft.Blazor.Common.Test
{ {
Assert.Throws<ArgumentException>(() => Assert.Throws<ArgumentException>(() =>
{ {
new CompositeMountedFileProvider(new[] new CompositeMountedFileProvider(
{ ("test", TestFileProvider("/something.txt")));
("test", TestFileProvider("/something.txt"))
});
}); });
} }
@ -35,10 +33,8 @@ namespace Microsoft.Blazor.Common.Test
{ {
Assert.Throws<ArgumentException>(() => Assert.Throws<ArgumentException>(() =>
{ {
new CompositeMountedFileProvider(new[] new CompositeMountedFileProvider(
{ ("/test/", TestFileProvider("/something.txt")));
("/test/", TestFileProvider("/something.txt"))
});
}); });
} }
@ -47,10 +43,8 @@ namespace Microsoft.Blazor.Common.Test
{ {
Assert.Throws<ArgumentException>(() => Assert.Throws<ArgumentException>(() =>
{ {
new CompositeMountedFileProvider(new[] new CompositeMountedFileProvider(
{ ("/test", TestFileProvider("something.txt")));
("/test", TestFileProvider("something.txt"))
});
}); });
} }
@ -63,10 +57,7 @@ namespace Microsoft.Blazor.Common.Test
TestItem("/rootitem.txt", "Root item contents"), TestItem("/rootitem.txt", "Root item contents"),
TestItem("/subdir/another", "Another test item"), TestItem("/subdir/another", "Another test item"),
}); });
var instance = new CompositeMountedFileProvider(new[] var instance = new CompositeMountedFileProvider(("/", childProvider));
{
("/", childProvider)
});
// Act // Act
var rootContents = instance.GetDirectoryContents(string.Empty); var rootContents = instance.GetDirectoryContents(string.Empty);
@ -99,12 +90,10 @@ namespace Microsoft.Blazor.Common.Test
public void CanMountFileProvidersAtSubPaths() public void CanMountFileProvidersAtSubPaths()
{ {
// Arrange // Arrange
var instance = new CompositeMountedFileProvider(new[] var instance = new CompositeMountedFileProvider(
{
("/dir", TestFileProvider("/first", "/A/second", "/A/third")), ("/dir", TestFileProvider("/first", "/A/second", "/A/third")),
("/dir/sub", TestFileProvider("/X", "/B/Y", "/B/Z")), ("/dir/sub", TestFileProvider("/X", "/B/Y", "/B/Z")),
("/other", TestFileProvider("/final")), ("/other", TestFileProvider("/final")));
});
// Act // Act
var rootContents = instance.GetDirectoryContents("/"); var rootContents = instance.GetDirectoryContents("/");
@ -139,11 +128,9 @@ namespace Microsoft.Blazor.Common.Test
public void CanMountMultipleFileProvidersAtSameLocation() public void CanMountMultipleFileProvidersAtSameLocation()
{ {
// Arrange // Arrange
var instance = new CompositeMountedFileProvider(new[] var instance = new CompositeMountedFileProvider(
{
("/dir", TestFileProvider("/first")), ("/dir", TestFileProvider("/first")),
("/dir", TestFileProvider("/second")) ("/dir", TestFileProvider("/second")));
});
// Act // Act
var contents = instance.GetDirectoryContents("/dir"); var contents = instance.GetDirectoryContents("/dir");
@ -159,11 +146,9 @@ namespace Microsoft.Blazor.Common.Test
{ {
Assert.Throws<ArgumentException>(() => Assert.Throws<ArgumentException>(() =>
{ {
new CompositeMountedFileProvider(new[] new CompositeMountedFileProvider(
{
("/dir", TestFileProvider("/file")), ("/dir", TestFileProvider("/file")),
("/", TestFileProvider("/dir/file")) ("/", TestFileProvider("/dir/file")));
});
}); });
} }
} }