Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow WithArgs to accept ReferenceExpressions #4415

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Aspire.Hosting/ExecutableResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ public static class ExecutableResourceBuilderExtensions
/// <param name="args">The arguments to the executable.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<ExecutableResource> AddExecutable(this IDistributedApplicationBuilder builder, string name, string command, string workingDirectory, params string[]? args)
{
return AddExecutable(builder, name, command, workingDirectory, (object[]?)args);
}

/// <summary>
/// Adds an executable resource to the application model.
/// </summary>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
/// <param name="name">The name of the resource.</param>
/// <param name="command">The executable path. This can be a fully qualified path or a executable to run from the shell/command line.</param>
/// <param name="workingDirectory">The working directory of the executable.</param>
/// <param name="args">The arguments to the executable.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<ExecutableResource> AddExecutable(this IDistributedApplicationBuilder builder, string name, string command, string workingDirectory, params object[]? args)
{
workingDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, workingDirectory));

Expand Down
2 changes: 2 additions & 0 deletions src/Aspire.Hosting/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,5 @@ static Aspire.Hosting.ProjectResourceBuilderExtensions.AddProject(this Aspire.Ho
static Aspire.Hosting.ProjectResourceBuilderExtensions.AddProject<TProject>(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, System.Action<Aspire.Hosting.ProjectResourceOptions!>! configure) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ProjectResource!>!
static readonly Aspire.Hosting.ApplicationModel.KnownResourceStates.TerminalStates -> System.Collections.Generic.IReadOnlyList<string!>!
static readonly Aspire.Hosting.ApplicationModel.KnownResourceStates.Waiting -> string!
static Aspire.Hosting.ExecutableResourceBuilderExtensions.AddExecutable(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, string! command, string! workingDirectory, params object![]? args) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ExecutableResource!>!
static Aspire.Hosting.ResourceBuilderExtensions.WithArgs<T>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<T>! builder, params object![]! args) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<T>!
12 changes: 12 additions & 0 deletions src/Aspire.Hosting/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ public static IResourceBuilder<T> WithArgs<T>(this IResourceBuilder<T> builder,
return builder.WithArgs(context => context.Args.AddRange(args));
}

/// <summary>
/// Adds the arguments to be passed to a container resource when the container is started.
/// </summary>
/// <typeparam name="T">The resource type.</typeparam>
/// <param name="builder">The resource builder.</param>
/// <param name="args">The arguments to be passed to the container when it is started.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<T> WithArgs<T>(this IResourceBuilder<T> builder, params object[] args) where T : IResourceWithArgs
davidfowl marked this conversation as resolved.
Show resolved Hide resolved
{
return builder.WithArgs(context => context.Args.AddRange(args));
}

/// <summary>
/// Adds a callback to be executed with a list of command-line arguments when a container resource is started.
/// </summary>
Expand Down
19 changes: 14 additions & 5 deletions tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public async Task AddExecutableWithArgs()
var appBuilder = DistributedApplication.CreateBuilder();

var testResource = new TestResource("test", "connectionString");
var testResource2 = new TestResource("test2", "anotherConnectionString");

var exe1 = appBuilder.AddExecutable("e1", "ruby", ".", "app.rb")
.WithEndpoint("ep", e =>
Expand All @@ -23,12 +24,13 @@ public async Task AddExecutableWithArgs()
e.AllocatedEndpoint = new(e, "localhost", 1234);
});

var exe2 = appBuilder.AddExecutable("e2", "python", ".", "app.py")
var exe2 = appBuilder.AddExecutable("e2", "python", ".", "app.py", exe1.GetEndpoint("ep"))
.WithArgs("arg1", testResource)
.WithArgs(context =>
{
context.Args.Add("arg1");
context.Args.Add("arg2");
context.Args.Add(exe1.GetEndpoint("ep"));
context.Args.Add(testResource);
context.Args.Add(testResource2);
});

using var app = appBuilder.Build();
Expand All @@ -37,9 +39,13 @@ public async Task AddExecutableWithArgs()

Assert.Collection(args,
arg => Assert.Equal("app.py", arg),
arg => Assert.Equal("http://localhost:1234", arg),
arg => Assert.Equal("arg1", arg),
arg => Assert.Equal("connectionString", arg),
arg => Assert.Equal("arg2", arg),
arg => Assert.Equal("http://localhost:1234", arg),
arg => Assert.Equal("connectionString", arg));
arg => Assert.Equal("anotherConnectionString", arg)
);

var manifest = await ManifestUtils.GetManifest(exe2.Resource);

Expand All @@ -51,9 +57,12 @@ public async Task AddExecutableWithArgs()
"command": "python",
"args": [
"app.py",
"{e1.bindings.ep.url}",
"arg1",
"{test.connectionString}",
"arg2",
"{e1.bindings.ep.url}",
"{test.connectionString}"
"{test2.connectionString}"
]
}
""";
Expand Down