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

Commit 1c5840a

Browse files
committed
Add dotnet-razor-precompile tool
1 parent b7a0393 commit 1c5840a

26 files changed

+1249
-63
lines changed

Mvc.NoFun.sln

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 14
3-
VisualStudioVersion = 14.0.24720.0
3+
VisualStudioVersion = 14.0.25420.1
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}"
66
EndProject
@@ -81,6 +81,10 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Mvc.Te
8181
EndProject
8282
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MvcSandbox", "samples\MvcSandbox\MvcSandbox.xproj", "{14ED4476-9F24-4776-8417-EA6927F6C9C9}"
8383
EndProject
84+
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}"
85+
EndProject
86+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Mvc.Razor.Precompilation", "src\Microsoft.AspNetCore.Mvc.Razor.Precompilation\Microsoft.AspNetCore.Mvc.Razor.Precompilation.xproj", "{EBB55C82-6E61-4A0D-8AD8-86915696E86E}"
87+
EndProject
8488
Global
8589
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8690
Debug|Any CPU = Debug|Any CPU
@@ -464,6 +468,30 @@ Global
464468
{14ED4476-9F24-4776-8417-EA6927F6C9C9}.Release|Mixed Platforms.Build.0 = Release|Any CPU
465469
{14ED4476-9F24-4776-8417-EA6927F6C9C9}.Release|x86.ActiveCfg = Release|Any CPU
466470
{14ED4476-9F24-4776-8417-EA6927F6C9C9}.Release|x86.Build.0 = Release|Any CPU
471+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
472+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Debug|Any CPU.Build.0 = Debug|Any CPU
473+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
474+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
475+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Debug|x86.ActiveCfg = Debug|Any CPU
476+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Debug|x86.Build.0 = Debug|Any CPU
477+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Release|Any CPU.ActiveCfg = Release|Any CPU
478+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Release|Any CPU.Build.0 = Release|Any CPU
479+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
480+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Release|Mixed Platforms.Build.0 = Release|Any CPU
481+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Release|x86.ActiveCfg = Release|Any CPU
482+
{F8BF7D95-0633-407F-BB0B-02563F13C068}.Release|x86.Build.0 = Release|Any CPU
483+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
484+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Debug|Any CPU.Build.0 = Debug|Any CPU
485+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
486+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
487+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Debug|x86.ActiveCfg = Debug|Any CPU
488+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Debug|x86.Build.0 = Debug|Any CPU
489+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Release|Any CPU.ActiveCfg = Release|Any CPU
490+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Release|Any CPU.Build.0 = Release|Any CPU
491+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
492+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Release|Mixed Platforms.Build.0 = Release|Any CPU
493+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Release|x86.ActiveCfg = Release|Any CPU
494+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E}.Release|x86.Build.0 = Release|Any CPU
467495
EndGlobalSection
468496
GlobalSection(SolutionProperties) = preSolution
469497
HideSolutionNode = FALSE
@@ -502,5 +530,7 @@ Global
502530
{8FC726B5-E766-44E0-8B38-1313B6D8D9A7} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
503531
{9879B5D5-2325-4A81-B4DF-F279FE8FEEB4} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
504532
{14ED4476-9F24-4776-8417-EA6927F6C9C9} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
533+
{F8BF7D95-0633-407F-BB0B-02563F13C068} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
534+
{EBB55C82-6E61-4A0D-8AD8-86915696E86E} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
505535
EndGlobalSection
506536
EndGlobal
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Reflection;
9+
using Microsoft.DotNet.Cli.Utils;
10+
using Microsoft.DotNet.InternalAbstractions;
11+
using Microsoft.DotNet.ProjectModel;
12+
using Microsoft.Extensions.CommandLineUtils;
13+
using Microsoft.Extensions.Internal;
14+
using NuGet.Frameworks;
15+
16+
namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Internal
17+
{
18+
public class PrecompileDispatchCommand : PrecompileCommandBase
19+
{
20+
private CommandOption NoBuildOption { get; set; }
21+
22+
protected override void Configure(CommandLineApplication app)
23+
{
24+
base.Configure(app);
25+
NoBuildOption = app.Option(
26+
"--no-build",
27+
"Do not build project before compiling views.",
28+
CommandOptionType.NoValue);
29+
}
30+
31+
protected override int ExecuteCore()
32+
{
33+
if (!NoBuildOption.HasValue())
34+
{
35+
var exitCode = BuildProject();
36+
if (exitCode != 0)
37+
{
38+
return exitCode;
39+
}
40+
}
41+
42+
var dispatchArgs = new List<string>
43+
{
44+
ProjectPath,
45+
"--framework",
46+
TargetFramework.ToString(),
47+
"--configuration",
48+
Configuration,
49+
};
50+
51+
string outputPath = null;
52+
if (OutputPathOption.HasValue())
53+
{
54+
outputPath = OutputPathOption.Value();
55+
56+
dispatchArgs.Add("--output");
57+
dispatchArgs.Add(outputPath);
58+
}
59+
60+
if (ConfigureCompilationType.HasValue())
61+
{
62+
dispatchArgs.Add("--configure-compilation-type");
63+
dispatchArgs.Add(ConfigureCompilationType.Value());
64+
}
65+
66+
if (ContentRootOption.HasValue())
67+
{
68+
dispatchArgs.Add("--content-root");
69+
dispatchArgs.Add(ContentRootOption.Value());
70+
}
71+
72+
if (GeneratePdbOption.HasValue())
73+
{
74+
dispatchArgs.Add("--generate-pdbs");
75+
}
76+
77+
var toolName = typeof(Precompilation.Program).GetTypeInfo().Assembly.GetName().Name;
78+
var dispatchCommand = DotnetToolDispatcher.CreateDispatchCommand(
79+
dispatchArgs,
80+
TargetFramework,
81+
Configuration,
82+
outputPath: outputPath,
83+
buildBasePath: null,
84+
projectDirectory: ProjectPath,
85+
toolName: toolName);
86+
87+
var commandExitCode = dispatchCommand
88+
.ForwardStdErr(Console.Error)
89+
.ForwardStdOut(Console.Out)
90+
.Execute()
91+
.ExitCode;
92+
93+
var buildOutputPath = GetBuildOutputPath();
94+
var updatedBinary = Directory.EnumerateFiles(buildOutputPath, $"*{ModifiedAssemblyExtension}").FirstOrDefault();
95+
if (updatedBinary == null)
96+
{
97+
Console.Error.WriteLine("Unable to find modified binary with precompiled views.");
98+
return 1;
99+
}
100+
else
101+
{
102+
File.Copy(
103+
updatedBinary,
104+
Path.ChangeExtension(updatedBinary, ".dll"),
105+
overwrite: true);
106+
File.Delete(updatedBinary);
107+
}
108+
109+
return commandExitCode;
110+
}
111+
112+
private int BuildProject()
113+
{
114+
var workspace = new BuildWorkspace(ProjectReaderSettings.ReadFromEnvironment());
115+
116+
var projectContext = workspace.GetProjectContext(ProjectPath, NuGetFramework.Parse(FrameworkOption.Value()));
117+
if (projectContext == null)
118+
{
119+
Console.Error.WriteLine($"Project '{ProjectPath}' does not support framework: {FrameworkOption.Value()}");
120+
return 1;
121+
}
122+
123+
projectContext = workspace.GetRuntimeContext(
124+
projectContext,
125+
RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers());
126+
127+
var arguments = new List<string>
128+
{
129+
ProjectPath,
130+
"--framework",
131+
projectContext.TargetFramework.ToString(),
132+
};
133+
134+
if (ConfigurationOption.HasValue())
135+
{
136+
arguments.Add("--configuration");
137+
arguments.Add(ConfigurationOption.Value());
138+
}
139+
140+
if (OutputPathOption.HasValue())
141+
{
142+
arguments.Add("--output");
143+
arguments.Add(OutputPathOption.Value());
144+
}
145+
146+
return Command.CreateDotNet("build", arguments, NuGetFramework.Parse(FrameworkOption.Value()), ConfigurationOption.Value())
147+
.ForwardStdErr(Console.Error)
148+
.ForwardStdOut(Console.Out)
149+
.Execute()
150+
.ExitCode;
151+
}
152+
}
153+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project ToolsVersion="14.0.25420" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25420</VisualStudioVersion>
55
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
66
</PropertyGroup>
7-
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
88
<PropertyGroup Label="Globals">
9-
<ProjectGuid>6bb4c20b-24c0-45d6-9e4c-c2620959bdd5</ProjectGuid>
9+
<ProjectGuid>f8bf7d95-0633-407f-bb0b-02563f13c068</ProjectGuid>
10+
<RootNamespace>Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tools</RootNamespace>
1011
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
1112
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
1213
</PropertyGroup>
14+
1315
<PropertyGroup>
1416
<SchemaVersion>2.0</SchemaVersion>
1517
</PropertyGroup>
16-
<ItemGroup>
17-
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
18-
</ItemGroup>
19-
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
18+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
2019
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.AspNetCore.Mvc.Razor.Precompilation.Internal;
5+
6+
namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tools
7+
{
8+
public class Program
9+
{
10+
public static int Main(string[] args)
11+
{
12+
#if DEBUG
13+
DebugHelper.HandleDebugSwitch(ref args);
14+
#endif
15+
16+
var app = new PrecompilationApplication(typeof(Program));
17+
PrecompileCommandBase.Register<PrecompileDispatchCommand>(app);
18+
return app.Execute(args);
19+
}
20+
}
21+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"version": "1.0.0-*",
3+
"description": "Razor precompilation",
4+
"packOptions": {
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/AspNet/Mvc"
8+
},
9+
"tags": [
10+
"cshtml",
11+
"razor",
12+
"precompilation",
13+
"aspnetcore"
14+
]
15+
},
16+
"buildOptions": {
17+
"outputName": "dotnet-razor-precompile",
18+
"keyFile": "../../tools/Key.snk",
19+
"warningsAsErrors": true,
20+
"emitEntryPoint": true,
21+
"nowarn": [
22+
"CS1591"
23+
],
24+
"xmlDoc": true
25+
},
26+
"dependencies": {
27+
"Microsoft.AspNetCore.Hosting": "1.1.0-*",
28+
"Microsoft.AspNetCore.Mvc": "1.1.0-*",
29+
"Microsoft.DotNet.Cli.Utils": "1.0.0-*",
30+
"Microsoft.Extensions.CommandLineUtils": "1.1.0-*",
31+
"Microsoft.AspNetCore.Mvc.Razor.Precompilation": "1.0.0-*",
32+
"Microsoft.Extensions.DotnetToolDispatcher.Sources": {
33+
"type": "build",
34+
"version": "1.1.0-*"
35+
}
36+
},
37+
"frameworks": {
38+
"netcoreapp1.0": {
39+
"dependencies": {
40+
"Microsoft.DotNet.ProjectModel.Loader": "1.0.0-*",
41+
"Microsoft.NETCore.App": {
42+
"type": "platform",
43+
"version": "1.0.0-*"
44+
},
45+
"System.Diagnostics.Process": "4.1.0-*",
46+
"System.Runtime.Serialization.Primitives": "4.1.1-*"
47+
}
48+
},
49+
"net46": {}
50+
}
51+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.AspNetCore.Mvc.Razor.Internal;
7+
using Microsoft.Cci;
8+
using Microsoft.Cci.MutableCodeModel;
9+
10+
namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Internal
11+
{
12+
public class AddResourcesRewriter : MetadataRewriter
13+
{
14+
private readonly List<CompileOutputs> _outputs;
15+
private IAssembly _assembly;
16+
17+
public AddResourcesRewriter(IMetadataHost host, List<CompileOutputs> outputs)
18+
: base(host)
19+
{
20+
_outputs = outputs;
21+
}
22+
23+
public override IAssembly Rewrite(IAssembly assembly)
24+
{
25+
_assembly = assembly;
26+
return _assembly;
27+
}
28+
29+
public override List<IResourceReference> Rewrite(List<IResourceReference> resourceReferences)
30+
{
31+
if (resourceReferences == null)
32+
{
33+
resourceReferences = new List<IResourceReference>();
34+
}
35+
36+
foreach (var output in _outputs)
37+
{
38+
var dllResource = $"{PrecompiledViewsFeatureProvider.PrecompiledResourcePrefix}{output.RelativePath}.dll";
39+
resourceReferences.Add(new ResourceReference
40+
{
41+
Name = host.NameTable.GetNameFor(dllResource),
42+
DefiningAssembly = _assembly,
43+
IsPublic = true,
44+
Resource = new Resource
45+
{
46+
Data = output.AssemblyStream.ToArray().ToList(),
47+
IsPublic = true,
48+
DefiningAssembly = _assembly,
49+
}
50+
});
51+
52+
var pdbResource = $"{PrecompiledViewsFeatureProvider.PrecompiledResourcePrefix}{output.RelativePath}.pdb";
53+
resourceReferences.Add(new ResourceReference
54+
{
55+
Name = host.NameTable.GetNameFor(pdbResource),
56+
DefiningAssembly = _assembly,
57+
IsPublic = true,
58+
Resource = new Resource
59+
{
60+
Data = output.PdbStream.ToArray().ToList(),
61+
IsPublic = true,
62+
DefiningAssembly = _assembly,
63+
}
64+
});
65+
}
66+
67+
return resourceReferences;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)