-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CompilerHost.cs
129 lines (102 loc) · 5.03 KB
/
CompilerHost.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.CodeAnalysis;
namespace Microsoft.NET.Sdk.Razor.Tool
{
internal abstract class CompilerHost
{
public static CompilerHost Create()
{
return new DefaultCompilerHost();
}
public abstract ServerResponse Execute(ServerRequest request, CancellationToken cancellationToken);
private class DefaultCompilerHost : CompilerHost
{
public DefaultCompilerHost()
{
// The loader needs to live for the lifetime of the server.
//
// This means that if a request tries to use a set of binaries that are inconsistent with what
// the server already has, then it will be rejected to try again on the client.
//
// We also check each set of extensions for missing depenencies individually, so that we can
// consistently reject a request that doesn't specify everything it needs. Otherwise the request
// could succeed sometimes if it relies on transient state.
Loader = new DefaultExtensionAssemblyLoader(Path.Combine(Path.GetTempPath(), "Razor-Server"));
AssemblyReferenceProvider = (path, properties) => new CachingMetadataReference(path, properties);
}
public Func<string, MetadataReferenceProperties, PortableExecutableReference> AssemblyReferenceProvider { get; }
public ExtensionAssemblyLoader Loader { get; }
public override ServerResponse Execute(ServerRequest request, CancellationToken cancellationToken)
{
if (!TryParseArguments(request, out var parsed))
{
return new RejectedServerResponse();
}
var exitCode = 0;
var commandArgs = parsed.args.ToArray();
var outputWriter = new StringWriter();
var errorWriter = new StringWriter();
var checker = new DefaultExtensionDependencyChecker(Loader, outputWriter, errorWriter);
var app = new Application(cancellationToken, Loader, checker, AssemblyReferenceProvider, outputWriter, errorWriter);
exitCode = app.Execute(commandArgs);
var output = outputWriter.ToString();
var error = errorWriter.ToString();
outputWriter.Dispose();
errorWriter.Dispose();
// This will no-op if server logging is not enabled.
ServerLogger.Log(output);
ServerLogger.Log(error);
return new CompletedServerResponse(exitCode, utf8output: false, output, error);
}
private bool TryParseArguments(ServerRequest request, out (string workingDirectory, string tempDirectory, string[] args) parsed)
{
string workingDirectory = null;
string tempDirectory = null;
var args = new List<string>(request.Arguments.Count);
for (var i = 0; i < request.Arguments.Count; i++)
{
var argument = request.Arguments[i];
if (argument.Id == RequestArgument.ArgumentId.CurrentDirectory)
{
workingDirectory = argument.Value;
}
else if (argument.Id == RequestArgument.ArgumentId.TempDirectory)
{
tempDirectory = argument.Value;
}
else if (argument.Id == RequestArgument.ArgumentId.CommandLineArgument)
{
args.Add(argument.Value);
}
}
ServerLogger.Log($"WorkingDirectory = '{workingDirectory}'");
ServerLogger.Log($"TempDirectory = '{tempDirectory}'");
for (var i = 0; i < args.Count; i++)
{
ServerLogger.Log($"Argument[{i}] = '{request.Arguments[i]}'");
}
if (string.IsNullOrEmpty(workingDirectory))
{
ServerLogger.Log($"Rejecting build due to missing working directory");
parsed = default;
return false;
}
if (string.IsNullOrEmpty(tempDirectory))
{
ServerLogger.Log($"Rejecting build due to missing temp directory");
parsed = default;
return false;
}
if (string.IsNullOrEmpty(tempDirectory))
{
ServerLogger.Log($"Rejecting build due to missing temp directory");
parsed = default;
return false;
}
parsed = (workingDirectory, tempDirectory, args.ToArray());
return true;
}
}
}
}