private static bool GenerateUsageForTemplate(ITemplateInfo templateInfo, IHostSpecificDataLoader hostDataLoader, string commandName) { HostSpecificTemplateData hostTemplateData = hostDataLoader.ReadHostSpecificTemplateData(templateInfo); if (hostTemplateData.UsageExamples != null) { if (hostTemplateData.UsageExamples.Count == 0) { return(false); } Reporter.Output.WriteLine($" dotnet {commandName} {templateInfo.ShortName} {hostTemplateData.UsageExamples[0]}"); return(true); } Reporter.Output.Write($" dotnet {commandName} {templateInfo.ShortName}"); IReadOnlyList <ITemplateParameter> allParameterDefinitions = templateInfo.Parameters; IEnumerable <ITemplateParameter> filteredParams = TemplateParameterHelpBase.FilterParamsForHelp(allParameterDefinitions, hostTemplateData.HiddenParameterNames, parametersToAlwaysShow: hostTemplateData.ParametersToAlwaysShow); foreach (ITemplateParameter parameter in filteredParams) { if (string.Equals(parameter.DataType, "bool", StringComparison.OrdinalIgnoreCase) && string.Equals(parameter.DefaultValue, "false", StringComparison.OrdinalIgnoreCase)) { continue; } else if (string.Equals(parameter.DataType, "string", StringComparison.OrdinalIgnoreCase)) { continue; } else if (string.Equals(parameter.DataType, "choice", StringComparison.OrdinalIgnoreCase) && parameter.Choices.Count == 1) { continue; } string displayParameter = hostTemplateData.DisplayNameForParameter(parameter.Name); Reporter.Output.Write($" --{displayParameter}"); if (!string.IsNullOrEmpty(parameter.DefaultValue) && !string.Equals(parameter.DataType, "bool", StringComparison.OrdinalIgnoreCase)) { Reporter.Output.Write($" {parameter.DefaultValue}"); } } Reporter.Output.WriteLine(); return(true); }
private static bool IsTemplateHiddenByHostFile(ITemplateInfo templateInfo, IHostSpecificDataLoader hostDataLoader) { HostSpecificTemplateData hostData = hostDataLoader.ReadHostSpecificTemplateData(templateInfo); return hostData.IsHidden; }
public static void ParseTemplateArgs(ITemplateInfo templateInfo, IHostSpecificDataLoader hostDataLoader, INewCommandInput commandInput) { HostSpecificTemplateData hostData = hostDataLoader.ReadHostSpecificTemplateData(templateInfo); commandInput.ReparseForTemplate(templateInfo, hostData); }
private static TemplateGroupParameterDetails DetermineParameterDispositionsForTemplateGroup(IReadOnlyList <ITemplateInfo> templateGroup, IEngineEnvironmentSettings environmentSettings, INewCommandInput commandInput, IHostSpecificDataLoader hostDataLoader, TemplateCreator templateCreator) { HashSet <string> groupUserParamsWithInvalidValues = new HashSet <string>(StringComparer.Ordinal); bool groupHasPostActionScriptRunner = false; List <IParameterSet> parameterSetsForAllTemplatesInGroup = new List <IParameterSet>(); IDictionary <string, InvalidParameterInfo> invalidParametersForGroup = new Dictionary <string, InvalidParameterInfo>(StringComparer.Ordinal); bool firstInList = true; Dictionary <string, IReadOnlyList <string> > defaultVariantsForCanonicals = new Dictionary <string, IReadOnlyList <string> >(StringComparer.Ordinal); Dictionary <string, IReadOnlyList <string> > groupVariantsForCanonicals = new Dictionary <string, IReadOnlyList <string> >(StringComparer.Ordinal); HashSet <string> groupUserParamsWithDefaultValues = new HashSet <string>(StringComparer.Ordinal); Dictionary <string, bool> parameterHidingDisposition = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase); HashSet <string> parametersToAlwaysShow = new HashSet <string>(StringComparer.Ordinal); foreach (ITemplateInfo templateInfo in templateGroup.OrderByDescending(x => x.Precedence)) { TemplateUsageInformation usageInformation = TemplateUsageHelp.GetTemplateUsageInformation(templateInfo, environmentSettings, commandInput, hostDataLoader, templateCreator); HostSpecificTemplateData hostSpecificTemplateData = hostDataLoader.ReadHostSpecificTemplateData(templateInfo); HashSet <string> parametersToExplicitlyHide = hostSpecificTemplateData?.HiddenParameterNames ?? new HashSet <string>(StringComparer.Ordinal); foreach (ITemplateParameter parameter in usageInformation.AllParameters.ParameterDefinitions) { //If the parameter has previously been encountered... if (parameterHidingDisposition.TryGetValue(parameter.Name, out bool isCurrentlyHidden)) { //...and it was hidden, but it's not hidden in this template in the group, // remove its hiding, otherwise leave it as is if (isCurrentlyHidden && !parametersToExplicitlyHide.Contains(parameter.Name)) { parameterHidingDisposition[parameter.Name] = false; } } else { //...otherwise, since this is the first time the parameter has been seen, // its hiding state should be used as the current disposition parameterHidingDisposition[parameter.Name] = parametersToExplicitlyHide.Contains(parameter.Name); } } if (firstInList) { invalidParametersForGroup = usageInformation.InvalidParameters.ToDictionary(x => x.Canonical, x => x); firstInList = false; } else { invalidParametersForGroup = InvalidParameterInfo.IntersectWithExisting(invalidParametersForGroup, usageInformation.InvalidParameters); } groupUserParamsWithInvalidValues.IntersectWith(usageInformation.UserParametersWithInvalidValues); // intersect because if the value is valid for any version, it's valid. groupHasPostActionScriptRunner |= usageInformation.HasPostActionScriptRunner; parameterSetsForAllTemplatesInGroup.Add(usageInformation.AllParameters); // If this template has name overrides (either long or short), it's opinionated. // If it's the first opinionated template about the param, use its variants. // Else this template is not opinionated, note its values if there aren't defaults for the param already. // At the end, anything in the default list that isn't in the opinionated list will get merged in. // TODO: write tests for this code (and the rest of this method while we're at it) foreach (KeyValuePair <string, IReadOnlyList <string> > canonicalAndVariants in usageInformation.VariantsForCanonicals) { if (hostSpecificTemplateData.LongNameOverrides.ContainsKey(canonicalAndVariants.Key) || hostSpecificTemplateData.ShortNameOverrides.ContainsKey(canonicalAndVariants.Key)) { // this template is opinionated about this parameter. If no previous template is opinionated about this param, use this template's variants. if (!groupVariantsForCanonicals.ContainsKey(canonicalAndVariants.Key)) { groupVariantsForCanonicals[canonicalAndVariants.Key] = canonicalAndVariants.Value; } } else { // this template is not opinionated about this parameter. If no previous template had defaults for this param, use this template's defaults. if (!defaultVariantsForCanonicals.ContainsKey(canonicalAndVariants.Key)) { defaultVariantsForCanonicals[canonicalAndVariants.Key] = canonicalAndVariants.Value; } } } // If any template says the user input value is the default, include it here. groupUserParamsWithDefaultValues.UnionWith(usageInformation.UserParametersWithDefaultValues); parametersToAlwaysShow.UnionWith(hostSpecificTemplateData.ParametersToAlwaysShow); } // aggregate the parameter variants foreach (KeyValuePair <string, IReadOnlyList <string> > defaultVariants in defaultVariantsForCanonicals) { if (!groupVariantsForCanonicals.ContainsKey(defaultVariants.Key)) { // there were no opinionated variants, take the preferred default. groupVariantsForCanonicals[defaultVariants.Key] = defaultVariants.Value; } } IParameterSet allGroupParameters = new TemplateGroupParameterSet(parameterSetsForAllTemplatesInGroup); string parameterErrors = InvalidParameterInfo.InvalidParameterListToString(invalidParametersForGroup.Values.ToList()); HashSet <string> parametersToHide = new HashSet <string>(parameterHidingDisposition.Where(x => x.Value).Select(x => x.Key), StringComparer.Ordinal); return(new TemplateGroupParameterDetails { AllParams = allGroupParameters, AdditionalInfo = parameterErrors, InvalidParams = groupUserParamsWithInvalidValues.ToList(), ExplicitlyHiddenParams = parametersToHide, GroupVariantsForCanonicals = groupVariantsForCanonicals, GroupUserParamsWithDefaultValues = groupUserParamsWithDefaultValues, HasPostActionScriptRunner = groupHasPostActionScriptRunner, ParametersToAlwaysShow = parametersToAlwaysShow, }); }
/// <summary> /// Checks if the template is hidden by host specific template settings. /// </summary> internal static bool IsHiddenByHostFile(this ITemplateInfo template, IHostSpecificDataLoader hostSpecificDataLoader) { HostSpecificTemplateData hostData = hostSpecificDataLoader.ReadHostSpecificTemplateData(template); return(hostData.IsHidden); }