Remove manifest generation

This commit is contained in:
Pranav K 2017-06-08 13:58:16 -07:00
parent 911b8e0571
commit 60d47561b9
39 changed files with 72 additions and 214 deletions

View File

@ -13,7 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02F7AA35-91A
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{0398AFFF-505E-4283-89DA-BBD9D28B53DB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests", "test\Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests\Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests.csproj", "{46C9A4B2-8B1C-451B-B670-C194901D66AC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunctionalTests", "test\FunctionalTests\FunctionalTests.csproj", "{46C9A4B2-8B1C-451B-B670-C194901D66AC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Test", "test\Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Test\Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Test.csproj", "{E0D75B4E-839F-4F80-9B1F-B33F616BCC5F}"
EndProject

View File

@ -0,0 +1,46 @@
// 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.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
{
internal static class AssemblyMetadataGenerator
{
public static CSharpCompilation AddAssemblyMetadata(
CSharpCompiler compiler,
CSharpCompilation compilation,
CompilationOptions compilationOptions)
{
if (!string.IsNullOrEmpty(compilationOptions.KeyFile))
{
var updatedOptions = compilation.Options.WithStrongNameProvider(new DesktopStrongNameProvider());
var keyFilePath = Path.GetFullPath(compilationOptions.KeyFile);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || compilationOptions.PublicSign)
{
updatedOptions = updatedOptions.WithCryptoPublicKey(
SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFilePath)));
}
else
{
updatedOptions = updatedOptions.WithCryptoKeyFile(keyFilePath)
.WithDelaySign(compilationOptions.DelaySign);
}
compilation = compilation.WithOptions(updatedOptions);
}
var applicationAssemblyName = Assembly.Load(new AssemblyName(compilationOptions.ApplicationName)).GetName();
var assemblyVersionContent = $"[assembly:{typeof(AssemblyVersionAttribute).FullName}(\"{applicationAssemblyName.Version}\")]";
var syntaxTree = compiler.CreateSyntaxTree(SourceText.From(assemblyVersionContent));
return compilation.AddSyntaxTrees(syntaxTree);
}
}
}

View File

@ -1,149 +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 System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
{
internal class ManifestGenerator
{
public ManifestGenerator(
CSharpCompiler compiler,
CSharpCompilation compilation)
{
Compiler = compiler;
Compilation = compilation;
}
public CSharpCompiler Compiler { get; }
public CSharpCompilation Compilation { get; private set; }
public void GenerateManifest(IList<ViewCompilationInfo> results)
{
var views = new List<ViewCompilationInfo>();
var pages = new List<ViewCompilationInfo>();
for (var i = 0; i < results.Count; i++)
{
var result = results[i];
if (result.RouteTemplate != null)
{
pages.Add(result);
}
else
{
views.Add(result);
}
}
GeneratePageManifest(pages);
GenerateViewManifest(views);
}
private void GenerateViewManifest(IList<ViewCompilationInfo> result)
{
if (result.Count == 0)
{
return;
}
var precompiledViewsArray = new StringBuilder();
foreach (var item in result)
{
var path = item.ViewFileInfo.ViewEnginePath;
precompiledViewsArray.AppendLine(
$"new global::{typeof(ViewInfo).FullName}(@\"{path}\", typeof({item.TypeName})),");
}
var factoryContent = $@"
namespace {ViewsFeatureProvider.ViewInfoContainerNamespace}
{{
public class {ViewsFeatureProvider.ViewInfoContainerTypeName} : global::{typeof(ViewInfoContainer).FullName}
{{
public {ViewsFeatureProvider.ViewInfoContainerTypeName}() : base(new[]
{{
{precompiledViewsArray}
}})
{{
}}
}}
}}";
var syntaxTree = Compiler.CreateSyntaxTree(SourceText.From(factoryContent));
Compilation = Compilation.AddSyntaxTrees(syntaxTree);
}
private void GeneratePageManifest(IList<ViewCompilationInfo> pages)
{
if (pages.Count == 0)
{
return;
}
var precompiledViewsArray = new StringBuilder();
foreach (var item in pages)
{
var path = item.ViewFileInfo.ViewEnginePath;
var routeTemplate = item.RouteTemplate;
var escapedRouteTemplate = routeTemplate.Replace("\"", "\\\"");
precompiledViewsArray.AppendLine(
$"new global::{typeof(CompiledPageInfo).FullName}(@\"{path}\", typeof({item.TypeName}), \"{escapedRouteTemplate}\"),");
}
var factoryContent = $@"
namespace {CompiledPageFeatureProvider.CompiledPageManifestNamespace}
{{
public class {CompiledPageFeatureProvider.CompiledPageManifestTypeName} : global::{typeof(CompiledPageManifest).FullName}
{{
public {CompiledPageFeatureProvider.CompiledPageManifestTypeName}() : base(new[]
{{
{precompiledViewsArray}
}})
{{
}}
}}
}}";
var syntaxTree = Compiler.CreateSyntaxTree(SourceText.From(factoryContent));
Compilation = Compilation.AddSyntaxTrees(syntaxTree);
}
public void AddAssemblyMetadata(
AssemblyName applicationAssemblyName,
CompilationOptions compilationOptions)
{
if (!string.IsNullOrEmpty(compilationOptions.KeyFile))
{
var updatedOptions = Compilation.Options.WithStrongNameProvider(new DesktopStrongNameProvider());
var keyFilePath = Path.GetFullPath(compilationOptions.KeyFile);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || compilationOptions.PublicSign)
{
updatedOptions = updatedOptions.WithCryptoPublicKey(
SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFilePath)));
}
else
{
updatedOptions = updatedOptions.WithCryptoKeyFile(keyFilePath)
.WithDelaySign(compilationOptions.DelaySign);
}
Compilation = Compilation.WithOptions(updatedOptions);
}
var assemblyVersionContent = $"[assembly:{typeof(AssemblyVersionAttribute).FullName}(\"{applicationAssemblyName.Version}\")]";
var syntaxTree = Compiler.CreateSyntaxTree(SourceText.From(assemblyVersionContent));
Compilation = Compilation.AddSyntaxTrees(syntaxTree);
}
}
}

View File

@ -4,18 +4,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.CommandLineUtils;
@ -181,25 +176,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
});
compilation = compilation.AddSyntaxTrees(syntaxTrees);
Parallel.For(0, results.Length, ParalellOptions, i =>
{
results[i].TypeName = ReadTypeInfo(compilation, syntaxTrees[i]);
});
// Post process the compilation - run ExpressionRewritter and any user specified callbacks.
compilation = ExpressionRewriter.Rewrite(compilation);
var compilationContext = new RoslynCompilationContext(compilation);
MvcServiceProvider.ViewEngineOptions.CompilationCallback(compilationContext);
compilation = compilationContext.Compilation;
var codeGenerator = new ManifestGenerator(compiler, compilation);
codeGenerator.GenerateManifest(results);
var assemblyName = new AssemblyName(Options.ApplicationName);
assemblyName = Assembly.Load(assemblyName).GetName();
codeGenerator.AddAssemblyMetadata(assemblyName, Options);
return codeGenerator.Compilation;
compilation = AssemblyMetadataGenerator.AddAssemblyMetadata(
compiler,
compilationContext.Compilation,
Options);
return compilation;
}
private bool ParseArguments()
@ -247,11 +234,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
compilationInfo = new ViewCompilationInfo(fileInfo, csharpDocument);
}
if (PageDirectiveFeature.TryGetPageDirective(fileInfo.CreateReadStream, out var template))
{
compilationInfo.RouteTemplate = template ?? string.Empty;
}
results[i] = compilationInfo;
});
@ -277,21 +259,5 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
return viewFileInfo;
}
private string ReadTypeInfo(CSharpCompilation compilation, SyntaxTree syntaxTree)
{
var semanticModel = compilation.GetSemanticModel(syntaxTree, ignoreAccessibility: true);
var classDeclarations = syntaxTree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>();
foreach (var declaration in classDeclarations)
{
var typeSymbol = semanticModel.GetDeclaredSymbol(declaration);
if (typeSymbol.ContainingType == null && typeSymbol.DeclaredAccessibility == Accessibility.Public)
{
return typeSymbol.ToDisplayString();
}
}
return null;
}
}
}

View File

@ -1,12 +1,11 @@
// 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.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
{
internal class ViewCompilationInfo
internal struct ViewCompilationInfo
{
public ViewCompilationInfo(
ViewFileInfo viewFileInfo,
@ -19,9 +18,5 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
public ViewFileInfo ViewFileInfo { get; }
public RazorCSharpDocument CSharpDocument { get; }
public string TypeName { get; set; }
public string RouteTemplate { get; set; }
}
}

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class ApplicationConsumingPrecompiledViews
: IClassFixture<ApplicationConsumingPrecompiledViews.ApplicationConsumingPrecompiledViewsFixture>

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class ApplicationUsingRelativePathsTest :
IClassFixture<ApplicationUsingRelativePathsTest.ApplicationUsingRelativePathsTestFixture>

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class ApplicationWithConfigureMvcTest
: IClassFixture<ApplicationWithConfigureMvcTest.ApplicationWithConfigureMvcFixture>

View File

@ -9,7 +9,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests
namespace FunctionalTests
{
public class ApplicationWithCustomInputFilesTest
: IClassFixture<ApplicationWithCustomInputFilesTest.ApplicationWithCustomInputFilesTestFixture>

View File

@ -10,7 +10,7 @@ using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class ApplicationWithParseErrorsTest
{

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class TagHelperTest : IClassFixture<TagHelperTest.ApplicationWithTagHelpersFixture>
{

View File

@ -4,7 +4,7 @@
using System;
using System.IO;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public static class ApplicationPaths
{

View File

@ -9,7 +9,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public abstract class ApplicationTestFixture : IDisposable
{

View File

@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.Extensions.Logging;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public static class HttpClientExtensions
{

View File

@ -5,7 +5,7 @@ using System.IO;
using System.Reflection;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public static class TestEmbeddedResource
{

View File

@ -9,7 +9,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests
namespace FunctionalTests
{
public class PublishWithEmbedViewSourcesTest
: IClassFixture<PublishWithEmbedViewSourcesTest.PublishWithEmbedViewSourcesTestFixture>

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class RazorPagesAppTest : IClassFixture<RazorPagesAppTest.TestFixture>
{

View File

@ -6,7 +6,7 @@ using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public static class RuntimeFlavors
{

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class SimpleAppTest : IClassFixture<SimpleAppTest.SimpleAppTestFixture>
{

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class SimpleAppWithAssemblyRenameTest : IClassFixture<SimpleAppWithAssemblyRenameTest.TestFixture>
{

View File

@ -5,7 +5,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class SimpleAppX86DesktopOnlyTest : IClassFixture<SimpleAppX86DesktopOnlyTest.SimpleAppX86DesktopOnlyFixture>
{

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class StrongNamedAppTest : IClassFixture<StrongNamedAppTest.StrongNamedAppFixture>
{

View File

@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
namespace FunctionalTests
{
public class ViewCompilationOptionsTest : IClassFixture<ViewCompilationOptionsTest.TestFixture>
{