Use new file provider to serve Microsoft.Blazor.Browser and Microsoft.Blazor.Mono contents

This commit is contained in:
Steve Sanderson 2017-12-11 14:05:00 +00:00
parent 0ed4a4eba5
commit 867cb66b97
5 changed files with 41 additions and 49 deletions

View File

@ -1,31 +1,15 @@
// 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 Microsoft.Extensions.Primitives;
using System;
namespace Microsoft.Blazor.Browser
{
public class BlazorBrowserFileProvider : IFileProvider
public static class BlazorBrowserFileProvider
{
private EmbeddedFileProvider _embeddedFiles = new EmbeddedFileProvider(
public static IFileProvider Instance = new EmbeddedResourceFileProvider(
typeof(BlazorBrowserFileProvider).Assembly,
"blazor");
public static BlazorBrowserFileProvider Instance = new BlazorBrowserFileProvider();
private BlazorBrowserFileProvider()
{
}
public IFileInfo GetFileInfo(string subpath)
=> _embeddedFiles.GetFileInfo(subpath.Replace('/', '$'));
public IDirectoryContents GetDirectoryContents(string subpath)
=> throw new NotImplementedException(); // Don't need to support this
public IChangeToken Watch(string filter)
=> throw new NotImplementedException(); // Don't need to support this
"blazor.");
}
}

View File

@ -17,6 +17,7 @@
<ItemGroup>
<ProjectReference Include="..\Microsoft.Blazor.BuildTools\Microsoft.Blazor.BuildTools.csproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Microsoft.Blazor.Common\Microsoft.Blazor.Common.csproj" />
</ItemGroup>
<Import Project="..\Microsoft.Blazor.BuildTools\local.props" />
@ -30,7 +31,7 @@
<RemoveDir Directories="dist" />
<Exec Command="npm run build" />
<ItemGroup>
<EmbeddedResource Include="dist\blazor.js" LogicalName="blazor.$blazor.js" />
<EmbeddedResource Include="dist\blazor.js" LogicalName="blazor./blazor.js" />
</ItemGroup>
</Target>
</Project>

View File

@ -0,0 +1,29 @@
// 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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Microsoft.Blazor.Internal.Common.FileProviders
{
public class EmbeddedResourceFileProvider : InMemoryFileProvider
{
public EmbeddedResourceFileProvider(Assembly assembly, string resourceNamePrefix)
: base(ReadEmbeddedResources(assembly, resourceNamePrefix))
{
}
private static IEnumerable<(string, Stream)> ReadEmbeddedResources(
Assembly assembly, string resourceNamePrefix)
{
return assembly.GetManifestResourceNames()
.Where(name => name.StartsWith(resourceNamePrefix, StringComparison.Ordinal))
.Select(name => (
name.Substring(resourceNamePrefix.Length),
assembly.GetManifestResourceStream(name)));
}
}
}

View File

@ -7,14 +7,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.0.0" />
<ProjectReference Include="..\Microsoft.Blazor.Common\Microsoft.Blazor.Common.csproj" />
</ItemGroup>
<Target Name="EmbedMonoResources" BeforeTargets="PreBuildEvent" DependsOnTargets="OptimizeMono">
<ItemGroup>
<MonoResourcesToEmbed Include="$(MonoOptimizedDir)**" />
<EmbeddedResource Include="@(MonoResourcesToEmbed)">
<LogicalName>mono.$([System.String]::Copy('/%(RecursiveDir)%(FileName)%(Extension)').Replace('\', '$').Replace('/', '$'))</LogicalName>
<LogicalName>mono.$([System.String]::Copy('/%(RecursiveDir)%(FileName)%(Extension)').Replace('\', '/'))</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>

View File

@ -1,36 +1,14 @@
// 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 Microsoft.Extensions.Primitives;
using System;
namespace Microsoft.Blazor.Mono
{
public class MonoStaticFileProvider : IFileProvider
public static class MonoStaticFileProvider
{
private EmbeddedFileProvider _embeddedFiles = new EmbeddedFileProvider(
typeof(MonoStaticFileProvider).Assembly,
"mono");
public static MonoStaticFileProvider Instance = new MonoStaticFileProvider();
private MonoStaticFileProvider()
{
}
public IFileInfo GetFileInfo(string subpath)
{
// EmbeddedFileProvider can't find resources whose names include '/' (or '\'),
// so the resources in the assembly use '$' as a directory separator
var possibleResourceName = subpath.Replace('/', '$');
return _embeddedFiles.GetFileInfo(possibleResourceName);
}
public IDirectoryContents GetDirectoryContents(string subpath)
=> _embeddedFiles.GetDirectoryContents(subpath);
public IChangeToken Watch(string filter)
=> throw new NotImplementedException(); // Don't need to support this
public readonly static IFileProvider Instance = new EmbeddedResourceFileProvider(
typeof(MonoStaticFileProvider).Assembly, "mono.");
}
}