From 34e20061731dc3b05f86d04a609b4535418d621c Mon Sep 17 00:00:00 2001 From: davidebbo Date: Fri, 11 Nov 2011 23:11:50 -0800 Subject: [PATCH] Switch braces to Allman style :) --- RoslynRazorViewEngine/RoslynRazorView.cs | 24 ++++++--- .../RoslynRazorViewEngine.cs | 54 ++++++++++++------- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/RoslynRazorViewEngine/RoslynRazorView.cs b/RoslynRazorViewEngine/RoslynRazorView.cs index 903598b..b39786d 100644 --- a/RoslynRazorViewEngine/RoslynRazorView.cs +++ b/RoslynRazorViewEngine/RoslynRazorView.cs @@ -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 fileExtension) { + public RoslynRazorView(string virtualPath, Type type, bool runViewStartPages, IEnumerable fileExtension) + { _type = type; _virtualPath = virtualPath; RunViewStartPages = runViewStartPages; ViewStartFileExtensions = fileExtension; } - public bool RunViewStartPages { + public bool RunViewStartPages + { get; private set; } - public IEnumerable ViewStartFileExtensions { + public IEnumerable 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"); } @@ -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); } diff --git a/RoslynRazorViewEngine/RoslynRazorViewEngine.cs b/RoslynRazorViewEngine/RoslynRazorViewEngine.cs index 334ba1b..e7f676c 100644 --- a/RoslynRazorViewEngine/RoslynRazorViewEngine.cs +++ b/RoslynRazorViewEngine/RoslynRazorViewEngine.cs @@ -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" @@ -52,26 +55,31 @@ 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); @@ -79,7 +87,8 @@ public class RoslynRazorViewEngine : VirtualPathProviderViewEngine, IVirtualPath Type type = (Type)HttpRuntime.Cache[cacheKey]; - if (type == null) { + if (type == null) + { DateTime utcStart = DateTime.UtcNow; type = GetTypeFromVirtualPathNoCache(virtualPath); @@ -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); @@ -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); } @@ -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(); - foreach (Assembly referencedAssembly in BuildManager.GetReferencedAssemblies()) { + foreach (Assembly referencedAssembly in BuildManager.GetReferencedAssemblies()) + { references.Add(new AssemblyFileReference(referencedAssembly.Location)); } @@ -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; @@ -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); } }