public virtual void CompileViews([NotNull] IBeforeCompileContext context) { var descriptors = CreateCompilationDescriptors(context); if (descriptors.Count > 0) { var collectionGenerator = new RazorFileInfoCollectionGenerator( descriptors, SyntaxTreeGenerator.GetParseOptions(context.CSharpCompilation)); var tree = collectionGenerator.GenerateCollection(); context.CSharpCompilation = context.CSharpCompilation.AddSyntaxTrees(tree); } }
protected virtual PrecompilationCacheEntry GetCacheEntry([NotNull] RelativeFileInfo fileInfo) { using (var stream = fileInfo.FileInfo.CreateReadStream()) { var host = GetRazorHost(); var results = host.GenerateCode(fileInfo.RelativePath, stream); if (results.Success) { var syntaxTree = SyntaxTreeGenerator.Generate(results.GeneratedCode, fileInfo.FileInfo.PhysicalPath, CompilationSettings); var fullTypeName = results.GetMainClassName(host, syntaxTree); if (fullTypeName != null) { var hashAlgorithmVersion = RazorFileHash.HashAlgorithmVersion1; var hash = RazorFileHash.GetHash(fileInfo.FileInfo, hashAlgorithmVersion); var razorFileInfo = new RazorFileInfo { RelativePath = fileInfo.RelativePath, LastModified = fileInfo.FileInfo.LastModified, Length = fileInfo.FileInfo.Length, FullTypeName = fullTypeName, Hash = hash, HashAlgorithmVersion = hashAlgorithmVersion }; return(new PrecompilationCacheEntry(razorFileInfo, syntaxTree)); } } else { var diagnostics = results.ParserErrors .Select(error => error.ToDiagnostics(fileInfo.FileInfo.PhysicalPath)) .ToList(); return(new PrecompilationCacheEntry(diagnostics)); } } return(null); }
protected virtual IReadOnlyList <RazorFileInfo> CreateCompilationDescriptors( [NotNull] IBeforeCompileContext context) { var options = SyntaxTreeGenerator.GetParseOptions(context.CSharpCompilation); var list = new List <RazorFileInfo>(); foreach (var info in GetFileInfosRecursive(string.Empty)) { var descriptor = ParseView(info, context, options); if (descriptor != null) { list.Add(descriptor); } } return(list); }
protected virtual RazorFileInfo ParseView([NotNull] RelativeFileInfo fileInfo, [NotNull] IBeforeCompileContext context, [NotNull] CSharpParseOptions options) { using (var stream = fileInfo.FileInfo.CreateReadStream()) { var results = _host.GenerateCode(fileInfo.RelativePath, stream); foreach (var parserError in results.ParserErrors) { var diagnostic = parserError.ToDiagnostics(fileInfo.FileInfo.PhysicalPath); context.Diagnostics.Add(diagnostic); } var generatedCode = results.GeneratedCode; if (generatedCode != null) { var syntaxTree = SyntaxTreeGenerator.Generate(generatedCode, fileInfo.FileInfo.PhysicalPath, options); var fullTypeName = results.GetMainClassName(_host, syntaxTree); if (fullTypeName != null) { context.CSharpCompilation = context.CSharpCompilation.AddSyntaxTrees(syntaxTree); var hash = RazorFileHash.GetHash(fileInfo.FileInfo); return(new RazorFileInfo() { FullTypeName = fullTypeName, RelativePath = fileInfo.RelativePath, LastModified = fileInfo.FileInfo.LastModified, Length = fileInfo.FileInfo.Length, Hash = hash, }); } } } return(null); }
public virtual SyntaxTree GenerateCollection() { var builder = new StringBuilder(); builder.Append(Top); foreach (var fileInfo in FileInfos) { var perFileEntry = GenerateFile(fileInfo); builder.Append(perFileEntry); } builder.Append(Bottom); var sourceCode = builder.ToString(); var syntaxTree = SyntaxTreeGenerator.Generate(sourceCode, "__AUTO__GeneratedViewsCollection.cs", Options); return(syntaxTree); }
public virtual SyntaxTree GenerateCollection() { var builder = new StringBuilder(); builder.AppendFormat(CultureInfo.InvariantCulture, TopFormat, RazorFileInfoCollection.AssemblyResourceName, RazorFileInfoCollection.SymbolsResourceName); foreach (var fileInfo in RazorFileInfoCollection.FileInfos) { var perFileEntry = GenerateFile(fileInfo); builder.Append(perFileEntry); } builder.Append(Bottom); var sourceCode = builder.ToString(); var syntaxTree = SyntaxTreeGenerator.Generate(sourceCode, "__AUTO__GeneratedViewsCollection.cs", CompilationSettings); return(syntaxTree); }
/// <inheritdoc /> public CompilationResult Compile([NotNull] IFileInfo fileInfo, [NotNull] string compilationContent) { // The path passed to SyntaxTreeGenerator.Generate is used by the compiler to generate symbols (pdb) that // map to the source file. If a file does not exist on a physical file system, PhysicalPath will be null. // This prevents files that exist in a non-physical file system from being debugged. var path = fileInfo.PhysicalPath ?? fileInfo.Name; var compilationSettings = _compilerOptionsProvider.GetCompilationSettings(_environment); var syntaxTree = SyntaxTreeGenerator.Generate(compilationContent, path, compilationSettings); var references = _applicationReferences.Value; var assemblyName = Path.GetRandomFileName(); var compilationOptions = compilationSettings.CompilationOptions .WithOutputKind(OutputKind.DynamicallyLinkedLibrary); var compilation = CSharpCompilation.Create(assemblyName, options: compilationOptions, syntaxTrees: new[] { syntaxTree }, references: references); using (var ms = new MemoryStream()) { using (var pdb = new MemoryStream()) { EmitResult result; if (_supportsPdbGeneration.Value) { result = compilation.Emit(ms, pdbStream: pdb); } else { result = compilation.Emit(ms); } if (!result.Success) { var formatter = new DiagnosticFormatter(); var messages = result.Diagnostics .Where(IsError) .Select(d => GetCompilationMessage(formatter, d)) .ToList(); return(CompilationResult.Failed(fileInfo, compilationContent, messages)); } Assembly assembly; ms.Seek(0, SeekOrigin.Begin); if (_supportsPdbGeneration.Value) { pdb.Seek(0, SeekOrigin.Begin); assembly = _loader.LoadStream(ms, pdb); } else { assembly = _loader.LoadStream(ms, assemblySymbols: null); } var type = assembly.GetExportedTypes() .First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal)); return(UncachedCompilationResult.Successful(type, compilationContent)); } } }