示例#1
0
        internal static IEnumerable <TemplateCommand> GetMatchingTemplates(
            InstantiateCommandArgs instantiateCommandArgs,
            IEngineEnvironmentSettings environmentSettings,
            TemplatePackageManager templatePackageManager,
            TemplateGroup templateGroup)
        {
            List <TemplateCommand> matchingTemplates = new();

            //unlike instantiation we need to try all the templates
            //however we try them in precedence order
            //so the highest priority one is first
            foreach (IGrouping <int, CliTemplateInfo> templateGrouping in templateGroup.Templates.GroupBy(g => g.Precedence).OrderByDescending(g => g.Key))
            {
                foreach (CliTemplateInfo template in templateGrouping)
                {
                    if (ReparseForTemplate(
                            instantiateCommandArgs,
                            environmentSettings,
                            templatePackageManager,
                            templateGroup,
                            template)
                        is (TemplateCommand command, ParseResult parseResult))
                    {
                        if (!parseResult.Errors.Any())
                        {
                            matchingTemplates.Add(command);
                        }
                    }
                }
            }

            return(matchingTemplates);
        }
示例#2
0
 public IEnumerable <HelpSectionDelegate> CustomHelpLayout()
 {
     yield return((context) =>
     {
         InstantiateCommandArgs instantiateCommandArgs = new InstantiateCommandArgs(this, context.ParseResult);
         using IEngineEnvironmentSettings environmentSettings = CreateEnvironmentSettings(instantiateCommandArgs, context.ParseResult);
         WriteHelp(context, instantiateCommandArgs, environmentSettings);
     });
 }
示例#3
0
 public IEnumerable <HelpSectionDelegate> CustomHelpLayout()
 {
     yield return((context) =>
     {
         if (context.ParseResult.CommandResult.Command is not NewCommand newCommand)
         {
             throw new ArgumentException($"{nameof(context)} should be for {nameof(NewCommand)}");
         }
         NewCommandArgs args = new NewCommandArgs(newCommand, context.ParseResult);
         using IEngineEnvironmentSettings environmentSettings = CreateEnvironmentSettings(args, context.ParseResult);
         InstantiateCommandArgs instantiateCommandArgs = InstantiateCommandArgs.FromNewCommandArgs(args);
         InstantiateCommand.WriteHelp(context, instantiateCommandArgs, environmentSettings);
     });
 }
示例#4
0
        internal static void ShowHintForOtherTemplates(TemplateGroup templateGroup, CliTemplateInfo preferredtemplate, InstantiateCommandArgs args, TextWriter writer)
        {
            //other languages
            if (templateGroup.Languages.Count <= 1)
            {
                return;
            }

            string?preferredLanguage = preferredtemplate.GetLanguage();

            List <string> supportedLanguages = new List <string>();

            foreach (string?language in templateGroup.Languages)
            {
                if (string.IsNullOrWhiteSpace(language))
                {
                    continue;
                }
                if (!language.Equals(preferredLanguage, StringComparison.OrdinalIgnoreCase))
                {
                    supportedLanguages.Add(language);
                }
            }

            if (!supportedLanguages.Any())
            {
                return;
            }
            supportedLanguages.Sort(StringComparer.OrdinalIgnoreCase);
            writer.WriteLine(HelpStrings.Hint_HelpForOtherLanguages, string.Join(", ", supportedLanguages));
            writer.WriteLine(
                Example
                .For <NewCommand>(args.ParseResult)
                .WithArgument(NewCommand.ShortNameArgument, templateGroup.ShortNames[0])
                .WithHelpOption()
                .WithOption(SharedOptionsFactory.CreateLanguageOption(), supportedLanguages.First())
                .ToString().Indent());
            writer.WriteLine();

            //other types
            if (templateGroup.Types.Count <= 1)
            {
                return;
            }

            string?preferredType = preferredtemplate.GetTemplateType();

            List <string> supportedTypes = new List <string>();

            foreach (string?type in templateGroup.Types)
            {
                if (string.IsNullOrWhiteSpace(type))
                {
                    continue;
                }
                if (!type.Equals(preferredType, StringComparison.OrdinalIgnoreCase))
                {
                    supportedTypes.Add(type);
                }
            }

            if (!supportedTypes.Any())
            {
                return;
            }
            supportedTypes.Sort(StringComparer.OrdinalIgnoreCase);
            writer.WriteLine(HelpStrings.Hint_HelpForOtherTypes, string.Join(", ", supportedTypes));
            writer.WriteLine(Example
                             .For <NewCommand>(args.ParseResult)
                             .WithArgument(NewCommand.ShortNameArgument, templateGroup.ShortNames[0])
                             .WithHelpOption()
                             .WithOption(SharedOptionsFactory.CreateTypeOption(), supportedTypes.First())
                             .ToString().Indent());
            writer.WriteLine();
        }
示例#5
0
        public static void WriteHelp(HelpContext context, InstantiateCommandArgs instantiateCommandArgs, IEngineEnvironmentSettings environmentSettings)
        {
            if (string.IsNullOrWhiteSpace(instantiateCommandArgs.ShortName))
            {
                WriteCustomInstantiateHelp(context, instantiateCommandArgs.Command);
                return;
            }

            using TemplatePackageManager templatePackageManager = new TemplatePackageManager(environmentSettings);
            HostSpecificDataLoader hostSpecificDataLoader = new HostSpecificDataLoader(environmentSettings);

            //TODO: consider use cache only for help
            var selectedTemplateGroups = Task.Run(
                async() => await GetMatchingTemplateGroupsAsync(
                    instantiateCommandArgs,
                    templatePackageManager,
                    hostSpecificDataLoader,
                    default).ConfigureAwait(false))
                                         .GetAwaiter()
                                         .GetResult();

            if (!selectedTemplateGroups.Any())
            {
                //help do not return error exit code, so we write error to StdOut instead
                HandleNoMatchingTemplateGroup(instantiateCommandArgs, Reporter.Output);
                return;
            }
            if (selectedTemplateGroups.Take(2).Count() > 1)
            {
                HandleAmbiguousTemplateGroup(environmentSettings, templatePackageManager, selectedTemplateGroups, Reporter.Output);
                return;
            }

            TemplateGroup templateGroup = selectedTemplateGroups.Single();
            IEnumerable <TemplateCommand> matchingTemplates =
                GetMatchingTemplates(
                    instantiateCommandArgs,
                    environmentSettings,
                    templatePackageManager,
                    templateGroup);

            if (!matchingTemplates.Any())
            {
                //output is handled in HandleNoTemplateFoundResult
                HandleNoTemplateFoundResult(instantiateCommandArgs, environmentSettings, templatePackageManager, templateGroup, Reporter.Output);
                return;
            }

            if (!VerifyMatchingTemplates(
                    environmentSettings,
                    matchingTemplates,
                    Reporter.Output,
                    out IEnumerable <TemplateCommand>?templatesToShow))
            {
                //error
                //output is handled in VerifyMatchingTemplates
                return;
            }

            var preferredTemplate = templatesToShow.OrderByDescending(x => x.Template.Precedence).First();

            ShowTemplateDetailHeaders(preferredTemplate.Template, context.Output);
            //we need to show all possible short names (not just the one specified)
            ShowUsage(instantiateCommandArgs.Command, templateGroup.ShortNames, context);
            ShowCommandOptions(templatesToShow, preferredTemplate, context);
            ShowTemplateSpecificOptions(templatesToShow, context);
            ShowHintForOtherTemplates(templateGroup, preferredTemplate.Template, instantiateCommandArgs, context.Output);
        }