-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command annotations and wire up (#5538)
- Loading branch information
Showing
49 changed files
with
1,239 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ public enum KnownResourceState | |
Running, | ||
Building, | ||
Hidden, | ||
Waiting | ||
Waiting, | ||
Stopping | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/Aspire.Hosting/ApplicationModel/CommandsConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Hosting.Dcp; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Aspire.Hosting.ApplicationModel; | ||
|
||
internal static class CommandsConfigurationExtensions | ||
{ | ||
internal const string StartType = "start"; | ||
internal const string StopType = "stop"; | ||
internal const string RestartType = "restart"; | ||
|
||
internal static IResourceBuilder<T> WithLifeCycleCommands<T>(this IResourceBuilder<T> builder) where T : IResource | ||
{ | ||
builder.WithCommand( | ||
type: StartType, | ||
displayName: "Start", | ||
executeCommand: async context => | ||
{ | ||
var executor = context.ServiceProvider.GetRequiredService<ApplicationExecutor>(); | ||
|
||
await executor.StartResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false); | ||
return CommandResults.Success(); | ||
}, | ||
updateState: context => | ||
{ | ||
if (IsStarting(context.ResourceSnapshot.State?.Text)) | ||
{ | ||
return ResourceCommandState.Disabled; | ||
} | ||
else if (IsStopped(context.ResourceSnapshot.State?.Text)) | ||
{ | ||
return ResourceCommandState.Enabled; | ||
} | ||
else | ||
{ | ||
return ResourceCommandState.Hidden; | ||
} | ||
}, | ||
iconName: "Play", | ||
isHighlighted: true); | ||
|
||
builder.WithCommand( | ||
type: StopType, | ||
displayName: "Stop", | ||
executeCommand: async context => | ||
{ | ||
var executor = context.ServiceProvider.GetRequiredService<ApplicationExecutor>(); | ||
|
||
await executor.StopResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false); | ||
return CommandResults.Success(); | ||
}, | ||
updateState: context => | ||
{ | ||
if (IsWaiting(context.ResourceSnapshot.State?.Text) || IsStopping(context.ResourceSnapshot.State?.Text)) | ||
{ | ||
return ResourceCommandState.Disabled; | ||
} | ||
else if (!IsStopped(context.ResourceSnapshot.State?.Text) && !IsStarting(context.ResourceSnapshot.State?.Text)) | ||
{ | ||
return ResourceCommandState.Enabled; | ||
} | ||
else | ||
{ | ||
return ResourceCommandState.Hidden; | ||
} | ||
}, | ||
iconName: "Stop", | ||
isHighlighted: true); | ||
|
||
builder.WithCommand( | ||
type: RestartType, | ||
displayName: "Restart", | ||
executeCommand: async context => | ||
{ | ||
var executor = context.ServiceProvider.GetRequiredService<ApplicationExecutor>(); | ||
|
||
await executor.StopResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false); | ||
await executor.StartResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false); | ||
return CommandResults.Success(); | ||
}, | ||
updateState: context => | ||
{ | ||
if (IsWaiting(context.ResourceSnapshot.State?.Text) || IsStarting(context.ResourceSnapshot.State?.Text) || IsStopping(context.ResourceSnapshot.State?.Text) || IsStopped(context.ResourceSnapshot.State?.Text)) | ||
{ | ||
return ResourceCommandState.Disabled; | ||
} | ||
else | ||
{ | ||
return ResourceCommandState.Enabled; | ||
} | ||
}, | ||
iconName: "ArrowCounterclockwise", | ||
isHighlighted: false); | ||
|
||
return builder; | ||
|
||
static bool IsStopped(string? state) => state is "Exited" or "Finished" or "FailedToStart"; | ||
static bool IsStopping(string? state) => state is "Stopping"; | ||
static bool IsStarting(string? state) => state is "Starting"; | ||
static bool IsWaiting(string? state) => state is "Waiting"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.