public GeneratorResults GenerateCode(string rootRelativePath, Stream inputStream) { StreamReader sr = new StreamReader(inputStream); var className = MainClassNamePrefix + ParserHelpers.SanitizeClassName(rootRelativePath); var engine = new RazorTemplateEngine(_host); return engine.GenerateCode(GenerateStreamFromString(MinifyChunk(sr.ReadToEnd())), className, DefaultNamespace, rootRelativePath); }
public GeneratorResults Generate(Type modelType,string template) { //准备临时类名,读取模板文件和Razor代码生成器 string templateType = "YOYO.AspNetCore.ViewEngine.Razor.RazorViewTemplate"; string templateTypeName = modelType != null ? string.Format(templateType + @"<{0}>", modelType.FullName) : templateType; var class_name = "c" + Guid.NewGuid().ToString("N"); var host = new RazorEngineHost(new CSharpRazorCodeLanguage(), () => new HtmlMarkupParser()) { DefaultBaseClass = templateTypeName, DefaultClassName = class_name, DefaultNamespace = "YOYO.AspNetCore.ViewEngine.Razor", GeneratedClassContext = new GeneratedClassContext("Execute", "Write", "WriteLiteral", "WriteTo", "WriteLiteralTo", "RazorViewTemplate.Dynamic", new GeneratedTagHelperContext()) }; host.NamespaceImports.Add("System"); host.NamespaceImports.Add("System.Dynamic"); host.NamespaceImports.Add("System.Linq"); host.NamespaceImports.Add("System.Collections.Generic"); var engine = new RazorTemplateEngine(host); return engine.GenerateCode(new StringReader(template)); ; }
private static void GenerateCodeFile(string file, string @namespace, int iterations) { var basePath = Path.GetDirectoryName(file); var fileName = Path.GetFileName(file); var fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName); var codeLang = new CSharpRazorCodeLanguage(); var host = new MvcRazorHost(new DefaultChunkTreeCache(new PhysicalFileProvider(basePath))); var engine = new RazorTemplateEngine(host); Console.WriteLine("Press the ANY key to start."); Console.ReadLine(); Console.WriteLine($"Starting Code Generation: {file}"); var timer = Stopwatch.StartNew(); for (var i = 0; i < iterations; i++) { using (var fileStream = File.OpenText(file)) { var code = engine.GenerateCode( input: fileStream, className: fileNameNoExtension, rootNamespace: Path.GetFileName(@namespace), sourceFileName: fileName); } Console.WriteLine("Completed iteration: " + (i + 1)); } Console.WriteLine($"Completed after {timer.Elapsed}"); }
/// <summary> /// Gets an ordered <see cref="IReadOnlyList{ChunkTreeResult}"/> of parsed <see cref="ChunkTree"/>s and /// file paths for each <c>_ViewImports</c> that is applicable to the page located at /// <paramref name="pagePath"/>. The list is ordered so that the <see cref="ChunkTreeResult"/>'s /// <see cref="ChunkTreeResult.ChunkTree"/> for the <c>_ViewImports</c> closest to the /// <paramref name="pagePath"/> in the file system appears first. /// </summary> /// <param name="pagePath">The path of the page to locate inherited chunks for.</param> /// <returns>A <see cref="IReadOnlyList{ChunkTreeResult}"/> of parsed <c>_ViewImports</c> /// <see cref="ChunkTree"/>s and their file paths.</returns> /// <remarks> /// The resulting <see cref="IReadOnlyList{ChunkTreeResult}"/> is ordered so that the result /// for a _ViewImport closest to the application root appears first and the _ViewImport /// closest to the page appears last i.e. /// [ /_ViewImport, /Views/_ViewImport, /Views/Home/_ViewImport ] /// </remarks> public virtual IReadOnlyList<ChunkTreeResult> GetInheritedChunkTreeResults(string pagePath) { if (pagePath == null) { throw new ArgumentNullException(nameof(pagePath)); } var inheritedChunkTreeResults = new List<ChunkTreeResult>(); var templateEngine = new RazorTemplateEngine(_razorHost); foreach (var viewImportsPath in ViewHierarchyUtility.GetViewImportsLocations(pagePath)) { // viewImportsPath contains the app-relative path of the _ViewImports. // Since the parsing of a _ViewImports would cause parent _ViewImports to be parsed // we need to ensure the paths are app-relative to allow the GetGlobalFileLocations // for the current _ViewImports to succeed. var chunkTree = _chunkTreeCache.GetOrAdd( viewImportsPath, fileInfo => ParseViewFile( templateEngine, fileInfo, viewImportsPath)); if (chunkTree != null) { var result = new ChunkTreeResult(chunkTree, viewImportsPath); inheritedChunkTreeResults.Insert(0, result); } } return inheritedChunkTreeResults; }
private static ChunkTree ParseViewFile( RazorTemplateEngine engine, IFileInfo fileInfo, string viewImportsPath) { using (var stream = fileInfo.CreateReadStream()) { using (var streamReader = new StreamReader(stream)) { var parseResults = engine.ParseTemplate(streamReader, viewImportsPath); var className = ParserHelpers.SanitizeClassName(fileInfo.Name); var language = engine.Host.CodeLanguage; var chunkGenerator = language.CreateChunkGenerator( className, engine.Host.DefaultNamespace, viewImportsPath, engine.Host); chunkGenerator.Visit(parseResults); // Rewrite the location of inherited chunks so they point to the global import file. var chunkTree = chunkGenerator.Context.ChunkTreeBuilder.Root; foreach (var chunk in chunkTree.Children) { chunk.Start = new SourceLocation( viewImportsPath, chunk.Start.AbsoluteIndex, chunk.Start.LineIndex, chunk.Start.CharacterIndex); } return chunkTree; } } }
/// <inheritdoc /> public GeneratorResults GenerateCode(string rootRelativePath, Stream inputStream) { var className = ParserHelpers.SanitizeClassName(rootRelativePath); var engine = new RazorTemplateEngine(this); return engine.GenerateCode(inputStream, className, DefaultNamespace, rootRelativePath); }