This repository has been archived by the owner on Dec 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
/
DesignTimeHostCompiler.cs
134 lines (119 loc) · 4.57 KB
/
DesignTimeHostCompiler.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Framework.Runtime.Compilation;
namespace Microsoft.Framework.Runtime
{
public class DesignTimeHostCompiler : IDesignTimeHostCompiler
{
private readonly ProcessingQueue _queue;
private readonly IApplicationShutdown _shutdown;
private readonly ConcurrentDictionary<int, TaskCompletionSource<CompileResponse>> _compileResponses = new ConcurrentDictionary<int, TaskCompletionSource<CompileResponse>>();
private readonly TaskCompletionSource<Dictionary<string, int>> _projectContexts = new TaskCompletionSource<Dictionary<string, int>>();
private readonly IFileWatcher _watcher;
public DesignTimeHostCompiler(IApplicationShutdown shutdown, IFileWatcher watcher, Stream stream)
{
_shutdown = shutdown;
_watcher = watcher;
_queue = new ProcessingQueue(stream);
_queue.ProjectCompiled += OnProjectCompiled;
_queue.ProjectsInitialized += ProjectContextsInitialized;
_queue.ProjectChanged += _ => { };
_queue.ProjectSources += files =>
{
foreach (var file in files)
{
watcher.WatchFile(file);
}
};
_queue.Error += OnError;
_queue.Closed += OnClosed;
_queue.Start();
_queue.Send(new EnumerateProjectContextsMessage());
}
private void OnError(int? contextId, string error)
{
var exception = new InvalidOperationException(error);
if (contextId == null || contextId == -1)
{
_projectContexts.TrySetException(exception);
_shutdown.RequestShutdown();
}
else
{
_compileResponses.AddOrUpdate(contextId.Value,
_ =>
{
var tcs = new TaskCompletionSource<CompileResponse>();
tcs.SetException(exception);
return tcs;
},
(_, existing) =>
{
if (!existing.TrySetException(exception))
{
var tcs = new TaskCompletionSource<CompileResponse>();
tcs.TrySetException(exception);
return tcs;
}
return existing;
});
}
}
public async Task<CompileResponse> Compile(string projectPath, ILibraryKey library)
{
var contexts = await _projectContexts.Task.ConfigureAwait(false);
int contextId;
if (!contexts.TryGetValue(projectPath, out contextId))
{
// This should never happen
throw new InvalidOperationException();
}
_queue.Send(new GetCompiledAssemblyMessage
{
Name = library.Name,
Configuration = library.Configuration,
TargetFramework = library.TargetFramework,
Aspect = library.Aspect,
ContextId = contextId
});
_watcher.WatchProject(projectPath);
var task = _compileResponses.GetOrAdd(contextId, _ => new TaskCompletionSource<CompileResponse>()).Task;
return await task.ConfigureAwait(false);
}
private void OnClosed()
{
// Cancel all pending responses
foreach (var q in _compileResponses)
{
q.Value.TrySetCanceled();
}
}
private void ProjectContextsInitialized(Dictionary<string, int> projectContexts)
{
_projectContexts.TrySetResult(projectContexts);
}
private void OnProjectCompiled(int contextId, CompileResponse response)
{
_compileResponses.AddOrUpdate(contextId,
_ =>
{
var tcs = new TaskCompletionSource<CompileResponse>();
tcs.SetResult(response);
return tcs;
},
(_, existing) =>
{
if (!existing.TrySetResult(response))
{
var tcs = new TaskCompletionSource<CompileResponse>();
tcs.SetResult(response);
return tcs;
}
return existing;
});
}
}
}