forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServerProcessStateObserver.cs
More file actions
50 lines (40 loc) · 1.9 KB
/
WebServerProcessStateObserver.cs
File metadata and controls
50 lines (40 loc) · 1.9 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text.RegularExpressions;
using Microsoft.Build.Graph;
namespace Microsoft.DotNet.Watch;
/// <summary>
/// Observes the state of the web server by scanning its standard output for known patterns.
/// Notifies when the server starts listening.
/// </summary>
internal static partial class WebServerProcessStateObserver
{
private static readonly Regex s_nowListeningRegex = GetNowListeningOnRegex();
private static readonly Regex s_aspireDashboardUrlRegex = GetAspireDashboardUrlRegex();
[GeneratedRegex(@"Now listening on: (?<url>.*)\s*$", RegexOptions.Compiled)]
private static partial Regex GetNowListeningOnRegex();
[GeneratedRegex(@"Login to the dashboard at (?<url>.*)\s*$", RegexOptions.Compiled)]
private static partial Regex GetAspireDashboardUrlRegex();
public static Action<OutputLine> GetObserver(ProjectGraphNode serverProject, Action<string> onServerListening)
{
// Workaround for Aspire dashboard launching: scan for "Login to the dashboard at " prefix in the output and use the URL.
// TODO: https://github.com/dotnet/sdk/issues/9038
// Share launch profile processing logic as implemented in VS with dotnet-run and implement browser launching there.
bool isAspireHost = serverProject.GetCapabilities().Contains(AspireServiceFactory.AppHostProjectCapability);
var _notified = false;
return line =>
{
if (_notified)
{
return;
}
var match = (isAspireHost ? s_aspireDashboardUrlRegex : s_nowListeningRegex).Match(line.Content);
if (!match.Success)
{
return;
}
_notified = true;
onServerListening(match.Groups["url"].Value);
};
}
}