-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
234 lines (201 loc) · 6.87 KB
/
Program.cs
File metadata and controls
234 lines (201 loc) · 6.87 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System.ComponentModel;
using System.Diagnostics;
namespace AHKFlowApp.Launcher;
internal static class Program
{
private const string DefaultApiProfile = "LocalDB SQL";
private const string DefaultUiProfile = "http";
private const int ReadinessAttempts = 120;
private static readonly TimeSpan ReadinessDelay = TimeSpan.FromMilliseconds(500);
public static async Task<int> Main()
{
string rootDirectory = FindRepositoryRoot();
string apiProfile = Environment.GetEnvironmentVariable("AHKFLOW_API_PROFILE") ?? DefaultApiProfile;
string uiProfile = Environment.GetEnvironmentVariable("AHKFLOW_UI_PROFILE") ?? DefaultUiProfile;
IReadOnlyList<ProjectLaunch> launches = LauncherPlan.Create(apiProfile, uiProfile);
using CancellationTokenSource shutdown = new();
List<Process> processes = [];
Console.CancelKeyPress += (_, eventArgs) =>
{
eventArgs.Cancel = true;
shutdown.Cancel();
};
Task? browserLaunches = null;
try
{
foreach (ProjectLaunch launch in launches)
{
processes.Add(StartProject(rootDirectory, launch));
}
Console.WriteLine("AHKFlowApp is starting.");
Console.WriteLine($"API profile: {apiProfile}");
Console.WriteLine($"UI profile: {uiProfile}");
Console.WriteLine($"API: {LauncherPlan.ApiUrl}");
Console.WriteLine($"UI: {LauncherPlan.FrontendUrl}");
Console.WriteLine("Opening the UI in the browser when it is ready.");
Console.WriteLine("Press Ctrl+C to stop both projects.");
browserLaunches = OpenBrowsersWhenReadyAsync(launches, shutdown.Token);
int exitCode = await WaitForFirstExitAsync(processes, shutdown.Token);
return exitCode;
}
finally
{
shutdown.Cancel();
if (browserLaunches is not null)
{
await IgnoreCancellationAsync(browserLaunches);
}
foreach (Process process in processes)
{
StopProcess(process);
}
}
}
private static string FindRepositoryRoot()
{
return SearchParents(Directory.GetCurrentDirectory())
?? SearchParents(AppContext.BaseDirectory)
?? throw new InvalidOperationException("Could not locate the repository root.");
}
private static string? SearchParents(string startDirectory)
{
string directory = startDirectory;
while (!File.Exists(Path.Combine(directory, "AHKFlowApp.slnx")))
{
DirectoryInfo? parent = Directory.GetParent(directory);
if (parent is null)
{
return null;
}
directory = parent.FullName;
}
return directory;
}
private static Process StartProject(string rootDirectory, ProjectLaunch launch)
{
ProcessStartInfo startInfo = new("dotnet")
{
WorkingDirectory = rootDirectory,
UseShellExecute = false
};
startInfo.ArgumentList.Add("run");
startInfo.ArgumentList.Add("--project");
startInfo.ArgumentList.Add(launch.ProjectPath);
startInfo.ArgumentList.Add("--launch-profile");
startInfo.ArgumentList.Add(launch.LaunchProfile);
Process process = Process.Start(startInfo)
?? throw new InvalidOperationException($"Failed to start {launch.Name} project.");
Console.WriteLine($"{launch.Name} process started with PID {process.Id}.");
return process;
}
private static async Task OpenBrowsersWhenReadyAsync(
IReadOnlyList<ProjectLaunch> launches,
CancellationToken cancellationToken)
{
using HttpClient httpClient = new();
Task[] browserTasks = launches
.Select(launch => OpenBrowserWhenReadyAsync(httpClient, launch, cancellationToken))
.ToArray();
await Task.WhenAll(browserTasks);
}
private static async Task OpenBrowserWhenReadyAsync(
HttpClient httpClient,
ProjectLaunch launch,
CancellationToken cancellationToken)
{
if (launch.BrowserUrl is null)
{
return;
}
bool isReady = await WaitForUrlAsync(httpClient, launch.ReadyUrl, cancellationToken);
if (!isReady && !cancellationToken.IsCancellationRequested)
{
Console.WriteLine($"{launch.Name} did not become ready. Opening {launch.BrowserUrl} anyway.");
}
if (!cancellationToken.IsCancellationRequested)
{
OpenBrowser(launch.BrowserUrl);
}
}
private static async Task<bool> WaitForUrlAsync(
HttpClient httpClient,
string url,
CancellationToken cancellationToken)
{
for (int attempt = 0; attempt < ReadinessAttempts; attempt++)
{
try
{
using HttpResponseMessage response = await httpClient.GetAsync(url, cancellationToken);
if ((int)response.StatusCode < 500)
{
return true;
}
}
catch (HttpRequestException)
{
}
await Task.Delay(ReadinessDelay, cancellationToken);
}
return false;
}
private static void OpenBrowser(string url)
{
try
{
Process.Start(new ProcessStartInfo(url)
{
UseShellExecute = true
});
}
catch (Win32Exception exception)
{
Console.WriteLine($"Could not open {url}: {exception.Message}");
}
catch (InvalidOperationException exception)
{
Console.WriteLine($"Could not open {url}: {exception.Message}");
}
}
private static async Task<int> WaitForFirstExitAsync(
IReadOnlyList<Process> processes,
CancellationToken cancellationToken)
{
Task[] waitTasks = processes.Select(process => process.WaitForExitAsync(cancellationToken)).ToArray();
try
{
await Task.WhenAny(waitTasks);
}
catch (OperationCanceledException)
{
return 0;
}
Process? exitedProcess = processes.FirstOrDefault(process => process.HasExited);
return exitedProcess?.ExitCode ?? 0;
}
private static void StopProcess(Process process)
{
if (process.HasExited)
{
return;
}
try
{
process.Kill(entireProcessTree: true);
process.WaitForExit();
}
catch (InvalidOperationException)
{
}
}
private static async Task IgnoreCancellationAsync(Task task)
{
try
{
await task;
}
catch (OperationCanceledException)
{
}
}
}