Skip to content

Commit 0d3ab6c

Browse files
Copilotaaronpowell
andcommitted
Replace IDistributedApplicationLifecycleHook with event-based patterns
Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
1 parent 32efe61 commit 0d3ab6c

File tree

9 files changed

+71
-291
lines changed

9 files changed

+71
-291
lines changed

src/CommunityToolkit.Aspire.Hosting.Bun/BunAppExtensions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using Aspire.Hosting.ApplicationModel;
2-
using Aspire.Hosting.Lifecycle;
3-
using CommunityToolkit.Aspire.Hosting.Bun;
42
using CommunityToolkit.Aspire.Utils;
53
using Microsoft.Extensions.Hosting;
64

@@ -46,10 +44,27 @@ public static IResourceBuilder<BunAppResource> AddBunApp(
4644
/// Ensures the Bun packages are installed before the application starts using Bun as the package manager.
4745
/// </summary>
4846
/// <param name="resource">The Bun app resource.</param>
47+
/// <param name="configureInstaller">Configure the Bun installer resource.</param>
4948
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
50-
public static IResourceBuilder<BunAppResource> WithBunPackageInstallation(this IResourceBuilder<BunAppResource> resource)
49+
public static IResourceBuilder<BunAppResource> WithBunPackageInstallation(this IResourceBuilder<BunAppResource> resource, Action<IResourceBuilder<BunInstallerResource>>? configureInstaller = null)
5150
{
52-
resource.ApplicationBuilder.Services.TryAddLifecycleHook<BunPackageInstallerLifecycleHook>();
51+
// Only install packages during development, not in publish mode
52+
if (!resource.ApplicationBuilder.ExecutionContext.IsPublishMode)
53+
{
54+
var installerName = $"{resource.Resource.Name}-bun-install";
55+
var installer = new BunInstallerResource(installerName, resource.Resource.WorkingDirectory);
56+
57+
var installerBuilder = resource.ApplicationBuilder.AddResource(installer)
58+
.WithArgs("install")
59+
.WithParentRelationship(resource.Resource)
60+
.ExcludeFromManifest();
61+
62+
// Make the parent resource wait for the installer to complete
63+
resource.WaitForCompletion(installerBuilder);
64+
65+
configureInstaller?.Invoke(installerBuilder);
66+
}
67+
5368
return resource;
5469
}
5570

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Aspire.Hosting.ApplicationModel;
2+
3+
/// <summary>
4+
/// A resource that represents a Bun package installer.
5+
/// </summary>
6+
/// <param name="name">The name of the resource.</param>
7+
/// <param name="workingDirectory">The working directory to use for the command.</param>
8+
public class BunInstallerResource(string name, string workingDirectory)
9+
: ExecutableResource(name, "bun", workingDirectory);

src/CommunityToolkit.Aspire.Hosting.Bun/BunPackageInstallerLifecycleHook.cs

Lines changed: 0 additions & 126 deletions
This file was deleted.

src/CommunityToolkit.Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Aspire.Hosting;
55
using Aspire.Hosting.ApplicationModel;
66
using Aspire.Hosting.Eventing;
7-
using Aspire.Hosting.Lifecycle;
87
using Aspire.Hosting.Utils;
98
using Microsoft.Extensions.Configuration;
109
using Microsoft.Extensions.DependencyInjection;
@@ -23,7 +22,7 @@ internal sealed class DaprDistributedApplicationLifecycleHook(
2322
IConfiguration configuration,
2423
IHostEnvironment environment,
2524
ILogger<DaprDistributedApplicationLifecycleHook> logger,
26-
IOptions<DaprOptions> options) : IDistributedApplicationLifecycleHook, IDisposable
25+
IOptions<DaprOptions> options) : IDisposable
2726
{
2827
private readonly DaprOptions _options = options.Value;
2928

src/CommunityToolkit.Aspire.Hosting.Dapr/IDistributedApplicationBuilderExtensions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Aspire.Hosting.ApplicationModel;
5-
using Aspire.Hosting.Lifecycle;
5+
using Aspire.Hosting.Eventing;
66
using Aspire.Hosting.Publishing;
77
using Aspire.Hosting.Utils;
88
using CommunityToolkit.Aspire.Hosting.Dapr;
9+
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Hosting;
1012
using Microsoft.Extensions.Logging;
13+
using Microsoft.Extensions.Options;
1114

1215
namespace Aspire.Hosting;
1316

@@ -30,7 +33,16 @@ public static IDistributedApplicationBuilder AddDapr(this IDistributedApplicatio
3033
builder.Services.Configure(configure);
3134
}
3235

33-
builder.Services.TryAddLifecycleHook<DaprDistributedApplicationLifecycleHook>();
36+
builder.Eventing.Subscribe<BeforeStartEvent>(async (evt, ct) =>
37+
{
38+
var hook = new DaprDistributedApplicationLifecycleHook(
39+
evt.Services.GetRequiredService<IConfiguration>(),
40+
evt.Services.GetRequiredService<IHostEnvironment>(),
41+
evt.Services.GetRequiredService<ILogger<DaprDistributedApplicationLifecycleHook>>(),
42+
evt.Services.GetRequiredService<IOptions<DaprOptions>>());
43+
44+
await hook.BeforeStartAsync(evt.Model, ct).ConfigureAwait(false);
45+
});
3446

3547
return builder;
3648
}

src/CommunityToolkit.Aspire.Hosting.Deno/DenoAppHostingExtensions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Aspire.Hosting.ApplicationModel;
2-
using Aspire.Hosting.Lifecycle;
32
using Microsoft.Extensions.Hosting;
43
using CommunityToolkit.Aspire.Utils;
5-
using CommunityToolkit.Aspire.Hosting.Deno;
64

75
namespace Aspire.Hosting;
86
/// <summary>
@@ -73,10 +71,27 @@ public static IResourceBuilder<DenoAppResource> AddDenoTask(this IDistributedApp
7371
/// Ensures the Deno packages are installed before the application starts using Deno as the package manager.
7472
/// </summary>
7573
/// <param name="resource">The Deno app resource.</param>
74+
/// <param name="configureInstaller">Configure the Deno installer resource.</param>
7675
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
77-
public static IResourceBuilder<DenoAppResource> WithDenoPackageInstallation(this IResourceBuilder<DenoAppResource> resource)
76+
public static IResourceBuilder<DenoAppResource> WithDenoPackageInstallation(this IResourceBuilder<DenoAppResource> resource, Action<IResourceBuilder<DenoInstallerResource>>? configureInstaller = null)
7877
{
79-
resource.ApplicationBuilder.Services.TryAddLifecycleHook<DenoPackageInstallerLifecycleHook>();
78+
// Only install packages during development, not in publish mode
79+
if (!resource.ApplicationBuilder.ExecutionContext.IsPublishMode)
80+
{
81+
var installerName = $"{resource.Resource.Name}-deno-install";
82+
var installer = new DenoInstallerResource(installerName, resource.Resource.WorkingDirectory);
83+
84+
var installerBuilder = resource.ApplicationBuilder.AddResource(installer)
85+
.WithArgs("install")
86+
.WithParentRelationship(resource.Resource)
87+
.ExcludeFromManifest();
88+
89+
// Make the parent resource wait for the installer to complete
90+
resource.WaitForCompletion(installerBuilder);
91+
92+
configureInstaller?.Invoke(installerBuilder);
93+
}
94+
8095
return resource;
8196
}
8297

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Aspire.Hosting.ApplicationModel;
2+
3+
/// <summary>
4+
/// A resource that represents a Deno package installer.
5+
/// </summary>
6+
/// <param name="name">The name of the resource.</param>
7+
/// <param name="workingDirectory">The working directory to use for the command.</param>
8+
public class DenoInstallerResource(string name, string workingDirectory)
9+
: ExecutableResource(name, "deno", workingDirectory);

src/CommunityToolkit.Aspire.Hosting.Deno/DenoPackageInstaller.cs

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)