private static Command r(PackageRestoreContext restoreContext)
        {
            var rDirective = new Command("#r")
            {
                new Argument <PackageReferenceOrFileInfo>(
                    result =>
                {
                    var token = result.Tokens
                                .Select(t => t.Value)
                                .SingleOrDefault();

                    if (PackageReference.TryParse(token, out var reference))
                    {
                        return(reference);
                    }

                    if (token != null &&
                        !token.StartsWith("nuget:") &&
                        !Path.EndsInDirectorySeparator(token))
                    {
                        return(new FileInfo(token));
                    }

                    result.ErrorMessage = $"Unable to parse package reference: \"{token}\"";

                    return(null);
                })
                {
                    Name = "package"
                }
            };

            rDirective.Handler = CommandHandler.Create <PackageReferenceOrFileInfo, KernelInvocationContext>(HandleAddPackageReference);

            return(rDirective);

            Task HandleAddPackageReference(
                PackageReferenceOrFileInfo package,
                KernelInvocationContext context)
            {
                if (package?.Value is PackageReference pkg)
                {
                    var alreadyGotten = restoreContext.ResolvedPackageReferences
                                        .Concat(restoreContext.RequestedPackageReferences)
                                        .FirstOrDefault(r => r.PackageName.Equals(pkg.PackageName, StringComparison.OrdinalIgnoreCase));

                    if (alreadyGotten is { } && !string.IsNullOrWhiteSpace(pkg.PackageVersion) && pkg.PackageVersion != alreadyGotten.PackageVersion)
                    {
                        var errorMessage = GenerateErrorMessage(pkg, alreadyGotten).ToString(OutputMode.NonAnsi);
                        context.Publish(new ErrorProduced(errorMessage));
                    }
        private static Command i(PackageRestoreContext restoreContext)
        {
            var iDirective = new Command("#i")
            {
                new Argument <string>("source")
            };

            iDirective.Handler = CommandHandler.Create <string, KernelInvocationContext>((source, context) =>
            {
                restoreContext.AddRestoreSource(source.Replace("nuget:", ""));

                IHtmlContent content = div(
                    strong("Restore sources"),
                    ul(restoreContext.RestoreSources
                       .Select(s => li(span(s)))));

                context.DisplayAsync(content);
            });
            return(iDirective);
        }
        public static T UseNugetDirective <T>(this T kernel) where T : KernelBase, ISupportNuget
        {
            var restoreContext = new PackageRestoreContext(kernel);
            var iSupportNuget  = kernel as ISupportNuget;

            iSupportNuget.Initialize(restoreContext.AssemblyProbingPaths, restoreContext.NativeProbingRoots);

            kernel.SetProperty(restoreContext);
            kernel.RegisterForDisposal(restoreContext);

            kernel.AddDirective(i(restoreContext));
            kernel.AddDirective(r(restoreContext));

            var restore = new Command("#!nuget-restore")
            {
                Handler  = CommandHandler.Create(DoNugetRestore(kernel, restoreContext)),
                IsHidden = true
            };

            kernel.AddDirective(restore);

            return(kernel);
        }