Skip to content

Use System.Reflection.Metadata to generate BootConfig #17156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public static void Command(CommandLineApplication command)
"The path to a file that lists the paths to given referenced dll files",
CommandOptionType.SingleValue);

var embeddedResourcesFile = command.Option("--embedded-resources",
"The path to a file that lists the paths of .NET assemblies that may contain embedded resources (typically, referenced assemblies in their pre-linked states)",
CommandOptionType.SingleValue);

var outputPath = command.Option("--output",
"Path to the output file",
CommandOptionType.SingleValue);
Expand All @@ -44,14 +40,9 @@ public static void Command(CommandLineApplication command)
? File.ReadAllLines(referencesFile.Value())
: Array.Empty<string>();

var embeddedResourcesSources = embeddedResourcesFile.HasValue()
? File.ReadAllLines(embeddedResourcesFile.Value())
: Array.Empty<string>();

BootJsonWriter.WriteFile(
mainAssemblyPath.Value,
referencesSources,
embeddedResourcesSources,
linkerEnabledFlag.HasValue(),
outputPath.Value());
return 0;
Expand Down
69 changes: 22 additions & 47 deletions src/Components/Blazor/Build/src/Core/BootJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using Microsoft.AspNetCore.Components;
using Mono.Cecil;

namespace Microsoft.AspNetCore.Blazor.Build
{
Expand All @@ -16,79 +15,55 @@ internal class BootJsonWriter
public static void WriteFile(
string assemblyPath,
string[] assemblyReferences,
string[] embeddedResourcesSources,
bool linkerEnabled,
string outputPath)
{
var embeddedContent = EmbeddedResourcesProcessor.ExtractEmbeddedResources(
embeddedResourcesSources, Path.GetDirectoryName(outputPath));
var bootJsonText = GetBootJsonContent(
Path.GetFileName(assemblyPath),
GetAssemblyEntryPoint(assemblyPath),
AssemblyName.GetAssemblyName(assemblyPath).Name,
assemblyReferences,
embeddedContent,
linkerEnabled);
var normalizedOutputPath = Path.GetFullPath(outputPath);
Console.WriteLine("Writing boot data to: " + normalizedOutputPath);
File.WriteAllText(normalizedOutputPath, bootJsonText);
}

public static string GetBootJsonContent(string assemblyFileName, string entryPoint, string[] assemblyReferences, IEnumerable<EmbeddedResourceInfo> embeddedContent, bool linkerEnabled)
public static string GetBootJsonContent(string entryAssembly, string[] assemblyReferences, bool linkerEnabled)
{
var data = new BootJsonData(
assemblyFileName,
entryPoint,
entryAssembly,
assemblyReferences,
embeddedContent,
linkerEnabled);
return JsonSerializer.Serialize(data, JsonSerializerOptionsProvider.Options);
}

private static string GetAssemblyEntryPoint(string assemblyPath)
{
using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyPath))
{
var entryPoint = assemblyDefinition.EntryPoint;
if (entryPoint == null)
{
throw new ArgumentException($"The assembly at {assemblyPath} has no specified entry point.");
}

return $"{entryPoint.DeclaringType.FullName}::{entryPoint.Name}";
}
}

/// <summary>
/// Defines the structure of a Blazor boot JSON file
/// </summary>
class BootJsonData
readonly struct BootJsonData
{
public string Main { get; }
public string EntryPoint { get; }
public IEnumerable<string> AssemblyReferences { get; }
public IEnumerable<string> CssReferences { get; }
public IEnumerable<string> JsReferences { get; }
/// <summary>
/// Gets the name of the assembly with the application entry point
/// </summary>
public string EntryAssembly { get; }

/// <summary>
/// Gets the closure of assemblies to be loaded by Blazor WASM. This includes the application entry assembly.
/// </summary>
public IEnumerable<string> Assemblies { get; }

/// <summary>
/// Gets a value that determines if the linker is enabled.
/// </summary>
public bool LinkerEnabled { get; }

public BootJsonData(
string entrypointAssemblyWithExtension,
string entryPoint,
IEnumerable<string> assemblyReferences,
IEnumerable<EmbeddedResourceInfo> embeddedContent,
string entryAssembly,
IEnumerable<string> assemblies,
bool linkerEnabled)
{
Main = entrypointAssemblyWithExtension;
EntryPoint = entryPoint;
AssemblyReferences = assemblyReferences;
EntryAssembly = entryAssembly;
Assemblies = assemblies;
LinkerEnabled = linkerEnabled;

CssReferences = embeddedContent
.Where(c => c.Kind == EmbeddedResourceKind.Css)
.Select(c => c.RelativePath);

JsReferences = embeddedContent
.Where(c => c.Kind == EmbeddedResourceKind.JavaScript)
.Select(c => c.RelativePath);
}
}
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading