Tidy up build filesystem providers code

This commit is contained in:
Steve Sanderson 2017-12-14 15:12:18 +00:00
parent 85cc7aee34
commit c49a67dcf0
10 changed files with 85 additions and 82 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Blazor.BuildTools.Core.FileSystem;
using System.IO;
namespace Microsoft.Blazor.BuildTools.Core
@ -9,7 +10,7 @@ namespace Microsoft.Blazor.BuildTools.Core
{
internal static void Execute(string assemblyPath, string webRootPath)
{
var clientFileSystem = ClientFileSystem.Instantiate(assemblyPath, webRootPath);
var clientFileSystem = new ClientFileProvider(assemblyPath, webRootPath);
var distDirPath = Path.Combine(Path.GetDirectoryName(assemblyPath), "dist");
FileUtil.WriteFileProviderToDisk(clientFileSystem, distDirPath, clean: true);
}

View File

@ -1,45 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
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;
namespace Microsoft.Blazor.BuildTools.Core
{
internal static class ClientFileSystem
{
public static IFileProvider Instantiate(string clientAssemblyPath, string webRootPath)
{
var fileProviders = new List<(string, IFileProvider)>();
if (!File.Exists(clientAssemblyPath))
{
throw new FileNotFoundException($"Could not find client assembly file at '{clientAssemblyPath}'.", clientAssemblyPath);
}
var frameworkFileProvider = FrameworkFileProvider.Instantiate(
clientAssemblyPath);
fileProviders.Add(("/_framework", frameworkFileProvider));
if (!string.IsNullOrEmpty(webRootPath))
{
if (!Directory.Exists(webRootPath))
{
throw new DirectoryNotFoundException($"Could not find web root directory at '{webRootPath}'.");
}
var webRootFileProvider = WebRootFileProvider.Instantiate(
webRootPath,
Path.GetFileNameWithoutExtension(clientAssemblyPath),
frameworkFileProvider.GetDirectoryContents("/_bin"));
fileProviders.Add(("/", webRootFileProvider));
}
return new CompositeMountedFileProvider(fileProviders.ToArray());
}
}
}

View File

@ -0,0 +1,63 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Blazor.Internal.Common.FileProviders;
using Microsoft.Extensions.FileProviders;
using System.Collections.Generic;
using System.IO;
namespace Microsoft.Blazor.BuildTools.Core.FileSystem
{
internal class ClientFileProvider : CompositeMountedFileProvider
{
public ClientFileProvider(string clientAssemblyPath, string webRootPath)
: base(GetContents(clientAssemblyPath, webRootPath))
{
}
private static (string, IFileProvider)[] GetContents(string clientAssemblyPath, string webRootPath)
{
var fileProviders = new List<(string, IFileProvider)>();
// There must always be a client assembly, and we always supply a /_framework
// directory containing everything needed to execute it
if (!File.Exists(clientAssemblyPath))
{
throw new FileNotFoundException($"Could not find client assembly file at '{clientAssemblyPath}'.", clientAssemblyPath);
}
var frameworkFileProvider = new FrameworkFileProvider(clientAssemblyPath);
fileProviders.Add(("/_framework", frameworkFileProvider));
// The web root directory is optional. If it exists and contains /index.html, then
// we will inject the relevant <script> tag and supply that file. Otherwise, we just
// don't supply an /index.html file.
if (TryCreateIndexHtmlFileProvider(
webRootPath, clientAssemblyPath, frameworkFileProvider, out var indexHtmlFileProvider))
{
fileProviders.Add(("/", indexHtmlFileProvider));
}
return fileProviders.ToArray();
}
private static bool TryCreateIndexHtmlFileProvider(
string webRootPath, string assemblyPath, IFileProvider frameworkFileProvider, out IFileProvider result)
{
if (!string.IsNullOrEmpty(webRootPath))
{
var path = Path.Combine(webRootPath, "index.html");
if (File.Exists(path))
{
var template = File.ReadAllText(path);
var assemblyName = Path.GetFileNameWithoutExtension(assemblyPath);
var binFiles = frameworkFileProvider.GetDirectoryContents("/_bin");
result = new IndexHtmlFileProvider(template, assemblyName, binFiles);
return true;
}
}
result = null;
return false;
}
}
}

View File

@ -7,17 +7,26 @@ using Microsoft.Blazor.Mono;
using Microsoft.Extensions.FileProviders;
using System.IO;
namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
namespace Microsoft.Blazor.BuildTools.Core.FileSystem
{
internal static class FrameworkFileProvider
internal class FrameworkFileProvider : CompositeMountedFileProvider
{
public static IFileProvider Instantiate(string clientAssemblyPath)
=> new CompositeMountedFileProvider(
public FrameworkFileProvider(string clientAssemblyPath)
: base(GetContents(clientAssemblyPath))
{
}
private static (string, IFileProvider)[] GetContents(string clientAssemblyPath)
{
return new[]
{
("/", MonoStaticFileProvider.JsFiles),
("/", BlazorBrowserFileProvider.Instance),
("/_bin", BinDirFileProvider(clientAssemblyPath)));
("/_bin", CreateBinDirFileProvider(clientAssemblyPath))
};
}
private static IFileProvider BinDirFileProvider(string clientAssemblyPath)
private static IFileProvider CreateBinDirFileProvider(string clientAssemblyPath)
=> new ReferencedAssemblyFileProvider(
Path.GetFileNameWithoutExtension(clientAssemblyPath),
new ReferencedAssemblyResolver(

View File

@ -7,7 +7,7 @@ using System.Linq;
using Mono.Cecil;
using Microsoft.Blazor.Internal.Common.FileProviders;
namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
namespace Microsoft.Blazor.BuildTools.Core.FileSystem
{
internal class ReferencedAssemblyFileProvider : InMemoryFileProvider
{

View File

@ -5,7 +5,7 @@ using Microsoft.Extensions.FileProviders;
using System.IO;
using System.Linq;
namespace Microsoft.Blazor.BuildTools.Core.FrameworkFiles
namespace Microsoft.Blazor.BuildTools.Core.FileSystem
{
internal class ReferencedAssemblyResolver
{

View File

@ -8,7 +8,7 @@ using System.Text;
using Microsoft.Extensions.FileProviders;
using System.Linq;
namespace Microsoft.Blazor.BuildTools.Core.WebRootFiles
namespace Microsoft.Blazor.BuildTools.Core.FileSystem
{
internal class IndexHtmlFileProvider : InMemoryFileProvider
{

View File

@ -1,25 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Extensions.FileProviders;
using System.Collections.Generic;
using System.IO;
namespace Microsoft.Blazor.BuildTools.Core.WebRootFiles
{
internal static class WebRootFileProvider
{
public static IFileProvider Instantiate(
string clientWebRoot, string assemblyName, IEnumerable<IFileInfo> binFiles)
=> new CompositeFileProvider(
new IndexHtmlFileProvider(
ReadIndexHtmlFile(clientWebRoot), assemblyName, binFiles),
new PhysicalFileProvider(clientWebRoot));
private static string ReadIndexHtmlFile(string clientWebRoot)
{
var path = Path.Combine(clientWebRoot, "index.html");
return File.Exists(path) ? File.ReadAllText(path) : null;
}
}
}

View File

@ -7,7 +7,7 @@ using System.Linq;
using Xunit;
using System;
using AngleSharp.Parser.Html;
using Microsoft.Blazor.BuildTools.Core.WebRootFiles;
using Microsoft.Blazor.BuildTools.Core.FileSystem;
namespace Microsoft.Blazor.Server.Test
{

View File

@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Blazor.BuildTools.Core.FrameworkFiles;
using Microsoft.Blazor.BuildTools.Core.FileSystem;
using Microsoft.Blazor.Mono;
using Mono.Cecil;
using System;