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();
            }
        }
Ejemplo n.º 2
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();
            }
        }