-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuildService.cs
More file actions
207 lines (153 loc) · 7.67 KB
/
Copy pathBuildService.cs
File metadata and controls
207 lines (153 loc) · 7.67 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
using Express.Net.CodeAnalysis;
using Express.Net.CodeAnalysis.Diagnostics;
using Express.Net.Emit;
using Express.Net.Emit.Bootstrapping;
using Express.Net.Models.NuGet;
using Express.Net.Packages;
using System;
using System.IO;
using System.Linq;
using GeneratedSyntaxTrees = System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.SyntaxTree>;
using CSharpSyntaxTree = Microsoft.CodeAnalysis.SyntaxTree;
using CSharp = Microsoft.CodeAnalysis.CSharp;
using System.Text;
namespace Express.Net.Build.Services
{
internal class BuildService
{
public static EmitResult BuildProject(string? projectPath, string? outputPath = null, string? configuration = null, Action<string>? logger = null, bool dumpGeneratedFiles = false)
{
var projectFolder = Path.GetFullPath(string.IsNullOrEmpty(projectPath) ?
Directory.GetCurrentDirectory() :
projectPath.TrimEnd('\\', '/').Trim());
var buildConfiguration = string.IsNullOrEmpty(configuration) ? "Debug" : configuration;
var executableFolder = Path.GetFullPath(string.IsNullOrEmpty(outputPath) ?
Path.Combine(projectFolder, "bin", buildConfiguration) :
outputPath);
var projectFile = SourceFileDiscovery.GetProjectFileInDirectory(projectFolder);
var projectName = Path.GetFileNameWithoutExtension(projectFile);
if (string.IsNullOrEmpty(projectFile))
{
throw new Exception("No project file found");
}
var project = ProjectFileHandler.ReadProjectFile(projectFile);
if (project is null)
{
throw new Exception("Unable to read project file");
}
Directory.CreateDirectory(executableFolder);
var compilation = new ExpressNetCompilation(projectName, projectFolder, executableFolder, buildConfiguration)
.SetTargetFrameworks(TargetFrameworks.NetCore10, TargetFrameworks.AspNetCore10, TargetFrameworks.ExpressNet)
.SetBootstrapper(new BasicBootstrapper(projectName, project.GenerateSwaggerDoc, project.AddSwaggerUI));
var packageAssemblies = Enumerable.Empty<PackageAssembly>();
if (project.PackageReferences != null && project.PackageReferences.Length > 0)
{
logger?.Invoke($"Restore NuGet Packages for {projectName}");
var nugetClient = new NuGetClient(project, buildConfiguration, projectFolder);
packageAssemblies = nugetClient
.RestoreProjectDependenciesAsync()
.GetAwaiter()
.GetResult();
compilation = compilation.SetPackageAssemblies(packageAssemblies.ToArray());
}
logger?.Invoke($"Build starting for {projectName}");
var sourceFiles = SourceFileDiscovery.GetSourceFilesInDirectory(projectFolder);
if (sourceFiles.Length == 0)
{
throw new Exception("Unable locate any source files");
}
compilation = compilation.SetSyntaxTrees(ParseSourceFiles(sourceFiles, logger));
var csharpSourceFiles = SourceFileDiscovery.GetCSharpSourceFilesInDirectory(projectFolder);
if (csharpSourceFiles.Length > 0)
{
compilation = compilation.SetCSharpSyntaxTrees(ParseCSharpSourceFiles(csharpSourceFiles, logger));
}
logger?.Invoke($"Building Runtime Config");
RuntimeConfigBuilder.BuildRuntimeConfig(projectName, executableFolder, TargetFrameworks.ExpressNet);
logger?.Invoke($"Emiting IL");
var result = compilation.Emit();
if (result.Success)
{
logger?.Invoke("Emiting IL complete");
logger?.Invoke("Copying express framework assemblies");
ExpressDependencies.CopyFrameworkAssemblies(project, executableFolder);
if (packageAssemblies.Any())
{
logger?.Invoke("Copying package assemblies");
ExpressDependencies.CopyPackageAssemblies(packageAssemblies, projectFolder, executableFolder);
}
}
if (dumpGeneratedFiles)
{
logger?.Invoke("Saving generated C# files");
SaveGeneratedCSharpFiles(outputPath, projectFolder, result.GeneratedSyntaxTrees);
}
return result;
}
private static SyntaxTree[] ParseSourceFiles(string[] sourceFiles, Action<string>? logger)
{
var syntaxTrees = new SyntaxTree[sourceFiles.Length];
for (var i = 0; i < sourceFiles.Length; i++)
{
var sourceFile = sourceFiles[i];
var sourceFileName = Path.GetFileName(sourceFile);
logger?.Invoke($"Parsing file {sourceFileName}");
var syntaxTree = SyntaxTree.FromFile(sourceFile);
var hasErrors = false;
foreach (var diagnostic in syntaxTree.Diagnostics)
{
hasErrors = diagnostic.DiagnosticType == DiagnosticType.Error;
logger?.Invoke($"{diagnostic.DiagnosticType}: {diagnostic.Message} @ {diagnostic.Location}");
}
if (hasErrors)
{
throw new Exception($"Syntax errors in {sourceFileName}");
}
syntaxTrees[i] = syntaxTree;
}
return syntaxTrees;
}
private static CSharpSyntaxTree[] ParseCSharpSourceFiles(string[] sourceFiles, Action<string>? logger)
{
var syntaxTrees = new CSharpSyntaxTree[sourceFiles.Length];
for (var i = 0; i < sourceFiles.Length; i++)
{
var sourceFile = sourceFiles[i];
var sourceFileName = Path.GetFileName(sourceFile);
logger?.Invoke($"Parsing file {sourceFileName}");
var sourceText = File.ReadAllText(sourceFile);
var syntaxTree = CSharp.CSharpSyntaxTree.ParseText(sourceText, path: sourceFile, encoding: Encoding.UTF8);
var hasErrors = false;
foreach (var diagnostic in syntaxTree.GetDiagnostics())
{
hasErrors = diagnostic.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error;
logger?.Invoke($"{diagnostic.Severity}: {diagnostic.GetMessage()} @ {diagnostic.Location}");
}
if (hasErrors)
{
throw new Exception($"Syntax errors in {sourceFileName}");
}
syntaxTrees[i] = syntaxTree;
}
return syntaxTrees;
}
private static void SaveGeneratedCSharpFiles(string? outputPath, string projectFolder, GeneratedSyntaxTrees? generatedSyntaxTrees)
{
if (!generatedSyntaxTrees.HasValue)
{
return;
}
var generatedSourceFolder = Path.GetFullPath(string.IsNullOrEmpty(outputPath) ?
Path.Combine(projectFolder, "obj", "Generated") :
Path.Combine(outputPath, "Generated"));
Directory.CreateDirectory(generatedSourceFolder);
foreach (var generatedSyntaxTree in generatedSyntaxTrees)
{
var fileName = Path.GetFileNameWithoutExtension(generatedSyntaxTree.FilePath);
var filePath = Path.Combine(generatedSourceFolder, $"{fileName}.cs");
var text = generatedSyntaxTree.ToString();
File.WriteAllText(filePath, text);
}
}
}
}