|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.Tracing; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.CodeAnalysis.CSharp; |
| 9 | +using Microsoft.CodeAnalysis.Emit; |
| 10 | +using Microsoft.CodeAnalysis.Host; |
| 11 | +using Thor.Analyzer; |
| 12 | +using Thor.Generator.Properties; |
| 13 | +using Thor.Generator.Templates; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace Thor.Generator |
| 17 | +{ |
| 18 | + public class CompilationTests |
| 19 | + { |
| 20 | + [Fact] |
| 21 | + public void AnalyzeCsharpComplexEventSource() |
| 22 | + { |
| 23 | + // arrange |
| 24 | + TemplateStorage templateStorage = new TemplateStorage(); |
| 25 | + Template template = templateStorage.GetCustomTemplate("Defaults\\CSharpWithComplexLight"); |
| 26 | + |
| 27 | + EventSourceDefinitionVisitor eventSourceDefinitionVisitor = new EventSourceDefinitionVisitor(); |
| 28 | + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(Resources.EventSourceWithComplexTypeThatBuilds); |
| 29 | + eventSourceDefinitionVisitor.Visit(syntaxTree.GetRoot()); |
| 30 | + |
| 31 | + CodeEventAnalyzer analyzer = new CodeEventAnalyzer(new[] |
| 32 | + { |
| 33 | + MetadataReference.CreateFromFile(typeof(Core.Application).GetTypeInfo().Assembly.Location), |
| 34 | + MetadataReference.CreateFromFile(typeof(Core.Abstractions.EventSourceBase).GetTypeInfo().Assembly.Location), |
| 35 | + }); |
| 36 | + |
| 37 | + // act |
| 38 | + EventSourceTemplateEngine templateEngine = new EventSourceTemplateEngine(template); |
| 39 | + string eventSourceCode = templateEngine.Generate(eventSourceDefinitionVisitor.EventSource); |
| 40 | + Report report = analyzer.CompileAndAnalyze(eventSourceCode, "EventSources.FooEventSource"); |
| 41 | + |
| 42 | + // assert |
| 43 | + Assert.NotNull(report); |
| 44 | + Assert.False(report.HasErrors); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void AnalyzeCsharpSimpleEventSource() |
| 49 | + { |
| 50 | + // arrange |
| 51 | + TemplateStorage templateStorage = new TemplateStorage(); |
| 52 | + Template template = templateStorage.GetCustomTemplate("Defaults\\CSharpLight"); |
| 53 | + |
| 54 | + EventSourceDefinitionVisitor eventSourceDefinitionVisitor = new EventSourceDefinitionVisitor(); |
| 55 | + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(Resources.EventSourceThatBuilds); |
| 56 | + eventSourceDefinitionVisitor.Visit(syntaxTree.GetRoot()); |
| 57 | + |
| 58 | + CodeEventAnalyzer analyzer = new CodeEventAnalyzer(); |
| 59 | + |
| 60 | + // act |
| 61 | + EventSourceTemplateEngine templateEngine = new EventSourceTemplateEngine(template); |
| 62 | + string eventSourceCode = templateEngine.Generate(eventSourceDefinitionVisitor.EventSource); |
| 63 | + Report report = analyzer.CompileAndAnalyze(eventSourceCode, "EventSources.BarEventSource"); |
| 64 | + |
| 65 | + // assert |
| 66 | + Assert.NotNull(report); |
| 67 | + Assert.False(report.HasErrors); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public class CodeEventAnalyzer |
| 72 | + { |
| 73 | + private IReadOnlyCollection<MetadataReference> _references; |
| 74 | + |
| 75 | + |
| 76 | + public CodeEventAnalyzer() |
| 77 | + { |
| 78 | + _references = GetBaseAssemblies(); |
| 79 | + } |
| 80 | + |
| 81 | + public CodeEventAnalyzer(ICollection<PortableExecutableReference> references) |
| 82 | + { |
| 83 | + _references = GetBaseAssemblies().Union(references).ToList(); |
| 84 | + } |
| 85 | + |
| 86 | + public Report CompileAndAnalyze(string code, string className) |
| 87 | + { |
| 88 | + Assembly assembly = CreateAssemblyDefinition(code); |
| 89 | + |
| 90 | + Type type = assembly.GetType(className); |
| 91 | + EventSource eventSource = (EventSource) type.GetMethod("CreateInstance").Invoke(null, null); |
| 92 | + |
| 93 | + return Analyze(eventSource); |
| 94 | + } |
| 95 | + |
| 96 | + private Report Analyze(EventSource eventSource) |
| 97 | + { |
| 98 | + EventSourceAnalyzer analyzer = new EventSourceAnalyzer(); |
| 99 | + return analyzer.Inspect(eventSource); |
| 100 | + } |
| 101 | + |
| 102 | + private Assembly CreateAssemblyDefinition(string code) |
| 103 | + { |
| 104 | + CSharpLanguage sourceLanguage = new CSharpLanguage(); |
| 105 | + SyntaxTree syntaxTree = sourceLanguage.ParseText(code, SourceCodeKind.Regular); |
| 106 | + |
| 107 | + Compilation compilation = sourceLanguage |
| 108 | + .CreateLibraryCompilation(assemblyName: "InMemoryAssembly", enableOptimisations: false) |
| 109 | + .AddReferences(_references) |
| 110 | + .AddSyntaxTrees(syntaxTree); |
| 111 | + |
| 112 | + MemoryStream stream = new MemoryStream(); |
| 113 | + EmitResult emitResult = compilation.Emit(stream); |
| 114 | + |
| 115 | + if (emitResult.Success) |
| 116 | + { |
| 117 | + stream.Seek(0, SeekOrigin.Begin); |
| 118 | + |
| 119 | + using (stream) |
| 120 | + { |
| 121 | + byte[] data = new byte[stream.Length]; |
| 122 | + stream.Read(data, 0, data.Length); |
| 123 | + return Assembly.Load(data); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + private List<PortableExecutableReference> GetBaseAssemblies() |
| 131 | + { |
| 132 | + string[] trustedAssembliesPaths = ((string)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES")).Split(Path.PathSeparator); |
| 133 | + List<PortableExecutableReference> references = trustedAssembliesPaths |
| 134 | + .Select(p => MetadataReference.CreateFromFile(p)) |
| 135 | + .ToList(); |
| 136 | + |
| 137 | + references.AddRange(new[] |
| 138 | + { |
| 139 | + MetadataReference.CreateFromFile(typeof(EventSource).GetTypeInfo().Assembly.Location), |
| 140 | + }); |
| 141 | + |
| 142 | + return references; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public class CSharpLanguage : ILanguageService |
| 147 | + { |
| 148 | + private readonly IReadOnlyCollection<MetadataReference> _references = new[] |
| 149 | + { |
| 150 | + MetadataReference.CreateFromFile(typeof(Binder).GetTypeInfo().Assembly.Location), |
| 151 | + MetadataReference.CreateFromFile(typeof(ValueTuple<>).GetTypeInfo().Assembly.Location) |
| 152 | + }; |
| 153 | + |
| 154 | + private readonly LanguageVersion _maxLanguageVersion = Enum |
| 155 | + .GetValues(typeof(LanguageVersion)) |
| 156 | + .Cast<LanguageVersion>() |
| 157 | + .Max(); |
| 158 | + |
| 159 | + public SyntaxTree ParseText(string sourceCode, SourceCodeKind kind) |
| 160 | + { |
| 161 | + CSharpParseOptions options = new CSharpParseOptions(kind: kind, languageVersion: _maxLanguageVersion); |
| 162 | + |
| 163 | + // Return a syntax tree of our source code |
| 164 | + return CSharpSyntaxTree.ParseText(sourceCode, options); |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + public Compilation CreateLibraryCompilation(string assemblyName, bool enableOptimisations) |
| 169 | + { |
| 170 | + CSharpCompilationOptions options = new CSharpCompilationOptions( |
| 171 | + OutputKind.DynamicallyLinkedLibrary, |
| 172 | + optimizationLevel: enableOptimisations ? OptimizationLevel.Release : OptimizationLevel.Debug, |
| 173 | + allowUnsafe: true); |
| 174 | + |
| 175 | + return CSharpCompilation.Create(assemblyName, options: options, references: _references); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments