public GeneratorResults GenerateCode(RazorTemplate descriptor, string className, RazorEngineHost host) { var engine = new RazorTemplateEngine(host); GeneratorResults results; using (var fileStream = new FileStream(descriptor.FilePath, FileMode.Open, FileAccess.Read)) using (var reader = new StreamReader(fileStream)) { results = engine.GenerateCode(reader, className, host.DefaultNamespace, descriptor.ViewPath); } if (!results.Success) { throw CreateExceptionFromParserError(results.ParserErrors.Last(), descriptor.Name()); } return results; }
public GeneratorResults GenerateCode(RazorTemplate descriptor, string className, RazorEngineHost host) { var engine = new RazorTemplateEngine(host); GeneratorResults results; using (var fileStream = new FileStream(descriptor.FilePath, FileMode.Open, FileAccess.Read)) using (var reader = new StreamReader(fileStream)) { results = engine.GenerateCode(reader, className, host.DefaultNamespace, descriptor.ViewPath); } if (!results.Success) { throw CreateExceptionFromParserError(results.ParserErrors.Last(), descriptor.Name()); } return(results); }
public IFubuRazorView GetView(RazorTemplate descriptor) { Type viewType; var filePath = descriptor.FilePath; _cache.TryGetValue(filePath, out viewType); var lastModified = filePath.LastModified(); if (viewType == null || (_lastModifiedCache[filePath] != lastModified)) { viewType = getViewType(descriptor); lock (_cache) { _cache[filePath] = viewType; _lastModifiedCache[filePath] = lastModified; } } return Activator.CreateInstance(viewType).As<IFubuRazorView>(); }
private Type getViewType(RazorTemplate descriptor) { var className = ParserHelpers.SanitizeClassName(descriptor.ViewPath); var baseTemplateType = _razorEngineSettings.BaseTemplateType; var generatedClassContext = new GeneratedClassContext("Execute", "Write", "WriteLiteral", "WriteTo", "WriteLiteralTo", "FubuMVC.Razor.Rendering.TemplateHelper", "DefineSection"); var codeLanguage = RazorCodeLanguageFactory.Create(descriptor.FilePath.FileExtension()); var host = new RazorEngineHost(codeLanguage) { DefaultBaseClass = baseTemplateType.FullName, DefaultNamespace = "FubuMVC.Razor.GeneratedTemplates", GeneratedClassContext = generatedClassContext }; host.NamespaceImports.UnionWith(_commonViewNamespaces.Namespaces); var results = _templateGenerator.GenerateCode(descriptor, className, host); return _templateCompiler.Compile(className, results.GeneratedCode, host); }