-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3b960a
commit ea1d998
Showing
11 changed files
with
315 additions
and
7 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
5 changes: 5 additions & 0 deletions
5
...re.Hosting.Azure.StaticWebApps/CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps.csproj
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,5 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting" /> | ||
</ItemGroup> | ||
</Project> |
8 changes: 8 additions & 0 deletions
8
src/CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps/SwaApiEndpointAnnotation.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,8 @@ | ||
using Aspire.Hosting.ApplicationModel; | ||
|
||
namespace CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps; | ||
|
||
public class SwaApiEndpointAnnotation(IResourceBuilder<IResourceWithEndpoints> resource) : IResourceAnnotation | ||
{ | ||
public string Endpoint => resource.Resource.GetEndpoint("http").Url; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps/SwaAppEndpointAnnotation.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,8 @@ | ||
using Aspire.Hosting.ApplicationModel; | ||
|
||
namespace CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps; | ||
|
||
public class SwaAppEndpointAnnotation(IResourceBuilder<IResourceWithEndpoints> resource) : IResourceAnnotation | ||
{ | ||
public string Endpoint => resource.Resource.GetEndpoint("http").Url; | ||
} |
60 changes: 60 additions & 0 deletions
60
src/CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps/SwaAppHostingExtension.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,60 @@ | ||
using Aspire.Hosting; | ||
using Aspire.Hosting.ApplicationModel; | ||
|
||
namespace CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps; | ||
|
||
public static class SwaAppHostingExtension | ||
{ | ||
/// <summary> | ||
/// Adds a Static Web Apps emulator to the application. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/> to add the resource to.</param> | ||
/// <param name="name">The name of the resource.</param> | ||
/// <param name="options">The <see cref="JavaAppContainerResourceOptions"/> to configure the Java application.</param>" | ||
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns> | ||
/// <remarks>This resource will not be included in the published manifest.</remarks> | ||
public static IResourceBuilder<SwaResource> AddSwaEmulator(this IDistributedApplicationBuilder builder, string name, int port = 4280) | ||
{ | ||
var resource = new SwaResource(name, Environment.CurrentDirectory); | ||
return builder.AddResource(resource) | ||
.WithHttpEndpoint(isProxied: false, port: port) | ||
.WithArgs(ctx => | ||
{ | ||
ctx.Args.Add("start"); | ||
|
||
if (resource.TryGetAnnotationsOfType<SwaAppEndpointAnnotation>(out var appResource)) | ||
{ | ||
ctx.Args.Add("--app-devserver-url"); | ||
ctx.Args.Add(appResource.First().Endpoint); | ||
} | ||
|
||
if (resource.TryGetAnnotationsOfType<SwaApiEndpointAnnotation>(out var apiResource)) | ||
{ | ||
ctx.Args.Add("--api-devserver-url"); | ||
ctx.Args.Add(apiResource.First().Endpoint); | ||
} | ||
|
||
ctx.Args.Add("--port"); | ||
ctx.Args.Add(port.ToString()); | ||
}) | ||
.ExcludeFromManifest(); | ||
} | ||
|
||
/// <summary> | ||
/// Registers the application resource with the Static Web Apps emulator. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/> to add the resource to.</param> | ||
/// <param name="appResource">The existing <see cref="IResourceBuilder{IResourceWithEndpoint}"/> to use as the <c>--app-devserver-url</c> argument.</param> | ||
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns> | ||
public static IResourceBuilder<SwaResource> WithAppResource(this IResourceBuilder<SwaResource> builder, IResourceBuilder<IResourceWithEndpoints> appResource) => | ||
builder.WithAnnotation<SwaAppEndpointAnnotation>(new(appResource), ResourceAnnotationMutationBehavior.Replace); | ||
|
||
/// <summary> | ||
/// Registers the API resource with the Static Web Apps emulator. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/> to add the resource to.</param> | ||
/// <param name="apiResource">The existing <see cref="IResourceBuilder{IResourceWithEndpoint}"/> to use as the <c>--api-devserver-url</c> argument.</param> | ||
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns> | ||
public static IResourceBuilder<SwaResource> WithApiResource(this IResourceBuilder<SwaResource> builder, IResourceBuilder<IResourceWithEndpoints> apiResource) => | ||
builder.WithAnnotation<SwaApiEndpointAnnotation>(new(apiResource), ResourceAnnotationMutationBehavior.Replace); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps/SwaResource.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,7 @@ | ||
using Aspire.Hosting.ApplicationModel; | ||
|
||
namespace CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps; | ||
|
||
public class SwaResource(string name, string workingDirectory) : ExecutableResource(name, "swa", workingDirectory) | ||
{ | ||
} |
23 changes: 23 additions & 0 deletions
23
...zure.StaticWebApps.Tests/CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps.Tests.csproj
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps\CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
178 changes: 178 additions & 0 deletions
178
test/CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps.Tests/ResourceCreationTests.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,178 @@ | ||
using Aspire.Hosting; | ||
using Aspire.Hosting.ApplicationModel; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps.Tests; | ||
|
||
public class ResourceCreationTests | ||
{ | ||
[Fact] | ||
public void TargetPort_Defaults_to_4280() | ||
{ | ||
var builder = DistributedApplication.CreateBuilder(); | ||
|
||
builder.AddSwaEmulator("swa"); | ||
|
||
using var app = builder.Build(); | ||
|
||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
var resource = appModel.Resources.OfType<SwaResource>().SingleOrDefault(); | ||
|
||
Assert.NotNull(resource); | ||
|
||
Assert.Equal("swa", resource.Name); | ||
|
||
var httpEndpoint = resource.GetEndpoint("http"); | ||
Assert.Equal(4280, httpEndpoint.TargetPort); | ||
} | ||
|
||
[Fact] | ||
public void TargetPort_Can_Be_Overridden() | ||
{ | ||
var builder = DistributedApplication.CreateBuilder(); | ||
|
||
builder.AddSwaEmulator("swa", port: 1234); | ||
|
||
using var app = builder.Build(); | ||
|
||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
var resource = appModel.Resources.OfType<SwaResource>().SingleOrDefault(); | ||
|
||
Assert.NotNull(resource); | ||
|
||
Assert.Equal("swa", resource.Name); | ||
|
||
var httpEndpoint = resource.GetEndpoint("http"); | ||
Assert.Equal(1234, httpEndpoint.TargetPort); | ||
} | ||
|
||
[Fact] | ||
public void AppResource_Can_Be_Set() | ||
{ | ||
var builder = DistributedApplication.CreateBuilder(); | ||
|
||
var appResource = builder | ||
.AddContainer("app", "test/container") // container image doesn't need to be valid as we aren't actually running it | ||
.WithHttpEndpoint(); | ||
|
||
builder.AddSwaEmulator("swa") | ||
.WithAppResource(appResource); | ||
|
||
using var app = builder.Build(); | ||
|
||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
var resource = appModel.Resources.OfType<SwaResource>().SingleOrDefault(); | ||
|
||
Assert.NotNull(resource); | ||
|
||
Assert.Equal("swa", resource.Name); | ||
|
||
var result = resource.TryGetAnnotationsOfType<SwaAppEndpointAnnotation>(out var appResources); | ||
|
||
Assert.True(result); | ||
Assert.NotNull(appResources); | ||
Assert.Single(appResources); | ||
} | ||
|
||
[Fact] | ||
public void ApiResource_Can_Be_Set() | ||
{ | ||
var builder = DistributedApplication.CreateBuilder(); | ||
|
||
var apiResource = builder | ||
.AddContainer("api", "test/container") // container image doesn't need to be valid as we aren't actually running it | ||
.WithHttpEndpoint(); | ||
|
||
builder.AddSwaEmulator("swa") | ||
.WithApiResource(apiResource); | ||
|
||
using var app = builder.Build(); | ||
|
||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
var resource = appModel.Resources.OfType<SwaResource>().SingleOrDefault(); | ||
|
||
Assert.NotNull(resource); | ||
|
||
Assert.Equal("swa", resource.Name); | ||
|
||
var result = resource.TryGetAnnotationsOfType<SwaApiEndpointAnnotation>(out var apiResources); | ||
|
||
Assert.True(result); | ||
Assert.NotNull(apiResources); | ||
|
||
Assert.Single(apiResources); | ||
} | ||
|
||
[Fact] | ||
public void Start_Will_Be_An_Arg() | ||
{ | ||
var builder = DistributedApplication.CreateBuilder(); | ||
|
||
builder.AddSwaEmulator("swa"); | ||
|
||
using var app = builder.Build(); | ||
|
||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
var resource = appModel.Resources.OfType<SwaResource>().SingleOrDefault(); | ||
|
||
Assert.NotNull(resource); | ||
|
||
Assert.Equal("swa", resource.Name); | ||
|
||
var result = resource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var annotations); | ||
|
||
Assert.True(result); | ||
Assert.NotNull(annotations); | ||
|
||
Assert.Single(annotations); | ||
|
||
var annotation = annotations.Single(); | ||
|
||
List<object> args = []; | ||
var ctx = new CommandLineArgsCallbackContext(args); | ||
|
||
annotation.Callback(ctx); | ||
|
||
Assert.Contains("start", args); | ||
} | ||
|
||
[Fact] | ||
public void Port_Will_Be_An_Arg() | ||
{ | ||
var builder = DistributedApplication.CreateBuilder(); | ||
|
||
builder.AddSwaEmulator("swa"); | ||
|
||
using var app = builder.Build(); | ||
|
||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
var resource = appModel.Resources.OfType<SwaResource>().SingleOrDefault(); | ||
|
||
Assert.NotNull(resource); | ||
|
||
Assert.Equal("swa", resource.Name); | ||
|
||
var result = resource.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var annotations); | ||
|
||
Assert.True(result); | ||
Assert.NotNull(annotations); | ||
|
||
Assert.Single(annotations); | ||
|
||
var annotation = annotations.Single(); | ||
|
||
List<object> args = []; | ||
var ctx = new CommandLineArgsCallbackContext(args); | ||
|
||
annotation.Callback(ctx); | ||
|
||
Assert.Contains("--port", args); | ||
Assert.Contains("4280", args); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project> | ||
<Import Project="..\Directory.Build.props" /> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting.Testing" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Aspire.Hosting.Testing" /> | ||
</ItemGroup> | ||
</Project> |