This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathLoaderTestResources.cs
146 lines (120 loc) · 3.65 KB
/
LoaderTestResources.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
namespace Microsoft.AspNetCore.Razor.Tools
{
internal static class LoaderTestResources
{
static LoaderTestResources()
{
Delta = CreateAssemblyBlob("Delta", Array.Empty<AssemblyBlob>(), @"
using System.Text;
namespace Delta
{
public class D
{
public void Write(StringBuilder sb, string s)
{
sb.AppendLine(""Delta: "" + s);
}
}
}
");
Gamma = CreateAssemblyBlob("Gamma", new[] { Delta, }, @"
using System.Text;
using Delta;
namespace Gamma
{
public class G
{
public void Write(StringBuilder sb, string s)
{
D d = new D();
d.Write(sb, ""Gamma: "" + s);
}
}
}
");
Alpha = CreateAssemblyBlob("Alpha", new[] { Gamma, }, @"
using System.Text;
using Gamma;
namespace Alpha
{
public class A
{
public void Write(StringBuilder sb, string s)
{
G g = new G();
g.Write(sb, ""Alpha: "" + s);
}
}
}
");
Beta = CreateAssemblyBlob("Beta", new[] { Gamma, }, @"
using System.Text;
using Gamma;
namespace Beta
{
public class B
{
public void Write(StringBuilder sb, string s)
{
G g = new G();
g.Write(sb, ""Beta: "" + s);
}
}
}
");
}
public static AssemblyBlob Alpha { get; }
public static AssemblyBlob Beta { get; }
public static AssemblyBlob Delta { get; }
public static AssemblyBlob Gamma { get; }
private static AssemblyBlob CreateAssemblyBlob(string assemblyName, AssemblyBlob[] references, string text)
{
var defaultReferences = new[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
};
var compilation = CSharpCompilation.Create(
assemblyName,
new[] { CSharpSyntaxTree.ParseText(text) },
references.Select(r => r.ToMetadataReference()).Concat(defaultReferences),
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var assemblyStream = new MemoryStream())
using (var symbolStream = new MemoryStream())
{
var result = compilation.Emit(assemblyStream, symbolStream);
Assert.Empty(result.Diagnostics);
return new AssemblyBlob(assemblyName, assemblyStream.GetBuffer(), symbolStream.GetBuffer());
}
}
public class AssemblyBlob
{
public AssemblyBlob(string assemblyName, byte[] assemblyBytes, byte[] symbolBytes)
{
AssemblyName = assemblyName;
AssemblyBytes = assemblyBytes;
SymbolBytes = symbolBytes;
}
public string AssemblyName { get; }
public byte[] AssemblyBytes { get; }
public byte[] SymbolBytes { get; }
public MetadataReference ToMetadataReference()
{
return MetadataReference.CreateFromImage(AssemblyBytes);
}
internal string WriteToFile(string directoryPath, string fileName)
{
var filePath = Path.Combine(directoryPath, fileName);
File.WriteAllBytes(filePath, AssemblyBytes);
return filePath;
}
}
}
}