public void Compile_RunsCallback() { var content = "public class MyTestType {}"; var applicationEnvironment = PlatformServices.Default.Application; var libraryExporter = CompilationServices.Default.LibraryExporter; RoslynCompilationContext usedCompilation = null; var mvcRazorHost = new Mock <IMvcRazorHost>(); mvcRazorHost.SetupGet(m => m.MainClassNamePrefix) .Returns(string.Empty); var compilationService = new RoslynCompilationService( applicationEnvironment, libraryExporter, mvcRazorHost.Object, GetOptions(callback: c => usedCompilation = c), GetFileProviderAccessor(), NullLoggerFactory.Instance); var relativeFileInfo = new RelativeFileInfo( new TestFileInfo { PhysicalPath = "SomePath" }, "some-relative-path"); // Act var result = compilationService.Compile(relativeFileInfo, content); Assert.NotNull(usedCompilation); Assert.NotNull(usedCompilation.Compilation); Assert.Equal(1, usedCompilation.Compilation.SyntaxTrees.Length); }
/// <inheritdoc /> public CompilationResult Compile(RelativeFileInfo fileInfo, string compilationContent) { if (fileInfo == null) { throw new ArgumentNullException(nameof(fileInfo)); } if (compilationContent == null) { throw new ArgumentNullException(nameof(compilationContent)); } _logger.GeneratedCodeToAssemblyCompilationStart(fileInfo.RelativePath); var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0; var assemblyName = Path.GetRandomFileName(); var syntaxTree = SyntaxTreeGenerator.Generate( compilationContent, assemblyName, _parseOptions); var references = _applicationReferences.Value; var compilation = CSharpCompilation.Create( assemblyName, options: _compilationOptions, syntaxTrees: new[] { syntaxTree }, references: references); compilation = Rewrite(compilation); var compilationContext = new RoslynCompilationContext(compilation); _compilationCallback(compilationContext); compilation = compilationContext.Compilation; 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) { return GetCompilationFailedResult( fileInfo.RelativePath, compilationContent, assemblyName, result.Diagnostics); } Assembly assembly; ms.Seek(0, SeekOrigin.Begin); if (_supportsPdbGeneration.Value) { pdb.Seek(0, SeekOrigin.Begin); assembly = LoadStream(ms, pdb); } else { assembly = LoadStream(ms, assemblySymbols: null); } var type = assembly.GetExportedTypes() .First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal)); _logger.GeneratedCodeToAssemblyCompilationEnd(fileInfo.RelativePath, startTimestamp); return new CompilationResult(type); } } }
/// <inheritdoc /> public CompilationResult Compile(RelativeFileInfo fileInfo, string compilationContent) { if (fileInfo == null) { throw new ArgumentNullException(nameof(fileInfo)); } if (compilationContent == null) { throw new ArgumentNullException(nameof(compilationContent)); } _logger.GeneratedCodeToAssemblyCompilationStart(fileInfo.RelativePath); var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0; var assemblyName = Path.GetRandomFileName(); var syntaxTree = SyntaxTreeGenerator.Generate( compilationContent, assemblyName, _parseOptions); var references = _applicationReferences.Value; var compilation = CSharpCompilation.Create( assemblyName, options: _compilationOptions, syntaxTrees: new[] { syntaxTree }, references: references); compilation = Rewrite(compilation); var compilationContext = new RoslynCompilationContext(compilation); _compilationCallback(compilationContext); compilation = compilationContext.Compilation; 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) { return(GetCompilationFailedResult( fileInfo.RelativePath, compilationContent, assemblyName, result.Diagnostics)); } Assembly assembly; ms.Seek(0, SeekOrigin.Begin); if (_supportsPdbGeneration.Value) { pdb.Seek(0, SeekOrigin.Begin); assembly = LoadStream(ms, pdb); } else { assembly = LoadStream(ms, assemblySymbols: null); } var type = assembly.GetExportedTypes() .First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal)); _logger.GeneratedCodeToAssemblyCompilationEnd(fileInfo.RelativePath, startTimestamp); return(new CompilationResult(type)); } } }