public static CreationResultStatus DisplayAliasValues(IEngineEnvironmentSettings environment, INewCommandInput commandInput, AliasRegistry aliasRegistry, string commandName)
        {
            IReadOnlyDictionary <string, string> aliasesToShow;

            if (!string.IsNullOrEmpty(commandInput.ShowAliasesAliasName))
            {
                if (aliasRegistry.AllAliases.TryGetValue(commandInput.ShowAliasesAliasName, out string aliasValue))
                {
                    aliasesToShow = new Dictionary <string, string>()
                    {
                        { commandInput.ShowAliasesAliasName, aliasValue }
                    };
                }
                else
                {
                    Reporter.Output.WriteLine(string.Format(LocalizableStrings.AliasShowErrorUnknownAlias, commandInput.ShowAliasesAliasName, commandName));
                    return(CreationResultStatus.InvalidParamValues);
                }
            }
            else
            {
                aliasesToShow = aliasRegistry.AllAliases;
                Reporter.Output.WriteLine(LocalizableStrings.AliasShowAllAliasesHeader);
            }

            HelpFormatter <KeyValuePair <string, string> > formatter = new HelpFormatter <KeyValuePair <string, string> >(environment, aliasesToShow, 2, '-', false)
                                                                       .DefineColumn(t => t.Key, LocalizableStrings.AliasName)
                                                                       .DefineColumn(t => t.Value, LocalizableStrings.AliasValue);

            Reporter.Output.WriteLine(formatter.Layout());
            return(CreationResultStatus.Success);
        }
        private void DisplayUpdateCheckResults(IEnumerable <CheckUpdateResult> versionCheckResults, INewCommandInput commandInput, bool showUpdates = true)
        {
            _ = versionCheckResults ?? throw new ArgumentNullException(nameof(versionCheckResults));
            _ = commandInput ?? throw new ArgumentNullException(nameof(commandInput));

            //handle success
            if (versionCheckResults.Any(result => result.Success && !result.IsLatestVersion) && showUpdates)
            {
                Reporter.Output.WriteLine(LocalizableStrings.TemplatePackageCoordinator_Update_Info_UpdateAvailablePackages);
                IEnumerable <(string Identifier, string CurrentVersion, string LatestVersion)> displayableResults = versionCheckResults
                                                                                                                    .Where(result => result.Success && !result.IsLatestVersion)
                                                                                                                    .Select(result => (result.TemplatePackage.Identifier, result.TemplatePackage.Version, result.LatestVersion));
                var formatter =
                    HelpFormatter
                    .For(
                        _engineEnvironmentSettings,
                        commandInput,
                        displayableResults,
                        columnPadding: 2,
                        headerSeparator: '-',
                        blankLineBetweenRows: false)
                    .DefineColumn(r => r.Identifier, out object packageColumn, LocalizableStrings.ColumnNamePackage, showAlways: true)
                    .DefineColumn(r => r.CurrentVersion, LocalizableStrings.ColumnNameCurrentVersion, showAlways: true)
                    .DefineColumn(r => r.LatestVersion, LocalizableStrings.ColumnNameLatestVersion, showAlways: true)
                    .OrderBy(packageColumn);
                Reporter.Output.WriteLine(formatter.Layout());
                Reporter.Output.WriteLine();

                Reporter.Output.WriteLine(LocalizableStrings.TemplatePackageCoordinator_Update_Info_UpdateSingleCommandHeader);
                Reporter.Output.WriteCommand(commandInput.InstallCommandExample(withVersion: true));
                Reporter.Output.WriteCommand(
                    commandInput.InstallCommandExample(
                        packageID: displayableResults.First().Identifier,
                        version: displayableResults.First().LatestVersion));
                Reporter.Output.WriteLine();
                Reporter.Output.WriteLine(LocalizableStrings.TemplatePackageCoordinator_Update_Info_UpdateAllCommandHeader);
                Reporter.Output.WriteCommand(commandInput.UpdateApplyCommandExample());
                Reporter.Output.WriteLine();
            }

            //handle errors
            if (versionCheckResults.Any(result => !result.Success))
            {
                foreach (CheckUpdateResult result in versionCheckResults.Where(result => !result.Success))
                {
                    HandleUpdateCheckErrors(result);
                }
                Reporter.Error.WriteLine();
            }
        }
Example #3
0
        internal static CreationResultStatus DisplayAliasValues(IEngineEnvironmentSettings environment, INewCommandInput commandInput, AliasRegistry aliasRegistry, string commandName)
        {
            IReadOnlyDictionary <string, IReadOnlyList <string> > aliasesToShow;

            if (!string.IsNullOrEmpty(commandInput.ShowAliasesAliasName))
            {
                if (aliasRegistry.AllAliases.TryGetValue(commandInput.ShowAliasesAliasName, out IReadOnlyList <string> aliasValue))
                {
                    aliasesToShow = new Dictionary <string, IReadOnlyList <string> >()
                    {
                        { commandInput.ShowAliasesAliasName, aliasValue }
                    };
                }
                else
                {
                    Reporter.Output.WriteLine(string.Format(LocalizableStrings.AliasShowErrorUnknownAlias, commandInput.ShowAliasesAliasName, commandName));
                    return(CreationResultStatus.InvalidParamValues);
                }
            }
            else
            {
                aliasesToShow = aliasRegistry.AllAliases;
                Reporter.Output.WriteLine(LocalizableStrings.AliasShowAllAliasesHeader);
            }

            HelpFormatter <KeyValuePair <string, IReadOnlyList <string> > > formatter =
                new HelpFormatter <KeyValuePair <string, IReadOnlyList <string> > >(
                    environment,
                    commandInput,
                    aliasesToShow,
                    columnPadding: 2,
                    headerSeparator: '-',
                    blankLineBetweenRows: false)
                .DefineColumn(t => t.Key, LocalizableStrings.AliasName, showAlways: true)
                .DefineColumn(t => string.Join(" ", t.Value), LocalizableStrings.AliasValue, showAlways: true);

            Reporter.Output.WriteLine(formatter.Layout());
            return(CreationResultStatus.Success);
        }
Example #4
0
        private void ParameterHelp(IParameterSet allParams, string additionalInfo, HashSet <string> hiddenParams)
        {
            if (!string.IsNullOrEmpty(additionalInfo))
            {
                Reporter.Error.WriteLine(additionalInfo.Bold().Red());
                Reporter.Output.WriteLine();
            }

            IEnumerable <ITemplateParameter> filteredParams = FilterParamsForHelp(allParams, hiddenParams);

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

                formatter.DefineColumn(
                    param =>
                {
                    // the key is guaranteed to exist
                    IList <string> variants = _app.CanonicalToVariantsTemplateParamMap[param.Name];
                    string 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));
                            displayValue.AppendLine("- " + choiceInfo.Value);
                        }
                    }
                    else
                    {
                        displayValue.Append(param.DataType ?? "string");
                        displayValue.AppendLine(" - " + param.Priority.ToString());
                    }

                    // display the configured value if there is one
                    string configuredValue = null;
                    if (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) &&
                            _app.TemplateParamHasValue(param.Name) &&
                            string.IsNullOrEmpty(_app.TemplateParamValue(param.Name)))
                        {
                            configuredValue = "true";
                        }
                        else
                        {
                            _app.AllTemplateParams.TryGetValue(param.Name, out configuredValue);
                        }
                    }

                    if (!string.IsNullOrEmpty(configuredValue))
                    {
                        displayValue.AppendLine(string.Format(LocalizableStrings.ConfiguredValue, configuredValue));
                    }

                    // 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);
            }
        }
Example #5
0
        private void DisplayTemplateList(bool showAll = false)
        {
            IReadOnlyCollection <ITemplateInfo> results = null;

            try
            {
                if (showAll)
                {
                    results = PerformAllTemplatesInContextQueryAsync().Result.Where(x => x.IsMatch).Select(x => x.Info).ToList();

                    if (results.Count == 0)
                    {
                        results = PerformAllTemplatesInContextQueryAsync().Result.Select(x => x.Info).ToList();
                    }
                }
                else
                {
                    results = _matchedTemplates.Where(x => x.IsMatch).Select(x => x.Info).ToList();

                    if (results.Count == 0)
                    {
                        results = _matchedTemplates.Select(x => x.Info).ToList();
                    }
                }
            }
            catch (TemplateAuthoringException tae)
            {
                Reporter.Error.WriteLine(tae.Message.Bold().Red());
                return;
            }

            IEnumerable <IGrouping <string, ITemplateInfo> > grouped = results.GroupBy(x => x.GroupIdentity);

            EnvironmentSettings.Host.TryGetHostParamDefault("prefs:language", out string defaultLanguage);

            Dictionary <ITemplateInfo, string> templatesVersusLanguages = new Dictionary <ITemplateInfo, string>();

            foreach (IGrouping <string, ITemplateInfo> grouping in grouped)
            {
                List <string> languages = new List <string>();

                foreach (ITemplateInfo info in grouping)
                {
                    if (info.Tags != null && info.Tags.TryGetValue("language", out string lang))
                    {
                        if (string.IsNullOrEmpty(Language) && string.Equals(defaultLanguage, lang, StringComparison.OrdinalIgnoreCase))
                        {
                            lang = $"[{lang}]";
                        }

                        languages.Add(lang);
                    }
                }

                templatesVersusLanguages[grouping.First()] = string.Join(", ", languages);
            }

            HelpFormatter <KeyValuePair <ITemplateInfo, string> > formatter = HelpFormatter.For(EnvironmentSettings, templatesVersusLanguages, 6, '-', false)
                                                                              .DefineColumn(t => t.Key.Name, LocalizableStrings.Templates)
                                                                              .DefineColumn(t => t.Key.ShortName, LocalizableStrings.ShortName)
                                                                              .DefineColumn(t => t.Value, out object languageColumn, LocalizableStrings.Language)
                                                                              .DefineColumn(t => t.Key.Classifications != null ? string.Join("/", t.Key.Classifications) : null, out object tagsColumn, LocalizableStrings.Tags)
                                                                              .OrderByDescending(languageColumn, new NullOrEmptyIsLastStringComparer())
                                                                              .OrderBy(tagsColumn);

            Reporter.Output.WriteLine(formatter.Layout());

            if (!IsListFlagSpecified)
            {
                ShowInvocationExamples();
            }
        }