/// <summary> /// Extracts the events and delegates from the specified platform. /// </summary> /// <param name="writer">The writer where to output to.</param> /// <param name="input">The input into the processor.</param> /// <param name="framework">The framework we are adapting to.</param> /// <returns>A task to monitor the progress.</returns> public static async Task ExtractEventsFromAssemblies(TextWriter writer, InputAssembliesGroup input, NuGetFramework framework) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } using (var compilation = new EventBuilderCompiler(input, framework)) { var compilationOutputSyntax = CompilationUnit() .WithMembers(List <MemberDeclarationSyntax>(_resolvers.SelectMany(x => x.Create(compilation)))) .WithUsings(List(new[] { UsingDirective(IdentifierName("global::System")), UsingDirective(IdentifierName("global::System.Reactive")), UsingDirective(IdentifierName("global::System.Reactive.Linq")), UsingDirective(IdentifierName("global::System.Reactive.Subjects")), UsingDirective(IdentifierName("global::Pharmacist.Common")) })); await writer.WriteAsync(Environment.NewLine).ConfigureAwait(false); await writer.WriteAsync(compilationOutputSyntax.NormalizeWhitespace(elasticTrivia : true).ToString()).ConfigureAwait(false); await writer.FlushAsync().ConfigureAwait(false); } }
public void RunGenerator(EventBuilderCompiler compiler, out ImmutableArray <Diagnostic> compilationDiagnostics, out ImmutableArray <Diagnostic> generatorDiagnostics, params string[] sources) { var compilation = CreateCompilation(compiler, sources); var newCompilation = RunGenerators(compilation, out generatorDiagnostics, new EventGenerator()); compilationDiagnostics = newCompilation.GetDiagnostics(); ShouldHaveNoCompilerDiagnosticsWarningOrAbove(_testOutputHelper, newCompilation, compilationDiagnostics); ShouldHaveNoCompilerDiagnosticsWarningOrAbove(_testOutputHelper, compilation, generatorDiagnostics); }
private static Microsoft.CodeAnalysis.Compilation CreateCompilation(EventBuilderCompiler compiler, params string[] sources) { var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location); if (assemblyPath == null || string.IsNullOrWhiteSpace(assemblyPath)) { throw new InvalidOperationException("Could not find a valid assembly path."); } var assemblies = new HashSet <MetadataReference>(); assemblies.UnionWith(compiler.Modules.Where(x => x.PEFile is not null).Select(x => MetadataReference.CreateFromFile(x.PEFile !.FileName))); assemblies.UnionWith(compiler.ReferencedModules.Where(x => x.PEFile is not null).Select(x => MetadataReference.CreateFromFile(x.PEFile !.FileName))); assemblies.UnionWith(compiler.NeededModules.Where(x => x.PEFile is not null).Select(x => MetadataReference.CreateFromFile(x.PEFile !.FileName))); return(CSharpCompilation.Create( assemblyName: "compilation" + Guid.NewGuid(), syntaxTrees: sources.Select(x => CSharpSyntaxTree.ParseText(x, new CSharpParseOptions(LanguageVersion.Latest))), references: assemblies, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, deterministic: true))); }