Ejemplo n.º 1
0
        public static IReadOnlyCollection <ITemplate> List(string searchString, CommandOption source)
        {
            HashSet <ITemplate> results = new HashSet <ITemplate>(TemplateEqualityComparer.Default);
            IReadOnlyList <IConfiguredTemplateSource> searchSources;

            if (!source.HasValue())
            {
                searchSources = Program.Broker.GetConfiguredSources().ToList();
            }
            else
            {
                IConfiguredTemplateSource realSource = Program.Broker.GetConfiguredSources().FirstOrDefault(x => x.Alias == source.Value());
                if (realSource == null)
                {
                    return(results);
                }

                searchSources = new List <IConfiguredTemplateSource> {
                    realSource
                };
            }

            searchSources = ConfiguredTemplateSourceHelper.Scan(searchSources, Program.Broker.ComponentRegistry.OfType <ITemplateSource>());

            foreach (IGenerator gen in Program.Broker.ComponentRegistry.OfType <IGenerator>())
            {
                foreach (IConfiguredTemplateSource target in searchSources)
                {
                    results.UnionWith(gen.GetTemplatesFromSource(target));
                }
            }

            IReadOnlyCollection <ITemplate> aliasResults = AliasRegistry.GetTemplatesForAlias(searchString, results);

            if (!string.IsNullOrWhiteSpace(searchString))
            {
                results.RemoveWhere(x => x.Name.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) < 0 && (x.ShortName?.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) ?? -1) < 0);
            }

            results.UnionWith(aliasResults);
            return(results);
        }
Ejemplo n.º 2
0
        private static bool TryGetTemplate(string templateName, CommandOption source, bool quiet, out ITemplate tmplt, out IGenerator generator)
        {
            IReadOnlyList <IConfiguredTemplateSource> searchSources;

            if (!source.HasValue())
            {
                searchSources = Program.Broker.GetConfiguredSources().ToList();
            }
            else
            {
                IConfiguredTemplateSource realSource = Program.Broker.GetConfiguredSources().FirstOrDefault(x => x.Alias == source.Value());
                if (realSource == null)
                {
                    tmplt     = null;
                    generator = null;
                    return(false);
                }

                searchSources = new List <IConfiguredTemplateSource> {
                    realSource
                };
            }

            searchSources = ConfiguredTemplateSourceHelper.Scan(searchSources, Program.Broker.ComponentRegistry.OfType <ITemplateSource>());

            string aliasTemplateName = AliasRegistry.GetTemplateNameForAlias(templateName);

            generator = null;
            tmplt     = null;

            foreach (IGenerator gen in Program.Broker.ComponentRegistry.OfType <IGenerator>())
            {
                foreach (IConfiguredTemplateSource target in searchSources)
                {
                    if (gen.TryGetTemplateFromSource(target, templateName, out tmplt))
                    {
                        generator = gen;
                        break;
                    }

                    if (aliasTemplateName != null && gen.TryGetTemplateFromSource(target, aliasTemplateName, out tmplt))
                    {
                        generator = gen;
                        break;
                    }
                }

                if (generator != null)
                {
                    break;
                }
            }

            if (generator == null || tmplt == null)
            {
                List <ITemplate> results = List(templateName, source).ToList();

                if (results.Count == 0)
                {
                    if (!string.IsNullOrWhiteSpace(templateName) || source.HasValue())
                    {
                        Reporter.Error.WriteLine($"No template containing \"{templateName}\" was found in any of the configured sources.".Bold().Red());
                    }
                    else
                    {
                        TableFormatter.Print(results, "(No Items)", "   ", '-', new Dictionary <string, Func <ITemplate, string> >
                        {
                            { "#", x => "0." },
                            { "Templates", x => x.Name },
                            { "Short Names", x => $"[{x.ShortName}]" }
                        });
                    }

                    return(false);
                }

                int index;

                if (results.Count != 1 || string.IsNullOrWhiteSpace(templateName))
                {
                    int counter = 0;
                    TableFormatter.Print(results, "(No Items)", "   ", '-', new Dictionary <string, Func <ITemplate, string> >
                    {
                        { "#", x => $"{++counter}." },
                        { "Templates", x => x.Name },
                        { "Short Names", x => $"[{x.ShortName}]" }
                    });

                    Reporter.Output.WriteLine();
                    Reporter.Output.WriteLine("Select a template [1]:");

                    string key = Console.ReadLine();

                    if (string.IsNullOrWhiteSpace(key))
                    {
                        key = "1";
                    }

                    while (!int.TryParse(key, out index))
                    {
                        if (string.Equals(key, "q", StringComparison.OrdinalIgnoreCase))
                        {
                            return(false);
                        }

                        key = Console.ReadLine();
                    }
                }
                else
                {
                    if (!quiet)
                    {
                        Reporter.Output.WriteLine($"Using template: {results[0].Name} [{results[0].ShortName}] {AliasRegistry.GetAliasForTemplate(results[0])}");
                    }

                    index = 1;
                }

                tmplt     = results[index - 1];
                generator = results[index - 1].Generator;
            }

            return(true);
        }