-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResourceBuilderProjectCommanderExtensions.cs
More file actions
281 lines (245 loc) · 11.6 KB
/
ResourceBuilderProjectCommanderExtensions.cs
File metadata and controls
281 lines (245 loc) · 11.6 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#pragma warning disable ASPIREINTERACTION001
using Aspire.Hosting.ApplicationModel;
using CommunityToolkit.Aspire.Hosting.ProjectCommander;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
// ReSharper disable once CheckNamespace
namespace Aspire.Hosting;
/// <summary>
/// Represents a command associated with a project, including its name and display name.
/// </summary>
/// <param name="Name">The unique name of the command. This value is typically used as an identifier.</param>
/// <param name="DisplayName">The user-friendly name of the command, intended for display in UI or logs.</param>
/// <param name="Arguments">Optional arguments to pass to the command.</param>
public record ProjectCommand(string Name, string DisplayName, params InteractionInput[] Arguments);
/// <summary>
/// Extension methods for configuring the Aspire Project Commander.
/// </summary>
public static class ResourceBuilderProjectCommanderExtensions
{
/// <summary>
/// Registers project commands from a projectcommander.json manifest file located in the project directory.
/// If no manifest file exists, no commands are registered.
/// This method can be combined with <see cref="WithProjectCommands{T}"/> to add additional commands.
/// </summary>
/// <typeparam name="T">The type of project resource.</typeparam>
/// <param name="builder">The resource builder.</param>
/// <returns>The resource builder for chaining.</returns>
public static IResourceBuilder<T> WithProjectManifest<T>(this IResourceBuilder<T> builder)
where T : ProjectResource
{
var projectPath = GetProjectDirectory(builder.Resource);
var manifest = ManifestReader.ReadManifest(projectPath);
if (manifest == null)
{
// No manifest found, nothing to register
return builder;
}
// Store startup form in annotation if present and register the configure command
if (manifest.StartupForm != null)
{
var startupFormAnnotation = new StartupFormAnnotation(manifest.StartupForm);
builder.WithAnnotation(startupFormAnnotation);
// Add environment variable so the client knows it needs to wait for startup form
builder.WithEnvironment("PROJECTCOMMANDER_STARTUP_FORM_REQUIRED", "true");
// Add a "Configure" command to trigger the startup form prompt
RegisterStartupFormCommand(builder, startupFormAnnotation);
}
// Register commands from manifest
if (manifest.Commands.Count > 0)
{
var projectCommands = manifest.Commands
.Select(c => new ProjectCommand(
c.Name,
c.DisplayName,
c.Inputs.Select(ManifestReader.ToInteractionInput).ToArray()))
.ToArray();
// Use the existing WithProjectCommands method to register
return builder.WithProjectCommands(projectCommands);
}
return builder;
}
/// <summary>
/// Registers the startup form command for a resource with a startup form.
/// The command is disabled after successful submission and requires a resource restart to re-enable.
/// </summary>
private static void RegisterStartupFormCommand<T>(IResourceBuilder<T> builder, StartupFormAnnotation annotation)
where T : ProjectResource
{
var form = annotation.Form;
var inputs = form.Inputs.Select(ManifestReader.ToInteractionInput).ToArray();
builder.WithCommand(
name: "projectcommander-configure",
displayName: form.Title,
executeCommand: async (context) =>
{
// Check if the startup form has already been completed
if (annotation.IsCompleted)
{
return new ExecuteCommandResult
{
Success = false,
ErrorMessage = "Startup form has already been submitted. Restart the resource to configure again."
};
}
try
{
var model = context.ServiceProvider.GetRequiredService<DistributedApplicationModel>();
var hubResource = model.Resources.OfType<ProjectCommanderHubResource>().SingleOrDefault();
if (hubResource?.Hub == null)
{
return new ExecuteCommandResult
{
Success = false,
ErrorMessage = "Project Commander hub is not running."
};
}
var interaction = context.ServiceProvider.GetRequiredService<IInteractionService>();
// Show the startup form prompt
var result = await interaction.PromptInputsAsync(
form.Title,
form.Description ?? "Please configure the following settings:",
inputs,
cancellationToken: context.CancellationToken);
if (result.Canceled)
{
return new ExecuteCommandResult
{
Success = false,
ErrorMessage = "Configuration cancelled."
};
}
// Build form data dictionary
var formData = new Dictionary<string, string?>();
for (var i = 0; i < inputs.Length; i++)
{
formData[inputs[i].Name] = result.Data[i].Value;
}
// Send form data to the project
var groupName = context.ResourceName;
await hubResource.Hub.Clients.Group(groupName).SendAsync(
"ReceiveStartupForm",
formData,
context.CancellationToken);
// Update annotation to mark as completed
annotation.FormData = formData;
annotation.IsCompleted = true;
return new ExecuteCommandResult { Success = true };
}
catch (Exception ex)
{
return new ExecuteCommandResult
{
Success = false,
ErrorMessage = ex.Message
};
}
},
new CommandOptions
{
IconName = "Settings",
IconVariant = IconVariant.Regular,
IsHighlighted = true,
// Dynamically update command state based on whether form is completed
UpdateState = (context) => annotation.IsCompleted
? ResourceCommandState.Disabled
: ResourceCommandState.Enabled
});
}
/// <summary>
/// Gets the project directory from a ProjectResource by reading its annotations.
/// </summary>
private static string GetProjectDirectory(ProjectResource resource)
{
// ProjectResource has IProjectMetadata annotation that contains the project path
var metadata = resource.Annotations.OfType<IProjectMetadata>().FirstOrDefault();
if (metadata == null)
{
throw new InvalidOperationException(
$"Resource '{resource.Name}' does not have project metadata. " +
"Ensure WithProjectManifest is called on a ProjectResource.");
}
return Path.GetDirectoryName(metadata.ProjectPath)
?? throw new InvalidOperationException(
$"Could not determine project directory from path: {metadata.ProjectPath}");
}
/// <summary>
/// Adds project commands to a project resource.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="builder"></param>
/// <param name="commands"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IResourceBuilder<T> WithProjectCommands<T>(
this IResourceBuilder<T> builder, params ProjectCommand[] commands)
where T : ProjectResource
{
if (commands.Length == 0)
{
throw new ArgumentException("You must supply at least one command.");
}
// Add command proxies to the dashboard
foreach (var command in commands)
{
builder.WithCommand(command.Name, command.DisplayName, async (context) =>
{
// Thank you LLMs for the idea, but
// // DO NOT DO THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// // it might work in simple cases, but it will fail in complex ones
// // trust me, I've tried
// // and this is what LLMs are good for, telling you what NOT to do
// // because you are all wondering what the heck I'm talking about
// // lets run this and see what happens
// // because I know you are all thinking it
// // and I don't want to hear it
// i++;
// builder.WithCommand($"dynamic-command-{i}", command.DisplayName, async (context) =>
// {
// return new ExecuteCommandResult() { Success = true };
// },
// new CommandOptions { });
bool success = false;
string errorMessage = string.Empty;
try
{
var model = context.ServiceProvider.GetRequiredService<DistributedApplicationModel>();
var hub = model.Resources.OfType<ProjectCommanderHubResource>().Single().Hub!;
if (command.Arguments.Length > 0)
{
var interaction = context.ServiceProvider.GetRequiredService<IInteractionService>();
var result = await interaction.PromptInputsAsync($"Arguments for {command.Name}", $"Arguments {command.Name}", command.Arguments, cancellationToken : context.CancellationToken);
if (result.Canceled)
{
return new ExecuteCommandResult() { Success = false, ErrorMessage = "User cancelled command." };
}
var args = new string?[command.Arguments.Length];
for (var i = 0; i < command.Arguments.Length; i++)
{
args[i] = result.Data[i].Value;
}
var groupName = context.ResourceName;
await hub.Clients.Group(groupName).SendAsync("ReceiveCommand", command.Name, args, context.CancellationToken);
}
else
{
var groupName = context.ResourceName;
await hub.Clients.Group(groupName).SendAsync("ReceiveCommand", command.Name, Array.Empty<string>(), context.CancellationToken);
}
success = true;
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
return new ExecuteCommandResult() { Success = success, ErrorMessage = errorMessage };
},
new CommandOptions
{
IconName = "DesktopSignal",
IconVariant = IconVariant.Regular
});
}
return builder;
}
}