|
1 | | -// --------------------------------------------------------------------------------------------------------------------- |
2 | | -// Imports |
3 | | -// --------------------------------------------------------------------------------------------------------------------- |
4 | | -using JetBrains.Annotations; |
5 | | -using Microsoft.CodeAnalysis; |
6 | | -using Microsoft.CodeAnalysis.CSharp; |
7 | | -using Microsoft.CodeAnalysis.Diagnostics; |
8 | | -using System.Collections.Concurrent; |
9 | | -using System.Collections.Immutable; |
10 | | -using System.Reflection; |
11 | | -using System.Text; |
12 | | - |
13 | | -namespace CodeOfChaos.Testing.TUnit; |
14 | | -// --------------------------------------------------------------------------------------------------------------------- |
15 | | -// Code |
16 | | -// --------------------------------------------------------------------------------------------------------------------- |
17 | | -public class RoslynCompilationRunner(string? name = null, LanguageVersion languageVersion = LanguageVersion.Latest) : IDisposable { |
18 | | - |
19 | | - private readonly ConcurrentBag<MetadataReference> _assemblyReferences = [ |
20 | | - // Some default assemblies |
21 | | - GetMetadataReference(typeof(string).Assembly), |
22 | | - GetMetadataReference(typeof(object).Assembly) |
23 | | - ]; |
24 | | - private readonly ConcurrentBag<(string Name, StringBuilder Source)> _documents = new(); |
25 | | - |
26 | | - private Project? _project; |
27 | | - private string Name { get; } = name ?? $"TestProject_{Guid.NewGuid()}"; |
28 | | - private Lazy<AdhocWorkspace> Workspace { get; } = new(() => new AdhocWorkspace()); |
29 | | - private Project Project { |
30 | | - get => _project ??= Workspace.Value.CurrentSolution |
31 | | - .AddProject(Name, $"{Name}.dll", "C#") |
32 | | - .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu)) |
33 | | - .WithParseOptions(new CSharpParseOptions(languageVersion)); |
34 | | - set => _project = value; |
35 | | - } |
36 | | - |
37 | | - public void Dispose() { |
38 | | - if (Workspace.IsValueCreated) { |
39 | | - Workspace.Value.Dispose(); |
40 | | - } |
41 | | - |
42 | | - GC.SuppressFinalize(this); |
43 | | - } |
44 | | - |
45 | | - // ----------------------------------------------------------------------------------------------------------------- |
46 | | - // Methods |
47 | | - // ----------------------------------------------------------------------------------------------------------------- |
48 | | - #region Add Documents |
49 | | - public RoslynCompilationRunner AddDocument(string name, [LanguageInjection("CSharp")] string source) { |
50 | | - _documents.Add((name, new StringBuilder(source))); |
51 | | - return this; |
52 | | - } |
53 | | - #endregion |
54 | | - |
55 | | - public async ValueTask<RoslynGeneratorRunner> GetGeneratorRunnerAsync() { |
56 | | - Compilation compilation = await GetCompilationAsync(); |
57 | | - return new RoslynGeneratorRunner(languageVersion) { |
58 | | - Compilation = compilation |
59 | | - }; |
60 | | - } |
61 | | - |
62 | | - public async ValueTask<Compilation> GetCompilationAsync() { |
63 | | - // Resolve all assemblies |
64 | | - Project = _assemblyReferences.Aggregate(Project, func: (current, reference) => current.AddMetadataReference(reference)); |
65 | | - |
66 | | - // Add All documents |
67 | | - foreach ((string name, StringBuilder source) in _documents) { |
68 | | - Project = Project.AddDocument(name, source.ToString()).Project; |
69 | | - } |
70 | | - |
71 | | - // Compile the project |
72 | | - Compilation? compilation = await Project.GetCompilationAsync(); |
73 | | - |
74 | | - await Assert.That(compilation).IsNotNull(); |
75 | | - |
76 | | - return compilation!; |
77 | | - } |
78 | | - |
79 | | - public async ValueTask<CompilationWithAnalyzers> GetCompilationWithAnalyzersAsync() { |
80 | | - // Resolve all assemblies |
81 | | - Project = _assemblyReferences.Aggregate(Project, func: (current, reference) => current.AddMetadataReference(reference)); |
82 | | - |
83 | | - // Add All documents |
84 | | - foreach ((string name, StringBuilder source) in _documents) { |
85 | | - Project = Project.AddDocument(name, source.ToString()).Project; |
86 | | - } |
87 | | - |
88 | | - // Add Analyzers to the compilation phase |
89 | | - ImmutableArray<DiagnosticAnalyzer> analyzers = Project.AnalyzerReferences |
90 | | - .OfType<AnalyzerImageReference>()// Ensures we only get valid analyzers |
91 | | - .SelectMany(reference => reference.GetAnalyzers(LanguageNames.CSharp)) |
92 | | - .ToImmutableArray(); |
93 | | - |
94 | | - // Compile the project |
95 | | - Compilation? compilation = await Project.GetCompilationAsync(); |
96 | | - await Assert.That(compilation).IsNotNull(); |
97 | | - |
98 | | - return compilation!.WithAnalyzers(analyzers); |
99 | | - } |
100 | | - |
101 | | - |
102 | | - public RoslynCompilationRunner AddDiagnosticAnalyzer<T>() where T : DiagnosticAnalyzer, new() { |
103 | | - IReadOnlyList<AnalyzerReference> currentAnalyzers = Project.AnalyzerReferences; |
104 | | - AnalyzerReference[] newAnalyzers = [new AnalyzerImageReference([new T()])]; |
105 | | - |
106 | | - Project = Project.WithAnalyzerReferences(currentAnalyzers.Concat(newAnalyzers)); |
107 | | - |
108 | | - return this; |
109 | | - } |
110 | | - #region Add Assembly references |
111 | | - public RoslynCompilationRunner AddReferences(params Span<Assembly> assemblies) { |
112 | | - foreach (Assembly assembly in assemblies) _assemblyReferences.Add(GetMetadataReference(assembly)); |
113 | | - return this; |
114 | | - } |
115 | | - |
116 | | - public RoslynCompilationRunner AddReferences(params Span<Type> types) { |
117 | | - foreach (Type type in types) _assemblyReferences.Add(GetMetadataReference(type)); |
118 | | - return this; |
119 | | - } |
120 | | - |
121 | | - private static PortableExecutableReference GetMetadataReference(Assembly assembly) => MetadataReference.CreateFromFile(assembly.Location); |
122 | | - private static PortableExecutableReference GetMetadataReference(Type type) => GetMetadataReference(type.Assembly); |
123 | | - #endregion |
124 | | -} |
| 1 | +// --------------------------------------------------------------------------------------------------------------------- |
| 2 | +// Imports |
| 3 | +// --------------------------------------------------------------------------------------------------------------------- |
| 4 | +using JetBrains.Annotations; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 8 | +using System.Collections.Concurrent; |
| 9 | +using System.Collections.Immutable; |
| 10 | +using System.Reflection; |
| 11 | +using System.Text; |
| 12 | + |
| 13 | +namespace CodeOfChaos.Testing; |
| 14 | +// --------------------------------------------------------------------------------------------------------------------- |
| 15 | +// Code |
| 16 | +// --------------------------------------------------------------------------------------------------------------------- |
| 17 | +public class RoslynCompilationRunner(string? name = null, LanguageVersion languageVersion = LanguageVersion.Latest) : IDisposable { |
| 18 | + |
| 19 | + private readonly ConcurrentBag<MetadataReference> _assemblyReferences = [ |
| 20 | + // Some default assemblies |
| 21 | + typeof(string).GetPortableExecutableReference(), |
| 22 | + typeof(object).GetPortableExecutableReference() |
| 23 | + ]; |
| 24 | + private readonly ConcurrentBag<(string Name, StringBuilder Source)> _documents = new(); |
| 25 | + |
| 26 | + private Project? _project; |
| 27 | + |
| 28 | + private string Name { get; } = name ?? $"TestProject_{Guid.NewGuid()}"; |
| 29 | + private Lazy<AdhocWorkspace> Workspace { get; } = new(() => new AdhocWorkspace()); |
| 30 | + private Project Project { |
| 31 | + get => _project ??= Workspace.Value.CurrentSolution |
| 32 | + .AddProject(Name, $"{Name}.dll", "C#") |
| 33 | + .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu)) |
| 34 | + .WithParseOptions(new CSharpParseOptions(languageVersion)); |
| 35 | + set => _project = value; |
| 36 | + } |
| 37 | + |
| 38 | + public void Dispose() { |
| 39 | + if (Workspace.IsValueCreated) { |
| 40 | + Workspace.Value.Dispose(); |
| 41 | + } |
| 42 | + |
| 43 | + GC.SuppressFinalize(this); |
| 44 | + } |
| 45 | + |
| 46 | + // ----------------------------------------------------------------------------------------------------------------- |
| 47 | + // Methods |
| 48 | + // ----------------------------------------------------------------------------------------------------------------- |
| 49 | + public RoslynCompilationRunner AddDocument(string name, [LanguageInjection("CSharp")] string source) { |
| 50 | + _documents.Add((name, new StringBuilder(source))); |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public async ValueTask<RoslynGeneratorRunner> GetGeneratorRunnerAsync() { |
| 55 | + Compilation compilation = await GetCompilationAsync(); |
| 56 | + return new RoslynGeneratorRunner(languageVersion) { |
| 57 | + Compilation = compilation |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + public async ValueTask<Compilation> GetCompilationAsync() { |
| 62 | + // Resolve all assemblies |
| 63 | + Project = _assemblyReferences.Aggregate(Project, func: (current, reference) => current.AddMetadataReference(reference)); |
| 64 | + |
| 65 | + // Add All documents |
| 66 | + foreach ((string name, StringBuilder source) in _documents) { |
| 67 | + Project = Project.AddDocument(name, source.ToString()).Project; |
| 68 | + } |
| 69 | + |
| 70 | + // Compile the project |
| 71 | + Compilation? compilation = await Project.GetCompilationAsync(); |
| 72 | + if (compilation is null) throw new InvalidOperationException("Compilation is null"); |
| 73 | + |
| 74 | + return compilation; |
| 75 | + } |
| 76 | + |
| 77 | + public async ValueTask<CompilationWithAnalyzers> GetCompilationWithAnalyzersAsync() { |
| 78 | + // Resolve all assemblies |
| 79 | + Project = _assemblyReferences.Aggregate(Project, func: (current, reference) => current.AddMetadataReference(reference)); |
| 80 | + |
| 81 | + // Add All documents |
| 82 | + foreach ((string name, StringBuilder source) in _documents) { |
| 83 | + Project = Project.AddDocument(name, source.ToString()).Project; |
| 84 | + } |
| 85 | + |
| 86 | + // Add Analyzers to the compilation phase |
| 87 | + ImmutableArray<DiagnosticAnalyzer> analyzers = Project.AnalyzerReferences |
| 88 | + .OfType<AnalyzerImageReference>()// Ensures we only get valid analyzers |
| 89 | + .SelectMany(reference => reference.GetAnalyzers(LanguageNames.CSharp)) |
| 90 | + .ToImmutableArray(); |
| 91 | + |
| 92 | + // Compile the project |
| 93 | + Compilation? compilation = await Project.GetCompilationAsync(); |
| 94 | + if (compilation is null) throw new InvalidOperationException("Compilation is null"); |
| 95 | + |
| 96 | + return compilation.WithAnalyzers(analyzers); |
| 97 | + } |
| 98 | + |
| 99 | + public RoslynCompilationRunner AddDiagnosticAnalyzer<T>() where T : DiagnosticAnalyzer, new() { |
| 100 | + IReadOnlyList<AnalyzerReference> currentAnalyzers = Project.AnalyzerReferences; |
| 101 | + AnalyzerReference[] newAnalyzers = [new AnalyzerImageReference([new T()])]; |
| 102 | + |
| 103 | + Project = Project.WithAnalyzerReferences(currentAnalyzers.Concat(newAnalyzers)); |
| 104 | + |
| 105 | + return this; |
| 106 | + } |
| 107 | + #region Add Assembly references |
| 108 | + public RoslynCompilationRunner AddReferences(params Span<Assembly> assemblies) { |
| 109 | + foreach (Assembly assembly in assemblies) _assemblyReferences.Add(assembly.GetPortableExecutableReference()); |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public RoslynCompilationRunner AddReferences(params Span<Type> types) { |
| 114 | + foreach (Type type in types) _assemblyReferences.Add(type.GetPortableExecutableReference()); |
| 115 | + return this; |
| 116 | + } |
| 117 | + #endregion |
| 118 | +} |
0 commit comments