Skip to content

Commit

Permalink
extra code review fixes for feedback from earlier `dotnet nuget add s…
Browse files Browse the repository at this point in the history
…ource` commit (#3244)

Fixes: NuGet/Home#9143
As part of #3206, there was feedback that came that was good to consider, but not before commiting to 5.5 codebase.
  • Loading branch information
Rob Relyea authored Feb 20, 2020
1 parent 8b4990f commit 8cb7886
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 288 deletions.
1 change: 0 additions & 1 deletion build/vsts_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ jobs:
VsTargetChannel: $[dependencies.Initialize_Build.outputs['updatebuildnumber.VsTargetChannel']]
VsTargetMajorVersion: $[dependencies.Initialize_Build.outputs['updatebuildnumber.VsTargetMajorVersion']]
LocalizedLanguageCount: "13"
DesktopTargetFramework: "net472"

pool:
name: VSEng-MicroBuildVS2019
Expand Down
159 changes: 13 additions & 146 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/CommandParsers.tt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<#
string InitCaps(string input)
{
string output = input.Substring(0,1).ToUpper() + input.Substring(1);
string output = input.Substring(0, 1).ToUpper() + input.Substring(1);
return output;
}

Expand All @@ -20,7 +20,7 @@

string GetProperty(XElement element)
{
switch (element.Name.LocalName)
switch (element.Name.LocalName)
{
case "SingleValueOption":
return "Option";
Expand All @@ -29,7 +29,7 @@
case "Argument":
return "Argument";
default:
return "Unknown Element Type" + element.Name.LocalName;
return "Unknown Element Type " + element.Name.LocalName;
}
}

Expand All @@ -44,12 +44,12 @@
case "Value":
return "Value";
default:
return "Unknown Element Type" + element.Name.LocalName;
return "Unknown Element Type " + element.Name.LocalName;
}
}

var commandFile = this.Host.ResolvePath("Commands.xml");
var commands = XDocument.Load(commandFile);
string commandFile = this.Host.ResolvePath("Commands.xml");
XDocument commands = XDocument.Load(commandFile);
#>
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Expand All @@ -69,158 +69,25 @@ namespace NuGet.CommandLine.XPlat
public static void Register(CommandLineApplication app, Func<ILogger> getLogger)
{
<#
foreach (var command in commands.Descendants(XName.Get("Verb","")))
foreach (XElement command in commands.Descendants(XName.Get("Verb","")))
{
var verbName = command.Attribute(XName.Get("Name", "")).Value;
var verbFormalName = InitCaps(verbName);
string verbName = command.Attribute(XName.Get("Name", "")).Value;
string verbFormalName = InitCaps(verbName);
#>
<#= verbFormalName #>VerbParser.Register(app, getLogger);
<#
}
#>
<#
foreach (var command in commands.Descendants(XName.Get("Command","")))
foreach (XElement command in commands.Descendants(XName.Get("Command","")))
{
var commandName = command.Attribute(XName.Get("Name", "")).Value;
var commandFormalName = InitCaps(commandName);
string commandName = command.Attribute(XName.Get("Name", "")).Value;
string commandFormalName = InitCaps(commandName);
#>
<#= commandFormalName #>CommandParser.Register(app, getLogger);
<#
}
#> }
}
<#
foreach (var command in commands.Descendants(XName.Get("Command","")))
{
var commandName = command.Attribute(XName.Get("Name", "")).Value;
var commandFormalName = InitCaps(commandName);
#>

internal partial class <#= commandFormalName #>CommandParser
{
internal static void Register(CommandLineApplication app, Func<ILogger> getLogger)
{
app.Command("<#= commandName #>", <#= commandFormalName #>Cmd =>
{
<#
foreach (var option in command.Descendants()) {
var optionName = option.Attribute(XName.Get("Name", "")).Value;
var optionHelp = option.Attribute(XName.Get("Help", ""))?.Value;
var optionFormalName = optionName.Replace("-","");
var optionCapsName = InitCaps(optionName);
if (IsArgument(GetProperty(option)))
{
#>
var <#= optionFormalName #> = <#= commandFormalName #>Cmd.Argument(
"<#= optionName #>", "<#= optionHelp != null ? optionHelp : "" #>");
<#
}
else
{
var optionShortcut = option.Attribute(XName.Get("Shortcut",""))?.Value;
#>
var <#= optionFormalName #> = <#= commandFormalName #>Cmd.Option(
"<#= (optionShortcut != null ? "-" + optionShortcut + "|" : "") + "--" + optionName #>",
Strings.Source_Description,
CommandOptionType.<#= GetOptionType(option) #>);
<#
}
}
#>
<#= commandFormalName #>Cmd.HelpOption("-h|--help");

<#= commandFormalName #>Cmd.OnExecute(() =>
{
var args = new <#= commandFormalName #>FArgs()
{
<#
foreach (var option in command.Descendants()) {
var optionName = option.Attribute(XName.Get("Name", "")).Value;
var optionFormalName = optionName.Replace("-","");
var optionCapsName = InitCaps(optionFormalName);
if (IsArgument(GetProperty(option)))
{
#>
<#= optionCapsName #> = <#=optionFormalName#>.Value,
<#
}
else
{
var optionType = GetOptionType(option);
switch (optionType)
{
case "SingleValue":
#>
<#= optionCapsName #> = <#= optionFormalName #>.Value(),
<#
break;
case "NoValue":
#>
<#= optionCapsName #> = <#= optionFormalName #>.HasValue(),
<#
break;
}

}
}

#>
};


<# // ****** IMPLEMENT REQUIRED ARGUMENTS ********


foreach (var option in command.Descendants()) {
var optionName = option.Attribute(XName.Get("Name", "")).Value;
var required = option.Attribute(XName.Get("Required", ""))?.Value == "true";
var optionFormalName = optionName.Replace("-","");
var optionCapsName = InitCaps(optionFormalName);

if (required)
{
if (IsArgument(GetProperty(option)))
{
#>
if (args.<#= optionCapsName #> == null)
{
throw new CommandException("'<#=optionFormalName#>' argument is missing but required.");
}
<#
}
else
{
var optionType = GetOptionType(option);
switch (optionType)
{
case "SingleValue":
#>
if (args.<#= optionCapsName #> == null)
{
throw new CommandException("'<#=optionFormalName#>' option is missing but required.");
}
<#
break;
case "NoValue":
} // End of CommandParser class
#>
//TODO: implement required for bool
<#
break;
}

}
}
}
// ****** END - IMPLEMENT REQUIRED ARGUMENTS ********
#>

<#= commandFormalName #>FRunner.Run(args, getLogger);
return 0;
});
});
}
}
<#
} // End of CommandParser class
#>
}
56 changes: 28 additions & 28 deletions src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Verbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ internal static void Register(CommandLineApplication app,
{
AddCmd.Command("source", SourceCmd =>
{
var Source = SourceCmd.Argument(
CommandArgument Source = SourceCmd.Argument(
"PackageSourcePath", Strings.SourcesCommandSourceDescription);
var name = SourceCmd.Option(
CommandOption name = SourceCmd.Option(
"-n|--name",
Strings.SourcesCommandNameDescription,
CommandOptionType.SingleValue);
var username = SourceCmd.Option(
CommandOption username = SourceCmd.Option(
"-u|--username",
Strings.SourcesCommandUserNameDescription,
CommandOptionType.SingleValue);
var password = SourceCmd.Option(
CommandOption password = SourceCmd.Option(
"-p|--password",
Strings.SourcesCommandPasswordDescription,
CommandOptionType.SingleValue);
var storePasswordInClearText = SourceCmd.Option(
CommandOption storePasswordInClearText = SourceCmd.Option(
"--store-password-in-clear-text",
Strings.SourcesCommandStorePasswordInClearTextDescription,
CommandOptionType.NoValue);
var validAuthenticationTypes = SourceCmd.Option(
CommandOption validAuthenticationTypes = SourceCmd.Option(
"--valid-authentication-types",
Strings.SourcesCommandValidAuthenticationTypesDescription,
CommandOptionType.SingleValue);
var configfile = SourceCmd.Option(
CommandOption configfile = SourceCmd.Option(
"--configfile",
Strings.Option_ConfigFile,
CommandOptionType.SingleValue);
Expand All @@ -68,7 +68,7 @@ internal static void Register(CommandLineApplication app,
});
AddCmd.HelpOption("-h|--help");
AddCmd.Description = Strings.Add_Description;
AddCmd.OnExecute(() =>
AddCmd.OnExecute(() =>
{
app.ShowHelp("add");
return 0;
Expand All @@ -86,9 +86,9 @@ internal static void Register(CommandLineApplication app,
{
DisableCmd.Command("source", SourceCmd =>
{
var name = SourceCmd.Argument(
CommandArgument name = SourceCmd.Argument(
"name", Strings.SourcesCommandNameDescription);
var configfile = SourceCmd.Option(
CommandOption configfile = SourceCmd.Option(
"--configfile",
Strings.Option_ConfigFile,
CommandOptionType.SingleValue);
Expand All @@ -108,7 +108,7 @@ internal static void Register(CommandLineApplication app,
});
DisableCmd.HelpOption("-h|--help");
DisableCmd.Description = Strings.Disable_Description;
DisableCmd.OnExecute(() =>
DisableCmd.OnExecute(() =>
{
app.ShowHelp("disable");
return 0;
Expand All @@ -126,9 +126,9 @@ internal static void Register(CommandLineApplication app,
{
EnableCmd.Command("source", SourceCmd =>
{
var name = SourceCmd.Argument(
CommandArgument name = SourceCmd.Argument(
"name", Strings.SourcesCommandNameDescription);
var configfile = SourceCmd.Option(
CommandOption configfile = SourceCmd.Option(
"--configfile",
Strings.Option_ConfigFile,
CommandOptionType.SingleValue);
Expand All @@ -148,7 +148,7 @@ internal static void Register(CommandLineApplication app,
});
EnableCmd.HelpOption("-h|--help");
EnableCmd.Description = Strings.Enable_Description;
EnableCmd.OnExecute(() =>
EnableCmd.OnExecute(() =>
{
app.ShowHelp("enable");
return 0;
Expand All @@ -166,11 +166,11 @@ internal static void Register(CommandLineApplication app,
{
ListCmd.Command("source", SourceCmd =>
{
var format = SourceCmd.Option(
CommandOption format = SourceCmd.Option(
"--format",
Strings.SourcesCommandFormatDescription,
CommandOptionType.SingleValue);
var configfile = SourceCmd.Option(
CommandOption configfile = SourceCmd.Option(
"--configfile",
Strings.Option_ConfigFile,
CommandOptionType.SingleValue);
Expand All @@ -190,7 +190,7 @@ internal static void Register(CommandLineApplication app,
});
ListCmd.HelpOption("-h|--help");
ListCmd.Description = Strings.List_Description;
ListCmd.OnExecute(() =>
ListCmd.OnExecute(() =>
{
app.ShowHelp("list");
return 0;
Expand All @@ -208,9 +208,9 @@ internal static void Register(CommandLineApplication app,
{
RemoveCmd.Command("source", SourceCmd =>
{
var name = SourceCmd.Argument(
CommandArgument name = SourceCmd.Argument(
"name", Strings.SourcesCommandNameDescription);
var configfile = SourceCmd.Option(
CommandOption configfile = SourceCmd.Option(
"--configfile",
Strings.Option_ConfigFile,
CommandOptionType.SingleValue);
Expand All @@ -230,7 +230,7 @@ internal static void Register(CommandLineApplication app,
});
RemoveCmd.HelpOption("-h|--help");
RemoveCmd.Description = Strings.Remove_Description;
RemoveCmd.OnExecute(() =>
RemoveCmd.OnExecute(() =>
{
app.ShowHelp("remove");
return 0;
Expand All @@ -248,29 +248,29 @@ internal static void Register(CommandLineApplication app,
{
UpdateCmd.Command("source", SourceCmd =>
{
var name = SourceCmd.Argument(
CommandArgument name = SourceCmd.Argument(
"name", Strings.SourcesCommandNameDescription);
var source = SourceCmd.Option(
CommandOption source = SourceCmd.Option(
"-s|--source",
Strings.SourcesCommandSourceDescription,
CommandOptionType.SingleValue);
var username = SourceCmd.Option(
CommandOption username = SourceCmd.Option(
"-u|--username",
Strings.SourcesCommandUserNameDescription,
CommandOptionType.SingleValue);
var password = SourceCmd.Option(
CommandOption password = SourceCmd.Option(
"-p|--password",
Strings.SourcesCommandPasswordDescription,
CommandOptionType.SingleValue);
var storePasswordInClearText = SourceCmd.Option(
CommandOption storePasswordInClearText = SourceCmd.Option(
"--store-password-in-clear-text",
Strings.SourcesCommandStorePasswordInClearTextDescription,
CommandOptionType.NoValue);
var validAuthenticationTypes = SourceCmd.Option(
CommandOption validAuthenticationTypes = SourceCmd.Option(
"--valid-authentication-types",
Strings.SourcesCommandValidAuthenticationTypesDescription,
CommandOptionType.SingleValue);
var configfile = SourceCmd.Option(
CommandOption configfile = SourceCmd.Option(
"--configfile",
Strings.Option_ConfigFile,
CommandOptionType.SingleValue);
Expand All @@ -295,7 +295,7 @@ internal static void Register(CommandLineApplication app,
});
UpdateCmd.HelpOption("-h|--help");
UpdateCmd.Description = Strings.Update_Description;
UpdateCmd.OnExecute(() =>
UpdateCmd.OnExecute(() =>
{
app.ShowHelp("update");
return 0;
Expand Down
Loading

0 comments on commit 8cb7886

Please sign in to comment.