Moving MvcRazorHost to a separate assembly

This commit is contained in:
Pranav K 2014-01-29 15:20:48 -08:00
parent cb8a32e5a6
commit a6393e77ab
9 changed files with 140 additions and 57 deletions

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
VisualStudioVersion = 12.0.30110.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}"
EndProject
@ -23,6 +23,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcSample.net45", "samples\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcSample.k10", "samples\MvcSample\MvcSample.k10.csproj", "{A7D7CD66-A407-4144-8AB7-07F895F87137}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Mvc.Razor.Host.net45", "src\Microsoft.AspNet.Mvc.Razor.Host\Microsoft.AspNet.Mvc.Razor.Host.net45.csproj", "{A25566C9-97C7-4770-BBC1-9B815D3B1380}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Mvc.Razor.Host.k10", "src\Microsoft.AspNet.Mvc.Razor.Host\Microsoft.AspNet.Mvc.Razor.Host.k10.csproj", "{613579ED-7B96-4F5C-8DBF-D2C580257BE2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -61,18 +65,28 @@ Global
{A7D7CD66-A407-4144-8AB7-07F895F87137}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7D7CD66-A407-4144-8AB7-07F895F87137}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7D7CD66-A407-4144-8AB7-07F895F87137}.Release|Any CPU.Build.0 = Release|Any CPU
{A25566C9-97C7-4770-BBC1-9B815D3B1380}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A25566C9-97C7-4770-BBC1-9B815D3B1380}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A25566C9-97C7-4770-BBC1-9B815D3B1380}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A25566C9-97C7-4770-BBC1-9B815D3B1380}.Release|Any CPU.Build.0 = Release|Any CPU
{613579ED-7B96-4F5C-8DBF-D2C580257BE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{613579ED-7B96-4F5C-8DBF-D2C580257BE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{613579ED-7B96-4F5C-8DBF-D2C580257BE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{613579ED-7B96-4F5C-8DBF-D2C580257BE2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{501817DD-8143-4A50-888D-99896A82CD12} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{A7D7CD66-A407-4144-8AB7-07F895F87137} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{64BD92BA-F313-496C-ACE6-B9C4C10C420E} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{BA88E212-5889-48DC-823F-A3A67DDEF123} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{28AA31B2-6504-432D-8599-1501FE0B100A} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{24947215-86AB-4DD0-B94E-88B1EC4DC877} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{365CA5C6-037D-4E6E-AF05-F5FA42E024EB} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{0D2C8C3D-AFC3-4E1F-A6D8-9F690EE5E063} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{501817DD-8143-4A50-888D-99896A82CD12} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{A7D7CD66-A407-4144-8AB7-07F895F87137} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{A25566C9-97C7-4770-BBC1-9B815D3B1380} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{613579ED-7B96-4F5C-8DBF-D2C580257BE2} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
EndGlobalSection
EndGlobal

View File

@ -22,9 +22,11 @@ namespace MvcSample
string appRoot = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
var fileSystem = new PhysicalFileSystem(appRoot);
serviceProvider.AddInstance<IFileSystem>(new VirtualFileSystem(fileSystem));
serviceProvider.AddInstance<ICompilationService>(new RazorCompilationService(new CscBasedCompilationService()));
serviceProvider.AddInstance<IFileSystem>(new PhysicalFileSystem(appRoot));
serviceProvider.Add<IVirtualFileSystem, VirtualFileSystem>();
serviceProvider.Add<IMvcRazorHost, MvcRazorHost>();
serviceProvider.Add<ICompilationService, CscBasedCompilationService>();
serviceProvider.Add<IRazorCompilationService, RazorCompilationService>();
serviceProvider.Add<IVirtualPathViewFactory, VirtualPathViewFactory>();
serviceProvider.Add<IViewEngine, RazorViewEngine>();

View File

@ -4,8 +4,10 @@
"Microsoft.AspNet.FileSystems": "0.1-alpha-*",
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
"Microsoft.AspNet.Razor" : "0.1-alpha-*",
"Microsoft.AspNet.Mvc" : "",
"Microsoft.AspNet.Mvc.Razor" : ""
"Microsoft.AspNet.Mvc.Razor" : "",
"Microsoft.AspNet.Mvc.Razor.Host" : ""
},
"configurations": {
"net45": {

View File

@ -0,0 +1,10 @@
using System.IO;
using Microsoft.AspNet.Razor;
namespace Microsoft.AspNet.Mvc.Razor
{
public interface IMvcRazorHost
{
GeneratorResults GenerateCode(string rootRelativePath, Stream inputStream);
}
}

View File

@ -0,0 +1,87 @@
using System;
using System.IO;
using System.Text;
using Microsoft.AspNet.Razor;
using Microsoft.AspNet.Razor.Generator;
namespace Microsoft.AspNet.Mvc.Razor
{
public class MvcRazorHost : RazorEngineHost, IMvcRazorHost
{
private static readonly string[] _defaultNamespaces = new[]
{
"System",
"System.Linq",
"System.Collections.Generic",
"Microsoft.AspNet.Mvc",
"Microsoft.AspNet.Mvc.Razor"
};
public MvcRazorHost(Type baseType)
: this(baseType.FullName)
{
}
public MvcRazorHost(string baseType)
: base(new CSharpRazorCodeLanguage())
{
DefaultBaseClass = baseType;
GeneratedClassContext = new GeneratedClassContext(
executeMethodName: "Execute",
writeMethodName: "Write",
writeLiteralMethodName: "WriteLiteral",
writeToMethodName: "WriteTo",
writeLiteralToMethodName: "WriteLiteralTo",
templateTypeName: "Template",
defineSectionMethodName: "DefineSection")
{
ResolveUrlMethodName = "Href"
};
foreach (var ns in _defaultNamespaces)
{
NamespaceImports.Add(ns);
}
}
public GeneratorResults GenerateCode(string rootRelativePath, Stream inputStream)
{
string className = Path.GetFileNameWithoutExtension(rootRelativePath);
if (rootRelativePath.StartsWith("~/", StringComparison.Ordinal))
{
rootRelativePath = rootRelativePath.Substring(2);
}
string classNamespace = GenerateNamespace(rootRelativePath);
using (var reader = new StreamReader(inputStream))
{
var engine = new RazorTemplateEngine(this);
return engine.GenerateCode(reader, className, classNamespace, rootRelativePath);
}
}
private static string GenerateNamespace(string rootRelativePath)
{
var namespaceBuilder = new StringBuilder(rootRelativePath.Length);
rootRelativePath = Path.GetDirectoryName(rootRelativePath);
for (int i = 0; i < rootRelativePath.Length; i++)
{
char c = rootRelativePath[i];
if (c == Path.DirectorySeparatorChar)
{
namespaceBuilder.Append('.');
}
else if (!Char.IsLetterOrDigit(c))
{
namespaceBuilder.Append('_');
}
else
{
namespaceBuilder.Append(c);
}
}
return namespaceBuilder.ToString();
}
}
}

View File

@ -0,0 +1,10 @@
{
"version" : "0.1-alpha-*",
"dependencies": {
"Microsoft.AspNet.Razor": "0.1-alpha-*"
},
"configurations": {
"net45": {},
"k10" : {}
}
}

View File

@ -1,40 +0,0 @@
using Microsoft.AspNet.Razor;
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Parser;
namespace Microsoft.AspNet.Mvc.Razor
{
public class MvcRazorHost : RazorEngineHost
{
private static readonly string[] _namespaces = new[]
{
"System",
"System.Linq",
"System.Collections.Generic",
"Microsoft.AspNet.Mvc",
"Microsoft.AspNet.Mvc.Razor"
};
public MvcRazorHost()
: base(new CSharpRazorCodeLanguage())
{
DefaultBaseClass = typeof(RazorView).FullName;
GeneratedClassContext = new GeneratedClassContext(
executeMethodName: "Execute",
writeMethodName: "Write",
writeLiteralMethodName: "WriteLiteral",
writeToMethodName: "WriteTo",
writeLiteralToMethodName: "WriteLiteralTo",
templateTypeName: "Template",
defineSectionMethodName: "DefineSection")
{
ResolveUrlMethodName = "Href"
};
foreach (var ns in _namespaces)
{
NamespaceImports.Add(ns);
}
}
}
}

View File

@ -12,10 +12,12 @@ namespace Microsoft.AspNet.Mvc.Razor
{
private static readonly CompilerCache _cache = new CompilerCache();
private readonly ICompilationService _baseCompilationService;
private readonly IMvcRazorHost _razorHost;
public RazorCompilationService(ICompilationService compilationService)
public RazorCompilationService(ICompilationService compilationService, IMvcRazorHost razorHost)
{
_baseCompilationService = compilationService;
_razorHost = razorHost;
}
public Task<CompilationResult> Compile(IFileInfo file)
@ -25,15 +27,10 @@ namespace Microsoft.AspNet.Mvc.Razor
private async Task<CompilationResult> CompileCore(IFileInfo file)
{
var host = new MvcRazorHost();
var engine = new RazorTemplateEngine(host);
var namespaceBuilder = GenerateNamespace(file);
GeneratorResults results;
using (TextReader rdr = new StreamReader(file.CreateReadStream()))
using (Stream inputStream = file.CreateReadStream())
{
results = engine.GenerateCode(rdr, '_' + file.Name, namespaceBuilder.ToString(), file.PhysicalPath ?? file.Name);
results = _razorHost.GenerateCode(file.PhysicalPath, inputStream);
}
if (!results.Success)

View File

@ -6,10 +6,11 @@
"Microsoft.AspNet.DependencyInjection": "0.1-alpha-*",
"Microsoft.AspNet.Razor": "0.1-alpha-*",
"Microsoft.AspNet.Mvc" : "",
"Microsoft.AspNet.Mvc.Forms" : ""
"Microsoft.AspNet.Mvc.Forms" : "",
"Microsoft.AspNet.Mvc.Razor.Host" : ""
},
"configurations": {
"net45": {},
"k10" : { }
"k10" : {}
}
}