-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathLiveRunner.cs
56 lines (47 loc) · 2.45 KB
/
LiveRunner.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
// 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.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
namespace Microsoft.DotNet.HotReload.Utils.Generator.Runners;
/// Generate deltas by watching for changes to the source files of the project
public class LiveRunner : Runner {
public LiveRunner (Config config) : base (config) { }
protected override Task PrepareToRun (CancellationToken ct = default) => Task.CompletedTask;
public override IAsyncEnumerable<Delta> SetupDeltas (BaselineArtifacts baselineArtifacts, CancellationToken ct = default)
{
return Livecoding (baselineArtifacts, config.LiveCodingWatchDir, config.LiveCodingWatchPattern, ct);
}
protected override bool PrepareCapabilitiesCore (out EnC.EditAndContinueCapabilities caps) {
caps = EnC.EditAndContinueCapabilities.None;
return false;
}
private static async IAsyncEnumerable<Delta> Livecoding (BaselineArtifacts baselineArtifacts, string watchDir, string pattern, [EnumeratorCancellation] CancellationToken cancellationToken= default) {
var last = DateTime.UtcNow;
var interval = TimeSpan.FromMilliseconds(250); /* FIXME: make this configurable */
var docResolver = baselineArtifacts.DocResolver;
var baselineProjectId = baselineArtifacts.BaselineProjectId;
using var fswgen = new Util.FSWGen (watchDir, pattern);
await foreach (var fsevent in fswgen.Watch(cancellationToken).ConfigureAwait(false)) {
if ((fsevent.ChangeType & WatcherChangeTypes.Changed) != 0) {
var e = DateTime.UtcNow;
Console.WriteLine($"change in {fsevent.FullPath} is a {fsevent.ChangeType} at {e}");
if (e - last < interval) {
Console.WriteLine($"too soon {e-last}");
continue;
}
Console.WriteLine($"more than 250ms since last change");
last = e;
var fp = fsevent.FullPath;
if (!docResolver.TryResolveDocumentId(fp, out var id)) {
Console.WriteLine ($"ignoring change in {fp} which is not in {baselineProjectId}");
continue;
}
yield return new Delta (Plan.Change.Create(id, fp));
}
}
}
}