44using System . CommandLine ;
55using System . Diagnostics ;
66using Aspire . Cli . Utils ;
7- using Semver ;
87using Spectre . Console ;
98
109namespace Aspire . Cli . Commands ;
@@ -43,6 +42,10 @@ public NewCommand(DotNetCliRunner runner, INuGetPackageCache nuGetPackageCache)
4342 var templateVersionOption = new Option < string ? > ( "--version" , "-v" ) ;
4443 templateVersionOption . Description = "The version of the project templates to use." ;
4544 Options . Add ( templateVersionOption ) ;
45+
46+ var prereleaseOption = new Option < bool > ( "--prerelease" ) ;
47+ prereleaseOption . Description = "Include prerelease versions when searching for project templates." ;
48+ Options . Add ( prereleaseOption ) ;
4649 }
4750
4851 private static async Task < ( string TemplateName , string TemplateDescription , string ? PathAppendage ) > GetProjectTemplateAsync ( ParseResult parseResult , CancellationToken cancellationToken )
@@ -106,28 +109,23 @@ private static async Task<string> GetOutputPathAsync(ParseResult parseResult, st
106109 return Path . GetFullPath ( outputPath ) ;
107110 }
108111
109- private static async Task < string > GetProjectTemplatesVersionAsync ( ParseResult parseResult , CancellationToken cancellationToken )
112+ private async Task < string > GetProjectTemplatesVersionAsync ( ParseResult parseResult , bool prerelease , string ? source , CancellationToken cancellationToken )
110113 {
111114 if ( parseResult . GetValue < string > ( "--version" ) is { } version )
112115 {
113116 return version ;
114117 }
115118 else
116119 {
117- version = await InteractionUtils . PromptForStringAsync (
118- "Project templates version:" ,
119- defaultValue : VersionHelper . GetDefaultTemplateVersion ( ) ,
120- validator : ( string value ) => {
121- if ( SemVersion . TryParse ( value , out var parsedVersion ) )
122- {
123- return ValidationResult . Success ( ) ;
124- }
125-
126- return ValidationResult . Error ( "Invalid version format. Please enter a valid version." ) ;
127- } ,
128- cancellationToken ) ;
120+ var workingDirectory = new DirectoryInfo ( Environment . CurrentDirectory ) ;
129121
130- return version ;
122+ var candidatePackages = await InteractionUtils . ShowStatusAsync (
123+ "Searching for available project template versions..." ,
124+ ( ) => _nuGetPackageCache . GetTemplatePackagesAsync ( workingDirectory , prerelease , source , cancellationToken )
125+ ) ;
126+
127+ var selectedPackage = await InteractionUtils . PromptForTemplatesVersionAsync ( candidatePackages , cancellationToken ) ;
128+ return selectedPackage . Version ;
131129 }
132130 }
133131
@@ -138,8 +136,9 @@ protected override async Task<int> ExecuteAsync(ParseResult parseResult, Cancell
138136 var template = await GetProjectTemplateAsync ( parseResult , cancellationToken ) ;
139137 var name = await GetProjectNameAsync ( parseResult , cancellationToken ) ;
140138 var outputPath = await GetOutputPathAsync ( parseResult , template . PathAppendage , cancellationToken ) ;
141- var version = await GetProjectTemplatesVersionAsync ( parseResult , cancellationToken ) ;
139+ var prerelease = parseResult . GetValue < bool > ( "--prerelease" ) ;
142140 var source = parseResult . GetValue < string ? > ( "--source" ) ;
141+ var version = await GetProjectTemplatesVersionAsync ( parseResult , prerelease , source , cancellationToken ) ;
143142
144143 var templateInstallResult = await AnsiConsole . Status ( )
145144 . Spinner ( Spinner . Known . Dots3 )
0 commit comments