Example #1
0
        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);
        }
Example #2
0
        private static void ShowParameterHelp(IReadOnlyDictionary <string, string> inputParams, bool showImplicitlyHiddenParams, TemplateGroupParameterDetails parameterDetails, IEngineEnvironmentSettings environmentSettings)
        {
            if (!string.IsNullOrEmpty(parameterDetails.AdditionalInfo))
            {
                Reporter.Error.WriteLine(parameterDetails.AdditionalInfo.Bold().Red());
                Reporter.Output.WriteLine();
            }

            IEnumerable <ITemplateParameter> filteredParams = TemplateParameterHelpBase.FilterParamsForHelp(parameterDetails.AllParams.ParameterDefinitions, parameterDetails.ExplicitlyHiddenParams,
                                                                                                            showImplicitlyHiddenParams, parameterDetails.HasPostActionScriptRunner, parameterDetails.ParametersToAlwaysShow);

            if (filteredParams.Any())
            {
                HelpFormatter <ITemplateParameter> formatter = new HelpFormatter <ITemplateParameter>(environmentSettings, filteredParams, 2, null, true);

                formatter.DefineColumn(
                    param =>
                {
                    string options;
                    if (string.Equals(param.Name, "allow-scripts", StringComparison.OrdinalIgnoreCase))
                    {
                        options = "--" + param.Name;
                    }
                    else
                    {
                        // the key is guaranteed to exist
                        IList <string> variants = parameterDetails.GroupVariantsForCanonicals[param.Name].ToList();
                        options = string.Join("|", variants.Reverse());
                    }

                    return("  " + options);
                },
                    LocalizableStrings.Options
                    );

                formatter.DefineColumn(delegate(ITemplateParameter param)
                {
                    StringBuilder displayValue = new StringBuilder(255);
                    displayValue.AppendLine(param.Documentation);

                    if (string.Equals(param.DataType, "choice", StringComparison.OrdinalIgnoreCase))
                    {
                        int longestChoiceLength = param.Choices.Keys.Max(x => x.Length);

                        foreach (KeyValuePair <string, string> choiceInfo in param.Choices)
                        {
                            displayValue.Append("    " + choiceInfo.Key.PadRight(longestChoiceLength + 4));

                            if (!string.IsNullOrWhiteSpace(choiceInfo.Value))
                            {
                                displayValue.Append("- " + choiceInfo.Value);
                            }

                            displayValue.AppendLine();
                        }
                    }
                    else
                    {
                        displayValue.Append(param.DataType ?? "string");
                        displayValue.AppendLine(" - " + param.Priority.ToString());
                    }

                    // display the configured value if there is one
                    string configuredValue = null;
                    if (parameterDetails.AllParams.ResolvedValues.TryGetValue(param, out object resolvedValueObject))
                    {
                        string resolvedValue = resolvedValueObject as string;

                        if (!string.IsNullOrEmpty(resolvedValue) &&
                            !string.IsNullOrEmpty(param.DefaultValue) &&
                            !string.Equals(param.DefaultValue, resolvedValue))
                        {
                            configuredValue = resolvedValue;
                        }
                    }

                    if (string.IsNullOrEmpty(configuredValue))
                    {
                        // this will catch when the user inputs the default value. The above deliberately skips it on the resolved values.
                        if (string.Equals(param.DataType, "bool", StringComparison.OrdinalIgnoreCase) &&
                            parameterDetails.GroupUserParamsWithDefaultValues.Contains(param.Name))
                        {
                            configuredValue = "true";
                        }
                        else
                        {
                            inputParams.TryGetValue(param.Name, out configuredValue);
                        }
                    }

                    if (!string.IsNullOrEmpty(configuredValue))
                    {
                        string realValue = configuredValue;

                        if (parameterDetails.InvalidParams.Contains(param.Name) ||
                            (string.Equals(param.DataType, "choice", StringComparison.OrdinalIgnoreCase) &&
                             !param.Choices.ContainsKey(configuredValue)))
                        {
                            realValue = realValue.Bold().Red();
                        }
                        else if (parameterDetails.AllParams.TryGetRuntimeValue(environmentSettings, param.Name, out object runtimeVal) && runtimeVal != null)
                        {
                            realValue = runtimeVal.ToString();
                        }

                        displayValue.AppendLine(string.Format(LocalizableStrings.ConfiguredValue, realValue));
                    }

                    // display the default value if there is one
                    if (!string.IsNullOrEmpty(param.DefaultValue))
                    {
                        displayValue.AppendLine(string.Format(LocalizableStrings.DefaultValue, param.DefaultValue));
                    }

                    return(displayValue.ToString());
                }, string.Empty);

                Reporter.Output.WriteLine(formatter.Layout());
            }
            else
            {
                Reporter.Output.WriteLine(LocalizableStrings.NoParameters);
            }
        }
        private static void ShowParameterHelp(IReadOnlyDictionary <string, string> inputParams, bool showImplicitlyHiddenParams, TemplateGroupParameterDetails parameterDetails, IEngineEnvironmentSettings environmentSettings, INewCommandInput commandInput)
        {
            IEnumerable <ITemplateParameter> filteredParams = TemplateParameterHelpBase.FilterParamsForHelp(
                parameterDetails.AllParams.ParameterDefinitions,
                parameterDetails.ExplicitlyHiddenParams,
                showImplicitlyHiddenParams,
                parameterDetails.HasPostActionScriptRunner,
                parameterDetails.ParametersToAlwaysShow);

            if (filteredParams.Any())
            {
                HelpFormatter <ITemplateParameter> formatter = new HelpFormatter <ITemplateParameter>(environmentSettings, commandInput, filteredParams, 2, null, true);

                formatter.DefineColumn(
                    param =>
                {
                    string options;
                    if (string.Equals(param.Name, "allow-scripts", StringComparison.OrdinalIgnoreCase))
                    {
                        options = "--" + param.Name;
                    }
                    else
                    {
                        // the key is guaranteed to exist
                        IList <string> variants = parameterDetails.GroupVariantsForCanonicals[param.Name].ToList();
                        options = string.Join("|", variants.Reverse());
                    }

                    return("  " + options);
                },
                    LocalizableStrings.Options
                    );

                formatter.DefineColumn(
                    delegate(ITemplateParameter param)
                {
                    StringBuilder displayValue = new StringBuilder(255);
                    displayValue.AppendLine(param.Documentation);

                    if (string.Equals(param.DataType, "choice", StringComparison.OrdinalIgnoreCase))
                    {
                        int longestChoiceLength = param.Choices.Keys.Max(x => x.Length);

                        foreach (KeyValuePair <string, ParameterChoice> choiceInfo in param.Choices)
                        {
                            displayValue.Append("    " + choiceInfo.Key.PadRight(longestChoiceLength + 4));

                            if (!string.IsNullOrWhiteSpace(choiceInfo.Value.Description))
                            {
                                displayValue.Append("- " + choiceInfo.Value.Description);
                            }

                            displayValue.AppendLine();
                        }
                    }
                    else
                    {
                        displayValue.Append(param.DataType ?? "string");
                        displayValue.AppendLine(" - " + param.Priority.ToString());
                    }

                    // determine the configured value
                    string configuredValue = null;
                    if (parameterDetails.AllParams.ResolvedValues.TryGetValue(param, out object resolvedValueObject))
                    {
                        // Set the configured value as long as it's non-empty and not the default value.
                        // If it's the default, we're not sure if it was explicitly entered or not.
                        // Below, there's a check if the user entered a value. If so, set it.
                        string resolvedValue = resolvedValueObject?.ToString() ?? string.Empty;

                        if (!string.IsNullOrEmpty(resolvedValue))
                        {
                            // bools get ToString() values of "True" & "False", but most templates use "true" & "false"
                            // So do a case-insensitive comparison on bools, case-sensitive on other values.
                            StringComparison comparisonType = string.Equals(param.DataType, "bool", StringComparison.OrdinalIgnoreCase)
                                    ? StringComparison.OrdinalIgnoreCase
                                    : StringComparison.Ordinal;

                            if (!string.Equals(param.DefaultValue, resolvedValue, comparisonType))
                            {
                                configuredValue = resolvedValue;
                            }
                        }
                    }

                    // If the resolved value is null/empty, or the default, only display the configured value if
                    // the user explicitly specified it or if it can be resolved from the DefaultIfOptionWithoutValue (or bool='true' for backwards compat).
                    if (string.IsNullOrEmpty(configuredValue))
                    {
                        bool handled = false;

                        if (parameterDetails.GroupUserParamsWithDefaultValues.Contains(param.Name))
                        {
                            if (param is IAllowDefaultIfOptionWithoutValue parameterWithNoValueDefault)
                            {
                                if (!string.IsNullOrEmpty(parameterWithNoValueDefault.DefaultIfOptionWithoutValue))
                                {
                                    configuredValue = parameterWithNoValueDefault.DefaultIfOptionWithoutValue;
                                    handled         = true;
                                }
                            }
                            else if (string.Equals(param.DataType, "bool", StringComparison.OrdinalIgnoreCase))
                            {
                                // Before the introduction of DefaultIfOptionWithoutValue, bool params were handled like this.
                                // Other param types didn't have any analogous handling.
                                configuredValue = "true";
                                handled         = true;
                            }
                        }

                        if (!handled)
                        {
                            // If the user explicitly specified the switch, the value is in the inputParams, so try to retrieve it here.
                            inputParams.TryGetValue(param.Name, out configuredValue);
                        }
                    }

                    // display the configured value if there is one
                    if (!string.IsNullOrEmpty(configuredValue))
                    {
                        string realValue = configuredValue;

                        if (parameterDetails.InvalidParams.Contains(param.Name) ||
                            (string.Equals(param.DataType, "choice", StringComparison.OrdinalIgnoreCase) &&
                             !param.Choices.ContainsKey(configuredValue)))
                        {
                            realValue = realValue.Bold().Red();
                        }
                        else if (parameterDetails.AllParams.TryGetRuntimeValue(environmentSettings, param.Name, out object runtimeVal) && runtimeVal != null)
                        {
                            realValue = runtimeVal.ToString();
                        }

                        displayValue.AppendLine(string.Format(LocalizableStrings.ConfiguredValue, realValue));
                    }

                    // display the default value if there is one
                    if (!string.IsNullOrWhiteSpace(param.DefaultValue))
                    {
                        displayValue.AppendLine(string.Format(LocalizableStrings.DefaultValue, param.DefaultValue));
                    }

                    if (param is IAllowDefaultIfOptionWithoutValue paramWithNoValueDefault &&
                        !string.IsNullOrWhiteSpace(paramWithNoValueDefault.DefaultIfOptionWithoutValue))
                    {
                        // default if option is provided without a value should not be displayed if:
                        // - it is bool parameter with "DefaultIfOptionWithoutValue": "true"
                        // - it is not bool parameter (int, string, etc) and default value coincides with "DefaultIfOptionWithoutValue"
                        if (string.Equals(param.DataType, "bool", StringComparison.OrdinalIgnoreCase))
                        {
                            if (!string.Equals(paramWithNoValueDefault.DefaultIfOptionWithoutValue, "true", StringComparison.OrdinalIgnoreCase))
                            {
                                displayValue.AppendLine(string.Format(LocalizableStrings.DefaultIfOptionWithoutValue, paramWithNoValueDefault.DefaultIfOptionWithoutValue));
                            }
                        }
                        else
                        {
                            if (!string.Equals(paramWithNoValueDefault.DefaultIfOptionWithoutValue, param.DefaultValue, StringComparison.Ordinal))
                            {
                                displayValue.AppendLine(string.Format(LocalizableStrings.DefaultIfOptionWithoutValue, paramWithNoValueDefault.DefaultIfOptionWithoutValue));
                            }
                        }
                    }

                    return(displayValue.ToString());
                },
                    string.Empty);

                Reporter.Output.WriteLine(formatter.Layout());
            }
            else
            {
                Reporter.Output.WriteLine(LocalizableStrings.NoParameters);
            }
        }