Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Commit dbe93ac

Browse files
committed
Adding Razor Precompilation
1 parent 6f57223 commit dbe93ac

File tree

75 files changed

+2805
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2805
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ nuget.exe
3535
*.sln.ide
3636
node_modules
3737
*launchSettings.json
38-
*.orig
38+
*.orig
39+
BuildInfo.generated.cs

MvcPrecompilation.sln

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Mvc.Razor.Precompilation.Design", "src\Microsoft.AspNetCore.Mvc.Razor.Precompilation.Design\Microsoft.AspNetCore.Mvc.Razor.Precompilation.Design.xproj", "{4339FC9B-AEC6-442A-B413-A41555ED76C7}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFiles", "SolutionFiles", "{01707B64-7DC7-4B5A-B0BB-7CD2773AA297}"
9+
ProjectSection(SolutionItems) = preProject
10+
global.json = global.json
11+
NuGet.config = NuGet.config
12+
EndProjectSection
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02F7AA35-91AF-491E-9F0E-03CFAF86C720}"
15+
EndProject
16+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tools", "src\Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tools\Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tools.xproj", "{F8BF7D95-0633-407F-BB0B-02563F13C068}"
17+
EndProject
18+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{0398AFFF-505E-4283-89DA-BBD9D28B53DB}"
19+
EndProject
20+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests", "test\Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests\Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests.xproj", "{46C9A4B2-8B1C-451B-B670-C194901D66AC}"
21+
EndProject
22+
Global
23+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
24+
Debug|Any CPU = Debug|Any CPU
25+
Release|Any CPU = Release|Any CPU
26+
EndGlobalSection
27+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
28+
{4339FC9B-AEC6-442A-B413-A41555ED76C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{4339FC9B-AEC6-442A-B413-A41555ED76C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{4339FC9B-AEC6-442A-B413-A41555ED76C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{4339FC9B-AEC6-442A-B413-A41555ED76C7}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{46C9A4B2-8B1C-451B-B670-C194901D66AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{46C9A4B2-8B1C-451B-B670-C194901D66AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{46C9A4B2-8B1C-451B-B670-C194901D66AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{46C9A4B2-8B1C-451B-B670-C194901D66AC}.Release|Any CPU.Build.0 = Release|Any CPU
40+
EndGlobalSection
41+
GlobalSection(SolutionProperties) = preSolution
42+
HideSolutionNode = FALSE
43+
EndGlobalSection
44+
GlobalSection(NestedProjects) = preSolution
45+
{4339FC9B-AEC6-442A-B413-A41555ED76C7} = {02F7AA35-91AF-491E-9F0E-03CFAF86C720}
46+
{F8BF7D95-0633-407F-BB0B-02563F13C068} = {02F7AA35-91AF-491E-9F0E-03CFAF86C720}
47+
{46C9A4B2-8B1C-451B-B670-C194901D66AC} = {0398AFFF-505E-4283-89DA-BBD9D28B53DB}
48+
EndGlobalSection
49+
EndGlobal

NuGetPackageVerifier.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"adx": { // Packages written by the ADX team and that ship on NuGet.org
3+
"rules": [
4+
"AdxVerificationCompositeRule"
5+
],
6+
"packages": {
7+
"Microsoft.AspNetCore.Mvc.Razor.Precompilation.Design": { },
8+
"Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tools": { }
9+
}
10+
},
11+
"Default": { // Rules to run for packages not listed in any other set.
12+
"rules": [
13+
"DefaultCompositeRule"
14+
]
15+
}
16+
}

TestApps.sln

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SimpleApp", "testapps\SimpleApp\SimpleApp.xproj", "{8FA176ED-C29E-48D6-BC7A-1C3A862CB15F}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{43488AEE-CCF2-4A90-B890-05320282BE29}"
9+
ProjectSection(SolutionItems) = preProject
10+
testapps\global.json = testapps\global.json
11+
testapps\NuGet.config = testapps\NuGet.config
12+
EndProjectSection
13+
EndProject
14+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApplicationWithTagHelpers", "testapps\ApplicationWithTagHelpers\ApplicationWithTagHelpers.xproj", "{2079872F-E8F9-4DB1-A340-C0D897807B86}"
15+
EndProject
16+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ClassLibraryTagHelper", "testapps\ClassLibraryTagHelper\ClassLibraryTagHelper.xproj", "{39EFA075-3673-49AB-95F3-AA5E88DF6C30}"
17+
EndProject
18+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApplicationWithConfigureMvc", "testapps\ApplicationWithConfigureMvc\ApplicationWithConfigureMvc.xproj", "{E2EAEB85-91D5-478E-9CE2-964F68DE20D0}"
19+
EndProject
20+
Global
21+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
22+
Debug|Any CPU = Debug|Any CPU
23+
Release|Any CPU = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
26+
{8FA176ED-C29E-48D6-BC7A-1C3A862CB15F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{8FA176ED-C29E-48D6-BC7A-1C3A862CB15F}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{8FA176ED-C29E-48D6-BC7A-1C3A862CB15F}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{8FA176ED-C29E-48D6-BC7A-1C3A862CB15F}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{2079872F-E8F9-4DB1-A340-C0D897807B86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{2079872F-E8F9-4DB1-A340-C0D897807B86}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{2079872F-E8F9-4DB1-A340-C0D897807B86}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{2079872F-E8F9-4DB1-A340-C0D897807B86}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{39EFA075-3673-49AB-95F3-AA5E88DF6C30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{39EFA075-3673-49AB-95F3-AA5E88DF6C30}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{39EFA075-3673-49AB-95F3-AA5E88DF6C30}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{39EFA075-3673-49AB-95F3-AA5E88DF6C30}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{E2EAEB85-91D5-478E-9CE2-964F68DE20D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{E2EAEB85-91D5-478E-9CE2-964F68DE20D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{E2EAEB85-91D5-478E-9CE2-964F68DE20D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{E2EAEB85-91D5-478E-9CE2-964F68DE20D0}.Release|Any CPU.Build.0 = Release|Any CPU
42+
EndGlobalSection
43+
GlobalSection(SolutionProperties) = preSolution
44+
HideSolutionNode = FALSE
45+
EndGlobalSection
46+
EndGlobal

build.cmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@ECHO OFF
2+
PowerShell -NoProfile -NoLogo -ExecutionPolicy unrestricted -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = '';& '%~dp0build.ps1' %*; exit $LASTEXITCODE"

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"projects": ["src", "test/WebSites", "samples"]
2+
"projects": ["src", "samples"]
33
}

makefile.shade

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use namespace="System.IO"
2+
3+
-BuildQuality = "preview3";
4+
5+
use-standard-lifecycle
6+
k-standard-goals
7+
8+
#repo-initialize target='initialize'
9+
dotnet command='restore src test ${E("KOREBUILD_DOTNET_RESTORE_OPTIONS")}' workingDir='${Directory.GetCurrentDirectory()}' if='!NoRestore'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.Extensions.CommandLineUtils;
5+
6+
namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Design.Internal
7+
{
8+
public class CommonOptions
9+
{
10+
public static readonly string ConfigureCompilationTypeTemplate = "--configure-compilation-type";
11+
public static readonly string ContentRootTemplate = "--content-root";
12+
13+
public CommandArgument ProjectArgument { get; private set; }
14+
15+
public CommandOption ConfigureCompilationType { get; private set; }
16+
17+
public CommandOption ContentRootOption { get; private set; }
18+
19+
public void Configure(CommandLineApplication app)
20+
{
21+
app.Description = "Precompiles an application.";
22+
app.HelpOption("-?|-h|--help");
23+
24+
ProjectArgument = app.Argument(
25+
"project",
26+
"The path to the project (project folder or project.json) with precompilation.");
27+
28+
ConfigureCompilationType = app.Option(
29+
ConfigureCompilationTypeTemplate,
30+
"Type with Configure method",
31+
CommandOptionType.SingleValue);
32+
33+
ContentRootOption = app.Option(
34+
ContentRootTemplate,
35+
"The application's content root.",
36+
CommandOptionType.SingleValue);
37+
}
38+
}
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#if DEBUG
5+
using System;
6+
using System.Diagnostics;
7+
using System.Linq;
8+
9+
namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Design.Internal
10+
{
11+
public static class DebugHelper
12+
{
13+
public static void HandleDebugSwitch(ref string[] args)
14+
{
15+
if (args.Length > 0 && string.Equals("--debug", args[0], StringComparison.OrdinalIgnoreCase))
16+
{
17+
args = args.Skip(1).ToArray();
18+
Console.WriteLine("Waiting for debugger to attach. Press ENTER to continue");
19+
Console.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}");
20+
Console.ReadLine();
21+
}
22+
}
23+
}
24+
}
25+
#endif
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using System.Reflection;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.Hosting.Internal;
10+
using Microsoft.AspNetCore.Mvc.Internal;
11+
using Microsoft.AspNetCore.Mvc.Razor.Internal;
12+
using Microsoft.Extensions.DependencyInjection;
13+
using Microsoft.Extensions.FileProviders;
14+
using Microsoft.Extensions.ObjectPool;
15+
using Microsoft.Extensions.Options;
16+
17+
namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Design.Internal
18+
{
19+
public class MvcServiceProvider
20+
{
21+
private readonly string _projectPath;
22+
private readonly string _contentRoot;
23+
private readonly string _applicationName;
24+
25+
public MvcServiceProvider(
26+
string projectPath,
27+
string applicationName,
28+
string contentRoot,
29+
string configureCompilationType)
30+
{
31+
_projectPath = projectPath;
32+
_contentRoot = contentRoot;
33+
_applicationName = applicationName;
34+
35+
var mvcBuilderConfiguration = GetConfigureCompilationAction(configureCompilationType);
36+
var serviceProvider = GetProvider(mvcBuilderConfiguration);
37+
38+
Host = serviceProvider.GetRequiredService<IMvcRazorHost>();
39+
Compiler = serviceProvider.GetRequiredService<CSharpCompiler>();
40+
ViewEngineOptions = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>().Value;
41+
FileProvider = serviceProvider.GetRequiredService<IRazorViewEngineFileProviderAccessor>().FileProvider;
42+
}
43+
44+
public IMvcRazorHost Host { get; }
45+
46+
public CSharpCompiler Compiler { get; }
47+
48+
public IFileProvider FileProvider { get; }
49+
50+
public RazorViewEngineOptions ViewEngineOptions { get; }
51+
52+
private IDesignTimeMvcBuilderConfiguration GetConfigureCompilationAction(string configureCompilationType)
53+
{
54+
Type type;
55+
if (!string.IsNullOrEmpty(configureCompilationType))
56+
{
57+
type = Type.GetType(configureCompilationType);
58+
if (type == null)
59+
{
60+
throw new InvalidOperationException($"Unable to find type '{type}.");
61+
}
62+
}
63+
else
64+
{
65+
var assemblyName = new AssemblyName(_applicationName);
66+
var assembly = Assembly.Load(assemblyName);
67+
type = assembly
68+
.GetExportedTypes()
69+
.FirstOrDefault(typeof(IDesignTimeMvcBuilderConfiguration).IsAssignableFrom);
70+
}
71+
72+
if (type == null)
73+
{
74+
return null;
75+
}
76+
77+
var instance = Activator.CreateInstance(type) as IDesignTimeMvcBuilderConfiguration;
78+
if (instance == null)
79+
{
80+
throw new InvalidOperationException($"Type {configureCompilationType} does not implement " +
81+
$"{typeof(IDesignTimeMvcBuilderConfiguration)}.");
82+
}
83+
84+
return instance;
85+
}
86+
87+
private IServiceProvider GetProvider(IDesignTimeMvcBuilderConfiguration mvcBuilderConfiguration)
88+
{
89+
var services = new ServiceCollection();
90+
91+
var hostingEnvironment = new HostingEnvironment
92+
{
93+
ApplicationName = _applicationName,
94+
WebRootFileProvider = new PhysicalFileProvider(_projectPath),
95+
ContentRootFileProvider = new PhysicalFileProvider(_contentRoot),
96+
ContentRootPath = _contentRoot,
97+
};
98+
var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
99+
100+
services
101+
.AddSingleton<IHostingEnvironment>(hostingEnvironment)
102+
.AddSingleton<DiagnosticSource>(diagnosticSource)
103+
.AddLogging()
104+
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
105+
106+
var mvcCoreBuilder = services
107+
.AddMvcCore()
108+
.AddRazorViewEngine();
109+
110+
var mvcBuilder = new MvcBuilder(mvcCoreBuilder.Services, mvcCoreBuilder.PartManager);
111+
mvcBuilderConfiguration?.ConfigureMvc(mvcBuilder);
112+
113+
return mvcBuilder.Services.BuildServiceProvider();
114+
}
115+
}
116+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Reflection;
6+
using Microsoft.Extensions.CommandLineUtils;
7+
8+
namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Design.Internal
9+
{
10+
public class PrecompilationApplication : CommandLineApplication
11+
{
12+
private readonly Type _callingType;
13+
14+
public PrecompilationApplication(Type callingType)
15+
{
16+
_callingType = callingType;
17+
18+
Name = "razor-precompile";
19+
FullName = "Microsoft Razor Precompilation Utility";
20+
Description = "Precompiles Razor views.";
21+
ShortVersionGetter = GetInformationalVersion;
22+
23+
HelpOption("-?|-h|--help");
24+
25+
OnExecute(() =>
26+
{
27+
ShowHelp();
28+
return 2;
29+
});
30+
}
31+
32+
public new int Execute(params string[] args)
33+
{
34+
try
35+
{
36+
return base.Execute(args);
37+
}
38+
catch (Exception ex)
39+
{
40+
Console.Error.WriteLine(ex.Message);
41+
#if DEBUG
42+
Console.Error.WriteLine(ex);
43+
#endif
44+
return 1;
45+
}
46+
}
47+
48+
private string GetInformationalVersion()
49+
{
50+
var assembly = _callingType.GetTypeInfo().Assembly;
51+
var attributes = assembly.GetCustomAttributes(
52+
typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute[];
53+
54+
var versionAttribute = attributes.Length == 0 ?
55+
assembly.GetName().Version.ToString() :
56+
attributes[0].InformationalVersion;
57+
58+
return versionAttribute;
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)