Skip to content
Closed
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
5 changes: 5 additions & 0 deletions src/Aspire.Cli/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public NewCommand(IDotNetCliRunner runner, INuGetPackageCache nuGetPackageCache,
templateVersionOption.Recursive = true;
Options.Add(templateVersionOption);

var frameworkOption = new Option<string?>("--framework", "-f");
frameworkOption.Description = NewCommandStrings.FrameworkArgumentDescription;
frameworkOption.Recursive = true;
Options.Add(frameworkOption);

_templates = templateProvider.GetTemplates();

foreach (var template in _templates)
Expand Down
23 changes: 20 additions & 3 deletions src/Aspire.Cli/DotNetCliRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal interface IDotNetCliRunner
Task<int> CheckHttpCertificateAsync(DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken);
Task<int> TrustHttpCertificateAsync(DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken);
Task<(int ExitCode, string? TemplateVersion)> InstallTemplateAsync(string packageName, string version, string? nugetSource, bool force, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken);
Task<int> NewProjectAsync(string templateName, string name, string outputPath, string[] extraArgs, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken);
Task<int> NewProjectAsync(string templateName, string name, string outputPath, string? framework, string[] extraArgs, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken);
Task<int> BuildAsync(FileInfo projectFilePath, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken);
Task<int> AddPackageAsync(FileInfo projectFilePath, string packageName, string packageVersion, string? nugetSource, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken);
Task<(int ExitCode, NuGetPackage[]? Packages)> SearchPackagesAsync(DirectoryInfo workingDirectory, string query, bool prerelease, int take, int skip, string? nugetSource, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken);
Expand Down Expand Up @@ -345,11 +345,28 @@ private static bool TryParsePackageVersionFromStdout(string stdout, [NotNullWhen
}
}

public async Task<int> NewProjectAsync(string templateName, string name, string outputPath, string[] extraArgs, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken)
public async Task<int> NewProjectAsync(string templateName, string name, string outputPath, string? framework, string[] extraArgs, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken)
{
using var activity = telemetry.ActivitySource.StartActivity();

string[] cliArgs = ["new", templateName, "--name", name, "--output", outputPath, ..extraArgs];
var cliArgsList = new List<string>
{
"new",
templateName,
"--name",
name,
"--output",
outputPath
};

if (!string.IsNullOrEmpty(framework))
{
cliArgsList.Add("--framework");
cliArgsList.Add(framework);
}

string[] cliArgs = [.. cliArgsList, ..extraArgs];

return await ExecuteAsync(
args: cliArgs,
env: null,
Expand Down
6 changes: 6 additions & 0 deletions src/Aspire.Cli/Resources/NewCommandStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Aspire.Cli/Resources/NewCommandStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
<data name="VersionArgumentDescription" xml:space="preserve">
<value>The version of the project templates to use.</value>
</data>
<data name="FrameworkArgumentDescription" xml:space="preserve">
<value>The target framework for the project.</value>
</data>
<data name="SelectATemplateVersion" xml:space="preserve">
<value>Select a template version:</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Aspire.Cli/Resources/xlf/NewCommandStrings.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Aspire.Cli/Templating/DotNetTemplateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ private async Task<int> ApplyTemplateAsync(CallbackTemplate template, ParseResul
{
var name = await GetProjectNameAsync(parseResult, cancellationToken);
var outputPath = await GetOutputPathAsync(parseResult, template.PathDeriver, name, cancellationToken);
var framework = parseResult.GetValue<string?>("--framework");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add prompting for selecting the framework for when people execute aspire new without arguments. interactive is the primary interaction model for aspire new.


// Some templates have additional arguments that need to be applied to the `dotnet new` command
// when it is executed. This callback will get those arguments and potentially prompt for them.
Expand Down Expand Up @@ -244,6 +245,7 @@ private async Task<int> ApplyTemplateAsync(CallbackTemplate template, ParseResul
template.Name,
name,
outputPath,
framework,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass this argument through via extraArgs.

extraArgs,
options,
cancellationToken);
Expand Down
55 changes: 52 additions & 3 deletions tests/Aspire.Cli.Tests/Commands/NewCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,11 @@ public async Task NewCommand_EmptyPackageList_DisplaysErrorMessage()
var result = command.Parse("new");

var exitCode = await result.InvokeAsync().WaitAsync(CliTestConstants.DefaultTimeout);

Assert.Equal(ExitCodeConstants.FailedToCreateNewProject, exitCode);
Assert.Contains("No template versions were found", displayedErrorMessage);
}

[Fact]
public async Task NewCommand_WhenCertificateServiceThrows_ReturnsNonZeroExitCode()
{
Expand Down Expand Up @@ -554,7 +554,7 @@ public async Task NewCommand_WhenCertificateServiceThrows_ReturnsNonZeroExitCode
return (0, version); // Success, return the template version
};

runner.NewProjectAsyncCallback = (templateName, name, outputPath, options, cancellationToken) =>
runner.NewProjectAsyncCallback = (templateName, name, outputPath, framework, options, cancellationToken) =>
{
return 0; // Success
};
Expand All @@ -571,6 +571,55 @@ public async Task NewCommand_WhenCertificateServiceThrows_ReturnsNonZeroExitCode
Assert.Equal(ExitCodeConstants.FailedToTrustCertificates, exitCode);
}

[Fact]
public async Task NewCommand_WhenFrameworkSpecified_UsesCorrectFramework()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var services = CliTestHelper.CreateServiceCollection(workspace, outputHelper, options => {

// Set of options that we'll give when prompted.
options.NewCommandPrompterFactory = (sp) =>
{
var interactionService = sp.GetRequiredService<IInteractionService>();
return new TestNewCommandPrompter(interactionService);
};

options.DotNetCliRunnerFactory = (sp) =>
{
var runner = new TestDotNetCliRunner();
runner.SearchPackagesAsyncCallback = (dir, query, prerelease, take, skip, nugetSource, options, cancellationToken) =>
{
var package = new NuGetPackage()
{
Id = "Aspire.ProjectTemplates",
Source = "nuget",
Version = "9.2.0"
};

return (
0, // Exit code.
new NuGetPackage[] { package } // Single package.
);
};

runner.NewProjectAsyncCallback = (templateName, name, outputPath, framework, options, cancellationToken) =>
{
Assert.Equal("net10.0", framework); // Check that the specified framework is used.
return 0; // Success
};

return runner;
};
});
var provider = services.BuildServiceProvider();

var command = provider.GetRequiredService<RootCommand>();
var result = command.Parse("new aspire-starter --use-redis-cache --test-framework None --framework net10.0");

var exitCode = await result.InvokeAsync().WaitAsync(CliTestConstants.DefaultTimeout);
Assert.Equal(0, exitCode);
}

private sealed class ThrowingCertificateService : ICertificateService
{
public Task EnsureCertificatesTrustedAsync(IDotNetCliRunner runner, CancellationToken cancellationToken)
Expand Down
6 changes: 3 additions & 3 deletions tests/Aspire.Cli.Tests/TestServices/TestDotNetCliRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class TestDotNetCliRunner : IDotNetCliRunner
public Func<FileInfo, DotNetCliRunnerInvocationOptions, CancellationToken, (int ExitCode, bool IsAspireHost, string? AspireHostingVersion)>? GetAppHostInformationAsyncCallback { get; set; }
public Func<FileInfo, string[], string[], DotNetCliRunnerInvocationOptions, CancellationToken, (int ExitCode, JsonDocument? Output)>? GetProjectItemsAndPropertiesAsyncCallback { get; set; }
public Func<string, string, string?, bool, DotNetCliRunnerInvocationOptions, CancellationToken, (int ExitCode, string? TemplateVersion)>? InstallTemplateAsyncCallback { get; set; }
public Func<string, string, string, DotNetCliRunnerInvocationOptions, CancellationToken, int>? NewProjectAsyncCallback { get; set; }
public Func<string, string, string, string?, DotNetCliRunnerInvocationOptions, CancellationToken, int>? NewProjectAsyncCallback { get; set; }
public Func<FileInfo, bool, bool, string[], IDictionary<string, string>?, TaskCompletionSource<IAppHostBackchannel>?, DotNetCliRunnerInvocationOptions, CancellationToken, Task<int>>? RunAsyncCallback { get; set; }
public Func<DirectoryInfo, string, bool, int, int, string?, DotNetCliRunnerInvocationOptions, CancellationToken, (int ExitCode, NuGetPackage[]? Packages)>? SearchPackagesAsyncCallback { get; set; }
public Func<DotNetCliRunnerInvocationOptions, CancellationToken, int>? TrustHttpCertificateAsyncCallback { get; set; }
Expand Down Expand Up @@ -64,10 +64,10 @@ public Task<int> CheckHttpCertificateAsync(DotNetCliRunnerInvocationOptions opti
: Task.FromResult<(int, string?)>((0, version)); // If not overridden, just return success for the version specified.
}

public Task<int> NewProjectAsync(string templateName, string name, string outputPath, string[] extraArgs, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken)
public Task<int> NewProjectAsync(string templateName, string name, string outputPath, string? framework, string[] extraArgs, DotNetCliRunnerInvocationOptions options, CancellationToken cancellationToken)
{
return NewProjectAsyncCallback != null
? Task.FromResult(NewProjectAsyncCallback(templateName, name, outputPath, options, cancellationToken))
? Task.FromResult(NewProjectAsyncCallback(templateName, name, outputPath, framework, options, cancellationToken))
: Task.FromResult(0); // If not overridden, just return success.
}

Expand Down
Loading