forked from cobbr/SharpShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.cs
More file actions
281 lines (249 loc) · 12.5 KB
/
Copy pathCompiler.cs
File metadata and controls
281 lines (249 loc) · 12.5 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Author: Ryan Cobb (@cobbr_io)
// Project: SharpShell (https://github.com/cobbr/SharpShell)
// License: BSD 3-Clause
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.IO.Compression;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Emit;
namespace SharpShell.API.Models
{
public class Compiler
{
public class CompilationRequest
{
public string Source { get; set; } = null;
public string SourceDirectory { get; set; } = null;
public string ResourceDirectory { get; set; } = null;
public string ReferenceDirectory { get; set; } = null;
public DotNetVersion TargetDotNetVersion { get; set; } = DotNetVersion.Net35;
public OutputKind OutputKind { get; set; } = OutputKind.DynamicallyLinkedLibrary;
public Platform Platform { get; set; } = Platform.AnyCpu;
public bool Optimize = true;
public string AssemblyName { get; set; } = null;
public List<Reference> References { get; set; } = new List<Reference>();
public List<EmbeddedResource> EmbeddedResources { get; set; } = new List<EmbeddedResource>();
}
public enum DotNetVersion
{
Net35,
Net40
}
public class EmbeddedResource
{
public string Name { get; set; }
public string File { get; set; }
public Platform Platform { get; set; } = Platform.AnyCpu;
public bool Enabled { get; set; } = false;
}
public class Reference
{
public string File { get; set; }
public DotNetVersion Framework { get; set; } = DotNetVersion.Net35;
public bool Enabled { get; set; } = false;
}
private class SourceSyntaxTree
{
public string FileName { get; set; } = "";
public SyntaxTree SyntaxTree { get; set; }
public List<INamedTypeSymbol> UsedTypes { get; set; } = new List<INamedTypeSymbol>();
}
public static byte[] Compile(CompilationRequest request)
{
// Gather SyntaxTrees for compilation
List<SourceSyntaxTree> sourceSyntaxTrees = Directory.GetFiles(request.SourceDirectory, "*.cs", SearchOption.AllDirectories)
.Select(F => new SourceSyntaxTree { FileName = F, SyntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(F), new CSharpParseOptions()) })
.ToList();
List<SyntaxTree> compilationTrees = sourceSyntaxTrees.Select(S => S.SyntaxTree).ToList();
SyntaxTree sourceTree = CSharpSyntaxTree.ParseText(request.Source, new CSharpParseOptions());
compilationTrees.Add(sourceTree);
// Use specified OutputKind and Platform
CSharpCompilationOptions options = new CSharpCompilationOptions(outputKind: request.OutputKind, optimizationLevel: OptimizationLevel.Release, platform: request.Platform);
// Compile to obtain SemanticModel
CSharpCompilation compilation = CSharpCompilation.Create(
request.AssemblyName == null ? Path.GetRandomFileName() : request.AssemblyName,
compilationTrees,
request.References.Where(R => R.Framework == request.TargetDotNetVersion).Where(R => R.Enabled).Select(R =>
{
string folder = (request.TargetDotNetVersion == DotNetVersion.Net35 ? request.ReferenceDirectory + "net35" + Path.DirectorySeparatorChar : request.ReferenceDirectory + "net40" + Path.DirectorySeparatorChar);
return MetadataReference.CreateFromFile(folder + R.File);
}).ToList(),
options
);
// Perform source code optimization, removing unused types
if (request.Optimize)
{
// Find all Types used by the generated compilation
List<INamedTypeSymbol> usedTypes = new List<INamedTypeSymbol>();
GetUsedTypesRecursively(compilation, sourceTree, ref usedTypes, ref sourceSyntaxTrees);
usedTypes = usedTypes.Distinct().ToList();
List<string> usedTypeNames = usedTypes.Select(T => GetFullyQualifiedTypeName(T)).ToList();
// SharpGenConsole.PrintInfoLine("usedTypes: " + String.Join(",", usedTypeNames));
// Filter SyntaxTrees to trees that define a used Type, otherwise the tree is not needed in this compilation
compilationTrees = sourceSyntaxTrees.Where(SST => SyntaxTreeDefinesUsedType(compilation, SST.SyntaxTree, usedTypeNames))
.Select(SST => SST.SyntaxTree)
.ToList();
// Removed unused Using statements from the additional entrypoint source
List<string> usedNamespaceNames = GetUsedTypes(compilation, sourceTree)
.Select(T => GetFullyQualifiedContainingNamespaceName(T)).Distinct().ToList();
// SharpGenConsole.PrintInfoLine("usedNamespaces: " + String.Join(",", usedNamespaceNames));
List<SyntaxNode> unusedUsingDirectives = sourceTree.GetRoot().DescendantNodes().Where(N =>
{
return N.Kind() == SyntaxKind.UsingDirective && !usedNamespaceNames.Contains(((UsingDirectiveSyntax)N).Name.ToFullString());
}).ToList();
sourceTree = sourceTree.GetRoot().RemoveNodes(unusedUsingDirectives, SyntaxRemoveOptions.KeepNoTrivia).SyntaxTree;
// Compile again, with unused SyntaxTrees and unused using statements removed
compilationTrees.Add(sourceTree);
compilation = CSharpCompilation.Create(
request.AssemblyName == null ? Path.GetRandomFileName() : request.AssemblyName,
compilationTrees,
request.References.Where(R => R.Framework == request.TargetDotNetVersion).Where(R => R.Enabled).Select(R =>
{
string folder = (request.TargetDotNetVersion == DotNetVersion.Net35 ? request.ReferenceDirectory + "net35" + Path.DirectorySeparatorChar : request.ReferenceDirectory + "net40" + Path.DirectorySeparatorChar);
return MetadataReference.CreateFromFile(folder + R.File);
}).ToList(),
options
);
}
// Emit compilation
EmitResult emitResult;
byte[] ILbytes = null;
using (var ms = new MemoryStream())
{
emitResult = compilation.Emit(
ms,
manifestResources: request.EmbeddedResources.Where(ER =>
{
return request.Platform == Platform.AnyCpu || ER.Platform == Platform.AnyCpu || ER.Platform == request.Platform;
}).Where(ER => ER.Enabled).Select(ER =>
{
return new ResourceDescription(ER.Name, () => File.OpenRead(request.ResourceDirectory + ER.File), true);
}).ToList()
);
if (emitResult.Success)
{
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
ILbytes = ms.ToArray();
}
else
{
StringBuilder sb = new StringBuilder();
foreach (Diagnostic d in emitResult.Diagnostics)
{
sb.AppendLine(d.ToString());
}
throw new CompilerException("CompilationErrors: " + Environment.NewLine + sb);
}
}
return ILbytes;
}
private static string GetFullyQualifiedContainingNamespaceName(INamespaceSymbol namespaceSymbol)
{
string name = namespaceSymbol.Name;
namespaceSymbol = namespaceSymbol.ContainingNamespace;
while (namespaceSymbol != null)
{
name = namespaceSymbol.Name + "." + name;
namespaceSymbol = namespaceSymbol.ContainingNamespace;
}
return name.Trim('.');
}
private static string GetFullyQualifiedContainingNamespaceName(INamedTypeSymbol namedTypeSymbol)
{
return GetFullyQualifiedContainingNamespaceName(namedTypeSymbol.ContainingNamespace);
}
private static string GetFullyQualifiedTypeName(INamedTypeSymbol namedTypeSymbol)
{
return GetFullyQualifiedContainingNamespaceName(namedTypeSymbol) + "." + namedTypeSymbol.Name;
}
private static bool SyntaxTreeDefinesUsedType(CSharpCompilation compilation, SyntaxTree tree, List<string> typeNames)
{
SemanticModel model = compilation.GetSemanticModel(tree);
return null != tree.GetRoot().DescendantNodes().FirstOrDefault(SN =>
{
if (SN.Kind() != SyntaxKind.ClassDeclaration)
{
return false;
}
INamedTypeSymbol symbol = model.GetDeclaredSymbol(((ClassDeclarationSyntax)SN));
if (symbol == null)
{
return false;
}
return typeNames.Contains(
GetFullyQualifiedTypeName(symbol)
);
});
}
private static List<INamedTypeSymbol> GetUsedTypes(CSharpCompilation compilation, SyntaxTree sourceTree)
{
return sourceTree.GetRoot().DescendantNodes().Select(N =>
{
ISymbol symbol = compilation.GetSemanticModel(sourceTree).GetSymbolInfo(N).Symbol;
if (symbol != null && symbol.ContainingType != null)
{
return symbol.ContainingType;
}
return null;
}).Distinct().Where(T => T != null).ToList();
}
private static List<INamedTypeSymbol> GetUsedTypesRecursively(CSharpCompilation compilation, SyntaxTree sourceTree, ref List<INamedTypeSymbol> currentUsedTypes, ref List<SourceSyntaxTree> sourceSyntaxTrees)
{
List<string> copyCurrentUsedTypes = currentUsedTypes.Select(CT => GetFullyQualifiedTypeName(CT)).ToList();
List<INamedTypeSymbol> usedTypes = GetUsedTypes(compilation, sourceTree)
.Where(T => !copyCurrentUsedTypes.Contains(GetFullyQualifiedTypeName(T)))
.ToList();
currentUsedTypes.AddRange(usedTypes);
List<SyntaxTree> searchTrees = new List<SyntaxTree>();
foreach (INamedTypeSymbol symbol in usedTypes)
{
SyntaxReference sr = symbol.DeclaringSyntaxReferences.FirstOrDefault();
if (sr != null)
{
SourceSyntaxTree sst = sourceSyntaxTrees.FirstOrDefault(SST => SST.SyntaxTree == sr.SyntaxTree);
if (sst != null) { sst.UsedTypes.Add(symbol); }
string fullyQualifiedTypeName = GetFullyQualifiedTypeName(symbol);
searchTrees.Add(sr.SyntaxTree);
}
}
searchTrees = searchTrees.Distinct().ToList();
foreach (SyntaxTree tree in searchTrees)
{
List<INamedTypeSymbol> newTypes = GetUsedTypesRecursively(compilation, tree, ref currentUsedTypes, ref sourceSyntaxTrees);
currentUsedTypes.AddRange(newTypes);
}
return currentUsedTypes;
}
public static byte[] Compress(byte[] bytes)
{
byte[] compressedILBytes;
using (MemoryStream memoryStream = new MemoryStream())
{
using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress))
{
deflateStream.Write(bytes, 0, bytes.Length);
}
compressedILBytes = memoryStream.ToArray();
}
return compressedILBytes;
}
}
public class CompilerException : Exception
{
public CompilerException()
{
}
public CompilerException(string message) : base(message)
{
}
public CompilerException(string message, Exception inner) : base(message, inner)
{
}
}
}