Skip to content

Commit 3b1cc5a

Browse files
authored
Improve path prompt for aspire new command. (#8542)
* Improve path prompt for aspire new command. * Turn project option into argument on aspire run. * Fix up message.
1 parent f9c19c1 commit 3b1cc5a

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

src/Aspire.Cli/Commands/NewCommand.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ internal sealed class NewCommand : BaseCommand
4949
// interrogate the various options and add them. For now we will
5050
// keep it simple.
5151
(string TemplateName, string TemplateDescription, string? PathAppendage)[] validTemplates = [
52-
("aspire-starter", "Aspire Starter App", "src") ,
53-
("aspire", "Aspire Empty App", "src"),
54-
("aspire-apphost", "Aspire App Host", null),
55-
("aspire-servicedefaults", "Aspire Service Defaults", null),
56-
("aspire-mstest", "Aspire Test Project (MSTest)", null),
57-
("aspire-nunit", "Aspire Test Project (NUnit)", null),
58-
("aspire-xunit", "Aspire Test Project (xUnit)", null)
52+
("aspire-starter", "Aspire Starter App", "./src") ,
53+
("aspire", "Aspire Empty App", "./src"),
54+
("aspire-apphost", "Aspire App Host", "./"),
55+
("aspire-servicedefaults", "Aspire Service Defaults", "./"),
56+
("aspire-mstest", "Aspire Test Project (MSTest)", "./"),
57+
("aspire-nunit", "Aspire Test Project (NUnit)", "./"),
58+
("aspire-xunit", "Aspire Test Project (xUnit)", "./")
5959
];
6060

6161
if (parseResult.GetValue<string?>("template") is { } templateName && validTemplates.SingleOrDefault(t => t.TemplateName == templateName) is { } template)
@@ -92,7 +92,7 @@ private static async Task<string> GetOutputPathAsync(ParseResult parseResult, st
9292
{
9393
outputPath = await PromptUtils.PromptForStringAsync(
9494
"Enter the output path:",
95-
defaultValue: Path.Combine(Environment.CurrentDirectory, pathAppendage ?? string.Empty),
95+
defaultValue: pathAppendage ?? ".",
9696
cancellationToken: cancellationToken
9797
);
9898
}

src/Aspire.Cli/Commands/RunCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public RunCommand(DotNetCliRunner runner) : base("run", "Run an Aspire app host
2323

2424
_runner = runner;
2525

26-
var projectOption = new Option<FileInfo?>("--project");
27-
projectOption.Validators.Add(ProjectFileHelper.ValidateProjectOption);
28-
Options.Add(projectOption);
26+
var projectArgument = new Argument<FileInfo?>("project");
27+
projectArgument.Validators.Add(ProjectFileHelper.ValidateProjectArgument);
28+
Arguments.Add(projectArgument);
2929

3030
var watchOption = new Option<bool>("--watch", "-w");
3131
Options.Add(watchOption);
@@ -35,7 +35,7 @@ protected override async Task<int> ExecuteAsync(ParseResult parseResult, Cancell
3535
{
3636
using var activity = _activitySource.StartActivity();
3737

38-
var passedAppHostProjectFile = parseResult.GetValue<FileInfo?>("--project");
38+
var passedAppHostProjectFile = parseResult.GetValue<FileInfo?>("project");
3939
var effectiveAppHostProjectFile = ProjectFileHelper.UseOrFindAppHostProjectFile(passedAppHostProjectFile);
4040

4141
if (effectiveAppHostProjectFile is null)

src/Aspire.Cli/DotNetCliRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal sealed class DotNetCliRunner(ILogger<DotNetCliRunner> logger, IServiceP
2323
{
2424
using var activity = _activitySource.StartActivity();
2525

26-
string[] cliArgs = ["msbuild", "-getproperty:IsAspireHost,AspireHostingSDKVersion"];
26+
string[] cliArgs = ["msbuild", "-getproperty:IsAspireHost,AspireHostingSDKVersion", projectFile.FullName];
2727

2828
string? stdout = null;
2929
string? stderr = null;

src/Aspire.Cli/Utils/ProjectFileHelper.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,38 @@ internal static class ProjectFileHelper
4747
};
4848
}
4949

50+
internal static void ValidateProjectArgument(ArgumentResult result)
51+
{
52+
var value = result.GetValueOrDefault<FileInfo?>();
53+
54+
if (value is null)
55+
{
56+
// Having no value here is fine, but there has to
57+
// be a single csproj file in the current
58+
// working directory.
59+
var csprojFiles = Directory.GetFiles(Environment.CurrentDirectory, "*.csproj");
60+
61+
if (csprojFiles.Length > 1)
62+
{
63+
result.AddError("The project argument was not specified and multiple *.csproj files were detected.");
64+
return;
65+
}
66+
else if (csprojFiles.Length == 0)
67+
{
68+
result.AddError("The project argument was not specified and no *.csproj files were detected.");
69+
return;
70+
}
71+
72+
return;
73+
}
74+
75+
if (!File.Exists(value.FullName))
76+
{
77+
result.AddError("The specified project file does not exist.");
78+
return;
79+
}
80+
}
81+
5082
internal static void ValidateProjectOption(OptionResult result)
5183
{
5284
var value = result.GetValueOrDefault<FileInfo?>();

0 commit comments

Comments
 (0)