Skip to content

Commit 679f4d5

Browse files
authored
Move New command definitions to CommandDefinitions project (#52526)
1 parent a2bbad7 commit 679f4d5

File tree

169 files changed

+5433
-5196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+5433
-5196
lines changed

src/Cli/Microsoft.DotNet.Cli.Definitions/CommandDefinitionStrings.resx

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,4 +1330,156 @@ To display a value, specify the corresponding command-line option without provid
13301330
<data name="Option_Output" xml:space="preserve">
13311331
<value>Location to place the generated output.</value>
13321332
</data>
1333-
</root>
1333+
<data name="Command_AliasAdd_Description" xml:space="preserve">
1334+
<value>Creates an alias for instantiate command with a certain set of arguments.</value>
1335+
</data>
1336+
<data name="Command_AliasShow_Description" xml:space="preserve">
1337+
<value>Displays defined aliases.</value>
1338+
</data>
1339+
<data name="Command_Alias_Description" xml:space="preserve">
1340+
<value>Creates or displays defined aliases.</value>
1341+
</data>
1342+
<data name="Command_Details_Description" xml:space="preserve">
1343+
<value>
1344+
Provides the details for specified template package.
1345+
The command checks if the package is installed locally, if it was not found, it searches the configured NuGet feeds.</value>
1346+
</data>
1347+
<data name="Command_Install_Argument_Package" xml:space="preserve">
1348+
<value>NuGet package ID or path to folder or NuGet package to install.
1349+
To install the NuGet package of certain version, use &lt;package ID&gt;::&lt;version&gt;.
1350+
</value>
1351+
</data>
1352+
<data name="Command_Install_Description" xml:space="preserve">
1353+
<value>Installs a template package.</value>
1354+
</data>
1355+
<data name="Command_Instantiate_Argument_ShortName" xml:space="preserve">
1356+
<value>A short name of the template to create.</value>
1357+
</data>
1358+
<data name="Command_Instantiate_Argument_TemplateOptions" xml:space="preserve">
1359+
<value>Template specific options to use.</value>
1360+
</data>
1361+
<data name="Command_Instantiate_Description" xml:space="preserve">
1362+
<value>Instantiates a template with given short name. An alias of 'dotnet new &lt;template name&gt;'.</value>
1363+
<comment>do not translate 'dotnet new &lt;template name&gt;'</comment>
1364+
</data>
1365+
<data name="Command_Legacy_Update_Check_Description" xml:space="preserve">
1366+
<value>Checks the currently installed template packages for updates.</value>
1367+
</data>
1368+
<data name="Command_List_Argument_Name" xml:space="preserve">
1369+
<value>If specified, only the templates matching the name will be shown.</value>
1370+
</data>
1371+
<data name="Command_List_Description" xml:space="preserve">
1372+
<value>Lists templates containing the specified template name. If no name is specified, lists all templates.</value>
1373+
</data>
1374+
<data name="Command_New_Description" xml:space="preserve">
1375+
<value>Template Instantiation Commands for .NET CLI.</value>
1376+
</data>
1377+
<data name="Command_Search_Argument_Name" xml:space="preserve">
1378+
<value>If specified, only the templates matching the name will be shown.</value>
1379+
</data>
1380+
<data name="Command_Search_Description" xml:space="preserve">
1381+
<value>Searches for the templates on NuGet.org.</value>
1382+
</data>
1383+
<data name="Command_Uninstall_Argument_Package" xml:space="preserve">
1384+
<value>NuGet package ID (without version) or path to folder to uninstall.
1385+
If command is specified without the argument, it lists all the template packages installed.</value>
1386+
</data>
1387+
<data name="Command_Uninstall_Description" xml:space="preserve">
1388+
<value>Uninstalls a template package.</value>
1389+
</data>
1390+
<data name="Command_Update_Description" xml:space="preserve">
1391+
<value>Checks the currently installed template packages for update, and install the updates.</value>
1392+
</data>
1393+
<data name="Command_Update_Option_CheckOnly" xml:space="preserve">
1394+
<value>Only checks for updates and display the template packages to be updated without applying update.</value>
1395+
</data>
1396+
<data name="ListCommand_Option_IgnoreConstraints" xml:space="preserve">
1397+
<value>Disables checking if the template meets the constraints to be run.</value>
1398+
</data>
1399+
<data name="Option_AddSource" xml:space="preserve">
1400+
<value>Specifies a NuGet source to use.</value>
1401+
</data>
1402+
<data name="Option_AuthorFilter" xml:space="preserve">
1403+
<value>Filters the templates based on the template author.</value>
1404+
</data>
1405+
<data name="Option_BaselineFilter" xml:space="preserve">
1406+
<value>Filters the templates based on baseline defined in the template.</value>
1407+
</data>
1408+
<data name="Option_Columns" xml:space="preserve">
1409+
<value>Specifies the columns to display in the output. </value>
1410+
</data>
1411+
<data name="Option_ColumnsAll" xml:space="preserve">
1412+
<value>Displays all columns in the output.</value>
1413+
</data>
1414+
<data name="Option_Debug_Attach" xml:space="preserve">
1415+
<value>Allows to pause execution in order to attach to the process for debug purposes.</value>
1416+
</data>
1417+
<data name="Option_Debug_CustomSettings" xml:space="preserve">
1418+
<value>Sets custom settings location.</value>
1419+
</data>
1420+
<data name="Option_Debug_RebuildCache" xml:space="preserve">
1421+
<value>If specified, removes the template cache prior to command execution.</value>
1422+
</data>
1423+
<data name="Option_Debug_Reinit" xml:space="preserve">
1424+
<value>If specified, resets the settings prior to command execution.</value>
1425+
</data>
1426+
<data name="Option_Debug_ShowConfig" xml:space="preserve">
1427+
<value>If specified, shows the template engine config prior to command execution.</value>
1428+
</data>
1429+
<data name="Option_Debug_VirtualSettings" xml:space="preserve">
1430+
<value>If specified, the settings will be not preserved on file system.</value>
1431+
</data>
1432+
<data name="Option_Install_Force" xml:space="preserve">
1433+
<value>Allows installing template packages from the specified sources even if they would override a template package from another source.</value>
1434+
</data>
1435+
<data name="Option_Interactive" xml:space="preserve">
1436+
<value>Allows the command to stop and wait for user input or action (for example to complete authentication).</value>
1437+
</data>
1438+
<data name="Option_LanguageFilter" xml:space="preserve">
1439+
<value>Filters templates based on language.</value>
1440+
</data>
1441+
<data name="Option_PackageFilter" xml:space="preserve">
1442+
<value>Filters the templates based on NuGet package ID.</value>
1443+
</data>
1444+
<data name="Option_ProjectPath" xml:space="preserve">
1445+
<value>The project that should be used for context evaluation.</value>
1446+
</data>
1447+
<data name="Option_TagFilter" xml:space="preserve">
1448+
<value>Filters the templates based on the tag.</value>
1449+
</data>
1450+
<data name="Option_TypeFilter" xml:space="preserve">
1451+
<value>Filters templates based on available types. Predefined values are "project" and "item".</value>
1452+
<comment>project and item should not be translated</comment>
1453+
</data>
1454+
<data name="TemplateCommand_Option_DryRun" xml:space="preserve">
1455+
<value>Displays a summary of what would happen if the given command line were run if it would result in a template creation.</value>
1456+
</data>
1457+
<data name="TemplateCommand_Option_Force" xml:space="preserve">
1458+
<value>Forces content to be generated even if it would change existing files.</value>
1459+
</data>
1460+
<data name="TemplateCommand_Option_Name" xml:space="preserve">
1461+
<value>The name for the output being created. If no name is specified, the name of the output directory is used.</value>
1462+
</data>
1463+
<data name="TemplateCommand_Option_NoUpdateCheck" xml:space="preserve">
1464+
<value>Disables checking for the template package updates when instantiating a template.</value>
1465+
</data>
1466+
<data name="Commands_Validator_WrongTokens" xml:space="preserve">
1467+
<value>Unrecognized command or argument(s): {0}.</value>
1468+
<comment>{0} - wrong token or comma-separated tokens (if multiple). Each token is enclosed with single quotes: 'token'.</comment>
1469+
</data>
1470+
<data name="DetailsCommand_Argument_PackageIdentifier" xml:space="preserve">
1471+
<value>Package identifier</value>
1472+
</data>
1473+
<data name="DisableSdkTemplates_OptionDescription" xml:space="preserve">
1474+
<value>If present, prevents templates bundled in the SDK from being presented.</value>
1475+
</data>
1476+
<data name="DisableProjectContextEval_OptionDescription" xml:space="preserve">
1477+
<value>Disables evaluating project context using MSBuild.</value>
1478+
</data>
1479+
<data name="Verbosity_OptionDescription" xml:space="preserve">
1480+
<value>Sets the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], and diag[nostic].</value>
1481+
</data>
1482+
<data name="Diagnostics_OptionDescription" xml:space="preserve">
1483+
<value>Enables diagnostic output.</value>
1484+
</data>
1485+
</root>

src/Cli/Microsoft.TemplateEngine.Cli/CommandDefinitions/CommandDefinitionExtensions.cs renamed to src/Cli/Microsoft.DotNet.Cli.Definitions/Commands/New/CommandDefinitionExtensions.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,70 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#pragma warning disable SA1202 // Elements should be ordered by access
5-
#pragma warning disable SA1308 // Variable names should not be prefixed
6-
#pragma warning disable SA1311 // Static readonly fields should begin with upper-case letter
7-
#pragma warning disable CA1810 // Initialize reference type static fields inline
8-
#pragma warning disable SA1010 // Opening square brackets should be spaced correctly
9-
104
using System.CommandLine;
115
using System.CommandLine.Parsing;
126

13-
namespace Microsoft.TemplateEngine.Cli;
7+
namespace Microsoft.DotNet.Cli.Commands.New;
148

159
internal static class CommandDefinitionExtensions
1610
{
17-
public static TDefinition AddShortNameArgumentValidator<TDefinition>(this TDefinition command, Argument nameArgument)
18-
where TDefinition : CommandDefinition
11+
public static TDefinition AddShortNameArgumentValidator<TDefinition>(this TDefinition definition, Argument nameArgument)
12+
where TDefinition : Command
1913
{
20-
command.Validators.Add(commandResult =>
14+
definition.Validators.Add(commandResult =>
2115
{
2216
var nameArgumentResult = commandResult.Children.FirstOrDefault(symbol => symbol is ArgumentResult argumentResult && argumentResult.Argument == nameArgument);
2317
if (nameArgumentResult == null)
2418
{
2519
return;
2620
}
2721

28-
ValidateArgumentUsage(commandResult, CommandDefinition.New.ShortNameArgument);
22+
ValidateArgumentUsage(commandResult, NewCommandDefinition.ShortNameArgumentName);
2923
});
3024

31-
return command;
25+
return definition;
3226
}
3327

34-
public static TDefinition AddNoLegacyUsageValidators<TDefinition>(this TDefinition command, params IEnumerable<Symbol> except)
35-
where TDefinition : CommandDefinition
28+
public static TDefinition AddNoLegacyUsageValidators<TDefinition>(this TDefinition command, params IEnumerable<string> except)
29+
where TDefinition : Command
3630
{
37-
foreach (var option in CommandDefinition.New.LegacyOptions)
31+
foreach (var optionName in LegacyOptions.AllNames)
3832
{
39-
if (!except.Contains(option))
33+
if (!except.Contains(optionName))
4034
{
41-
command.Validators.Add(symbolResult => symbolResult.ValidateOptionUsage(option));
35+
command.Validators.Add(symbolResult => symbolResult.ValidateOptionUsage(optionName));
4236
}
4337
}
4438

45-
foreach (var argument in new Argument[] { CommandDefinition.New.ShortNameArgument, CommandDefinition.New.RemainingArguments })
39+
foreach (var argumentName in new[] { NewCommandDefinition.ShortNameArgumentName, NewCommandDefinition.RemainingArgumentsName })
4640
{
47-
if (!except.Contains(argument))
41+
if (!except.Contains(argumentName))
4842
{
49-
command.Validators.Add(symbolResult => symbolResult.ValidateArgumentUsage(argument));
43+
command.Validators.Add(symbolResult => symbolResult.ValidateArgumentUsage(argumentName));
5044
}
5145
}
5246

5347
return command;
5448
}
5549

5650
public static TDefinition AddOptions<TDefinition>(this TDefinition command, IEnumerable<Option> options)
57-
where TDefinition : CommandDefinition
51+
where TDefinition : Command
5852
{
5953
command.Options.AddRange(options);
6054
return command;
6155
}
6256

63-
internal static void ValidateArgumentUsage(this CommandResult commandResult, params Argument[] arguments)
57+
internal static void ValidateArgumentUsage(this CommandResult commandResult, params string[] argumentNames)
6458
{
6559
if (commandResult.Parent is not CommandResult parentResult)
6660
{
6761
return;
6862
}
6963

7064
List<string> wrongTokens = new();
71-
foreach (Argument argument in arguments)
65+
foreach (var argumentName in argumentNames)
7266
{
73-
var newCommandArgument = parentResult.Children.OfType<ArgumentResult>().FirstOrDefault(result => result.Argument == argument);
67+
var newCommandArgument = parentResult.Children.OfType<ArgumentResult>().FirstOrDefault(result => result.Argument.Name == argumentName);
7468
if (newCommandArgument == null)
7569
{
7670
continue;
@@ -86,18 +80,18 @@ internal static void ValidateArgumentUsage(this CommandResult commandResult, par
8680
if (wrongTokens.Any())
8781
{
8882
//Unrecognized command or argument(s): {0}
89-
commandResult.AddError(string.Format(LocalizableStrings.Commands_Validator_WrongTokens, string.Join(",", wrongTokens)));
83+
commandResult.AddError(string.Format(CommandDefinitionStrings.Commands_Validator_WrongTokens, string.Join(",", wrongTokens)));
9084
}
9185
}
9286

93-
internal static void ValidateOptionUsage(this CommandResult commandResult, Option option)
87+
internal static void ValidateOptionUsage(this CommandResult commandResult, string optionName)
9488
{
9589
if (commandResult.Parent is not CommandResult parentResult)
9690
{
9791
return;
9892
}
9993

100-
OptionResult? optionResult = parentResult.Children.OfType<OptionResult>().FirstOrDefault(result => result.Option == option);
94+
OptionResult? optionResult = parentResult.Children.OfType<OptionResult>().FirstOrDefault(result => result.Option.Name == optionName);
10195
if (optionResult != null)
10296
{
10397
List<string> wrongTokens = new();
@@ -113,7 +107,7 @@ internal static void ValidateOptionUsage(this CommandResult commandResult, Optio
113107
}
114108
}
115109
//Unrecognized command or argument(s): {0}
116-
commandResult.AddError(string.Format(LocalizableStrings.Commands_Validator_WrongTokens, string.Join(",", wrongTokens)));
110+
commandResult.AddError(string.Format(CommandDefinitionStrings.Commands_Validator_WrongTokens, string.Join(",", wrongTokens)));
117111
}
118112
}
119113
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.CommandLine;
5+
6+
namespace Microsoft.DotNet.Cli.Commands.New;
7+
8+
public sealed class NewCreateCommandDefinition : Command
9+
{
10+
public new const string Name = "create";
11+
12+
public readonly Argument<string> ShortNameArgument = CreateShortNameArgument();
13+
14+
public readonly Argument<string[]> RemainingArguments = new("template-args")
15+
{
16+
Description = CommandDefinitionStrings.Command_Instantiate_Argument_TemplateOptions,
17+
Arity = new ArgumentArity(0, 999)
18+
};
19+
20+
public readonly InstantiateOptions InstantiateOptions = new();
21+
22+
public NewCreateCommandDefinition()
23+
: base(Name, CommandDefinitionStrings.Command_Instantiate_Description)
24+
{
25+
Arguments.Add(ShortNameArgument);
26+
Arguments.Add(RemainingArguments);
27+
28+
Options.AddRange(InstantiateOptions.AllOptions);
29+
30+
foreach (var option in InstantiateOptions.AllOptions)
31+
{
32+
Validators.Add(symbolResult => symbolResult.ValidateOptionUsage(option.Name));
33+
}
34+
35+
this.AddNoLegacyUsageValidators();
36+
}
37+
38+
public static Argument<string> CreateShortNameArgument() => new("template-short-name")
39+
{
40+
Description = CommandDefinitionStrings.Command_Instantiate_Argument_ShortName,
41+
Arity = new ArgumentArity(0, 1)
42+
};
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.CommandLine;
5+
6+
namespace Microsoft.DotNet.Cli.Commands.New;
7+
8+
public sealed class InstantiateOptions
9+
{
10+
public readonly Option<FileInfo> OutputOption = SharedOptionsFactory.CreateOutputOption();
11+
public readonly Option<string> NameOption = SharedOptionsFactory.CreateNameOption();
12+
public readonly Option<bool> DryRunOption = SharedOptionsFactory.CreateDryRunOption();
13+
public readonly Option<bool> ForceOption = SharedOptionsFactory.CreateForceOption();
14+
public readonly Option<bool> NoUpdateCheckOption = SharedOptionsFactory.CreateNoUpdateCheckOption();
15+
public readonly Option<FileInfo> ProjectOption = SharedOptionsFactory.CreateProjectOption();
16+
17+
public IEnumerable<Option> AllOptions
18+
{
19+
get
20+
{
21+
yield return OutputOption;
22+
yield return NameOption;
23+
yield return DryRunOption;
24+
yield return ForceOption;
25+
yield return NoUpdateCheckOption;
26+
yield return ProjectOption;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)