Skip to content

Commit

Permalink
Switch braces to Allman style :)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebbo committed Nov 12, 2011
1 parent 539747f commit 34e2006
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
24 changes: 16 additions & 8 deletions RoslynRazorViewEngine/RoslynRazorView.cs
Expand Up @@ -4,34 +4,41 @@
using System.Web.Mvc;
using System.Web.WebPages;

namespace RoslynRazorViewEngine {
public class RoslynRazorView : IView {
namespace RoslynRazorViewEngine
{
public class RoslynRazorView : IView
{
private readonly Type _type;
private readonly string _virtualPath;

public RoslynRazorView(string virtualPath, Type type, bool runViewStartPages, IEnumerable<string> fileExtension) {
public RoslynRazorView(string virtualPath, Type type, bool runViewStartPages, IEnumerable<string> fileExtension)
{
_type = type;
_virtualPath = virtualPath;
RunViewStartPages = runViewStartPages;
ViewStartFileExtensions = fileExtension;
}

public bool RunViewStartPages {
public bool RunViewStartPages
{
get;
private set;
}

public IEnumerable<string> ViewStartFileExtensions {
public IEnumerable<string> ViewStartFileExtensions
{
get;
private set;
}

public void Render(ViewContext viewContext, TextWriter writer) {
public void Render(ViewContext viewContext, TextWriter writer)
{
object instance = Activator.CreateInstance(_type);

WebViewPage webViewPage = instance as WebViewPage;

if (webViewPage == null) {
if (webViewPage == null)
{
throw new InvalidOperationException("Invalid view type");
}

Expand All @@ -41,7 +48,8 @@ public class RoslynRazorView : IView {
webViewPage.InitHelpers();

WebPageRenderingBase startPage = null;
if (this.RunViewStartPages) {
if (this.RunViewStartPages)
{
startPage = StartPage.GetStartPage(webViewPage, "_ViewStart", ViewStartFileExtensions);
}

Expand Down
54 changes: 36 additions & 18 deletions RoslynRazorViewEngine/RoslynRazorViewEngine.cs
Expand Up @@ -18,9 +18,12 @@
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

namespace RoslynRazorViewEngine {
public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPathFactory {
public RoslynRazorViewEngine() {
namespace RoslynRazorViewEngine
{
public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPathFactory
{
public RoslynRazorViewEngine()
{
base.AreaViewLocationFormats = new[] {
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
Expand Down Expand Up @@ -52,34 +55,40 @@ public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPath
};
}

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) {
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
Type type = GetTypeFromVirtualPath(partialPath);
return new RoslynRazorView(partialPath, type, false, base.FileExtensions);
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) {
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
Type type = GetTypeFromVirtualPath(viewPath);
return new RoslynRazorView(viewPath, type, true, base.FileExtensions);
}

public object CreateInstance(string virtualPath) {
public object CreateInstance(string virtualPath)
{
Type type = GetTypeFromVirtualPath(virtualPath);
return Activator.CreateInstance(type);
}

public bool Exists(string virtualPath) {
public bool Exists(string virtualPath)
{
return FileExists(controllerContext: null, virtualPath: virtualPath);
}

private Type GetTypeFromVirtualPath(string virtualPath) {
private Type GetTypeFromVirtualPath(string virtualPath)
{

virtualPath = VirtualPathUtility.ToAbsolute(virtualPath);

string cacheKey = "RoslynRazor_" + virtualPath;

Type type = (Type)HttpRuntime.Cache[cacheKey];

if (type == null) {
if (type == null)
{
DateTime utcStart = DateTime.UtcNow;

type = GetTypeFromVirtualPathNoCache(virtualPath);
Expand All @@ -92,7 +101,8 @@ public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPath
return type;
}

private Type GetTypeFromVirtualPathNoCache(string virtualPath) {
private Type GetTypeFromVirtualPathNoCache(string virtualPath)
{
// Use the Razor engine to generate source code from the view
WebPageRazorHost host = WebRazorHostFactory.CreateHostFromConfig(virtualPath);
string code = GenerateCodeFromRazorTemplate(host, virtualPath);
Expand All @@ -103,19 +113,23 @@ public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPath
return assembly.GetType(String.Format(CultureInfo.CurrentCulture, "{0}.{1}", host.DefaultNamespace, host.DefaultClassName));
}

private string GenerateCodeFromRazorTemplate(WebPageRazorHost host, string virtualPath) {
private string GenerateCodeFromRazorTemplate(WebPageRazorHost host, string virtualPath)
{

// Create Razor engine and use it to generate a CodeCompileUnit
var engine = new RazorTemplateEngine(host);
GeneratorResults results = null;
VirtualFile file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath);
using (var stream = file.Open()) {
using (TextReader reader = new StreamReader(stream)) {
using (var stream = file.Open())
{
using (TextReader reader = new StreamReader(stream))
{
results = engine.GenerateCode(reader, className: null, rootNamespace: null, sourceFileName: host.PhysicalPath);
}
}

if (!results.Success) {
if (!results.Success)
{
throw CreateExceptionFromParserError(results.ParserErrors.Last(), virtualPath);
}

Expand All @@ -127,13 +141,15 @@ public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPath
return srcFileWriter.ToString();
}

private Assembly CompileCodeIntoAssembly(string code, string virtualPath) {
private Assembly CompileCodeIntoAssembly(string code, string virtualPath)
{
// Parse the source file using Roslyn
var syntaxTree = SyntaxTree.ParseCompilationUnit(code);

// Add all the references we need for the compilation
var references = new List<AssemblyFileReference>();
foreach (Assembly referencedAssembly in BuildManager.GetReferencedAssemblies()) {
foreach (Assembly referencedAssembly in BuildManager.GetReferencedAssemblies())
{
references.Add(new AssemblyFileReference(referencedAssembly.Location));
}

Expand All @@ -146,7 +162,8 @@ public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPath
var memStream = new MemoryStream();
EmitResult emitResult = compilation.Emit(memStream);

if (!emitResult.Success) {
if (!emitResult.Success)
{
Diagnostic diagnostic = emitResult.Diagnostics.First();
string message = diagnostic.Info.ToString();
LinePosition linePosition = diagnostic.Location.GetLineSpan(usePreprocessorDirectives: true).StartLinePosition;
Expand All @@ -157,7 +174,8 @@ public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPath
return Assembly.Load(memStream.GetBuffer());
}

private HttpParseException CreateExceptionFromParserError(RazorError error, string virtualPath) {
private HttpParseException CreateExceptionFromParserError(RazorError error, string virtualPath)
{
return new HttpParseException(error.Message + Environment.NewLine, null, virtualPath, null, error.Location.LineIndex + 1);
}
}
Expand Down

0 comments on commit 34e2006

Please sign in to comment.