-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDeltaProject.cs
176 lines (145 loc) · 7.69 KB
/
DeltaProject.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.DotNet.HotReload.Utils.Generator
{
/// Drives the creation of deltas from textual changes.
public class DeltaProject
{
readonly EnC.ChangeMaker _changeMaker;
readonly Solution _solution;
readonly EmitBaseline _baseline;
readonly ProjectId _baseProjectId;
readonly DeltaNaming _nextName;
public DeltaProject(BaselineArtifacts artifacts) {
_changeMaker = new EnC.ChangeMaker();
_solution = artifacts.baselineSolution;
_baseline = artifacts.emitBaseline;
_baseProjectId = artifacts.baselineProjectId;
_nextName = new DeltaNaming(artifacts.baselineOutputAsmPath, 1);
}
internal DeltaProject (DeltaProject prev, Solution newSolution, EmitBaseline newBaseline)
{
_changeMaker = prev._changeMaker;
_solution = newSolution;
_baseline = newBaseline;
_baseProjectId = prev._baseProjectId;
_nextName = prev._nextName.Next ();
}
public Solution Solution => _solution;
public EmitBaseline Baseline => _baseline;
public ProjectId BaseProjectId => _baseProjectId;
/// The default output function
/// Creates files with the specified DeltaNaming without any other side-effects
public static DeltaOutputStreams DefaultMakeFileOutputs (DeltaNaming dinfo) {
var metaStream = File.Create(dinfo.Dmeta);
var ilStream = File.Create(dinfo.Dil);
var pdbStream = File.Create(dinfo.Dpdb);
return new DeltaOutputStreams(metaStream, ilStream, pdbStream);
}
/// Builds a delta for the specified document given a path to its updated contents and a revision count
/// On failure throws a DiffyException and with exitStatus > 0
public async Task<DeltaProject> BuildDelta (Delta delta, bool ignoreUnchanged = false,
Func<DeltaNaming, DeltaOutputStreams>? makeOutputs = default,
Action<DeltaNaming, DeltaOutputStreams>? outputsReady = default,
CancellationToken ct = default)
{
var change = delta.Change;
var dinfo = _nextName;
Console.WriteLine ($"parsing patch #{dinfo.Rev} from {change.Update} and creating delta");
Project project = Solution.GetProject(BaseProjectId)!;
DocumentId baseDocumentId = change.Document;
Document document = project.GetDocument(baseDocumentId)!;
Document updatedDocument;
await using (var contents = File.OpenRead (change.Update)) {
Solution updatedSolution = Solution.WithDocumentText (baseDocumentId, SourceText.From (contents, Encoding.UTF8));
updatedDocument = updatedSolution.GetDocument(baseDocumentId)!;
}
if (updatedDocument.Project.Id != BaseProjectId)
throw new Exception ("Unexpectedly, project Id of the delta != base project Id");
if (updatedDocument.Id != baseDocumentId)
throw new Exception ("Unexpectedly, document Id of the delta != base document Id");
project = updatedDocument.Project;
var changes = await updatedDocument.GetTextChangesAsync (document, ct);
if (!changes.Any()) {
Console.WriteLine ("no changes found");
if (ignoreUnchanged)
return this;
//FIXME can continue here and just ignore the revision
throw new DiffyException ($"no changes in revision {dinfo.Rev}", exitStatus: 5);
}
Console.WriteLine ($"Found changes in {document.Name}");
Task<Compilation> updatedCompilation = Task.Run(async () => {
var compilation = await project.GetCompilationAsync (ct);
if (!CheckCompilationDiagnostics(compilation, $"delta {dinfo.Rev}"))
throw new DeltaCompilationException(exitStatus: 6);
else
return compilation;
}, ct);
var editsCompilation = Task.Run(() => CompileEdits (document, updatedDocument, ct), ct);
Compilation updatedCompilationResult = await updatedCompilation;
var (edits, rudeEdits) = await editsCompilation;
if (!rudeEdits.IsDefault && rudeEdits.Any() ) {
throw new DeltaRudeEditException($"rude edits in revision {dinfo.Rev}", rudeEdits);
}
if (edits.IsDefault || !edits.Any()) {
Console.WriteLine("no semantic changes");
if (ignoreUnchanged)
return this;
else
throw new DeltaCompilationException("no semantic changes in revision", exitStatus: 7);
}
var baseline = Baseline ?? throw new NullReferenceException ($"got a null baseline for revision {dinfo.Rev}");
var updatedMethods = new List<System.Reflection.Metadata.MethodDefinitionHandle> ();
EmitDifferenceResult emitResult;
await using (var output = makeOutputs != null ? makeOutputs(dinfo) : DefaultMakeFileOutputs(dinfo)) {
emitResult = updatedCompilationResult.EmitDifference(baseline, edits, output.MetaStream, output.IlStream, output.PdbStream, updatedMethods, ct);
CheckEmitResult(emitResult);
outputsReady?.Invoke(dinfo, output);
}
Console.WriteLine($"wrote {dinfo.Dmeta}");
// return a new deltaproject that can build the next update
return new DeltaProject(this, project.Solution, emitResult.Baseline);
}
Task<(ImmutableArray<SemanticEdit>, ImmutableArray<EnC.RudeEditDiagnosticWrapper>)> CompileEdits (Document document, Document updatedDocument, CancellationToken ct = default)
{
return _changeMaker.GetChanges(document, updatedDocument, ct);
}
/// <returns>true if compilation succeeded, or false if there were errors</returns>
private static bool CheckCompilationDiagnostics ([NotNullWhen(true)] Compilation? compilation, string diagnosticPrefix)
{
if (compilation == null) {
Console.WriteLine ($"{diagnosticPrefix} compilation was null");
return false;
} else {
bool failed = false;
foreach (var diag in compilation.GetDiagnostics ().Where (d => d.Severity == DiagnosticSeverity.Error)) {
Console.WriteLine ($"{diagnosticPrefix} --- {diag}");
failed = true;
}
return !failed;
}
}
/// <summary>Check <see cref="EmitResult"/> or <see cref="EmitDifferenceResult"/> for failures</summary>
private static bool CheckEmitResult (EmitResult emitResult)
{
if (!emitResult.Success) {
Console.WriteLine ("Emit failed");
foreach (var diag in emitResult.Diagnostics.Where (d => d.Severity == DiagnosticSeverity.Error))
Console.WriteLine (diag);
}
return emitResult.Success;
}
}
}