Пример #1
0
        public void Nuget_package_reference_correctly_parses_package_name(string value)
        {
            NugetPackageReference.TryParse(value, out var reference);

            reference.PackageName.Should().Be("PocketLogger");
        }
Пример #2
0
        public void Nuget_package_reference_correctly_parses_package_version(string value, string expectedVersion)
        {
            NugetPackageReference.TryParse(value, out var reference);

            reference.PackageVersion.Should().Be(expectedVersion);
        }
Пример #3
0
        public static CSharpKernel UseNugetDirective(
            this CSharpKernel kernel)
        {
            var packageRefArg = new Argument <NugetPackageReference>((SymbolResult result, out NugetPackageReference reference) =>
                                                                     NugetPackageReference.TryParse(result.Token.Value, out reference))
            {
                Name = "package"
            };

            var command = new Command("#r")
            {
                packageRefArg
            };

            var restoreContext = new PackageRestoreContext();

            command.Handler = CommandHandler.Create <NugetPackageReference, KernelInvocationContext>(async(package, pipelineContext) =>
            {
                var addPackage = new AddNugetPackage(package);

                addPackage.Handler = async context =>
                {
                    var message = $"Installing package {package.PackageName}";
                    if (!string.IsNullOrWhiteSpace(package.PackageVersion))
                    {
                        message += $", version {package.PackageVersion}";
                    }

                    message += "...";

                    var key       = message;
                    var displayed = new DisplayedValueProduced(message, context.Command, valueId: key);
                    context.Publish(displayed);

                    var addPackageTask = restoreContext.AddPackage(
                        package.PackageName,
                        package.PackageVersion,
                        package.RestoreSources);

                    while (await Task.WhenAny(Task.Delay(500), addPackageTask) != addPackageTask)
                    {
                        message += ".";
                        context.Publish(new DisplayedValueUpdated(message, key));
                    }

                    message += "done!";
                    context.Publish(new DisplayedValueUpdated(message, key));

                    var result = await addPackageTask;

                    var helper = kernel.NativeAssemblyLoadHelper;

                    if (result.Succeeded)
                    {
                        switch (result)
                        {
                        case AddNugetPackageResult packageResult:

                            var nativeLibraryProbingPaths = packageResult.NativeLibraryProbingPaths;
                            helper?.SetNativeLibraryProbingPaths(nativeLibraryProbingPaths);

                            var addedAssemblyPaths =
                                packageResult
                                .AddedReferences
                                .SelectMany(added => added.AssemblyPaths)
                                .ToArray();

                            if (helper != null)
                            {
                                foreach (var addedReference in packageResult.AddedReferences)
                                {
                                    helper.Handle(addedReference);
                                }
                            }

                            kernel.AddScriptReferences(packageResult.AddedReferences);

                            context.Publish(
                                new DisplayedValueProduced($"Successfully added reference to package {package.PackageName}, version {packageResult.InstalledVersion}",
                                                           context.Command));

                            context.Publish(new NuGetPackageAdded(addPackage, package));

                            var resolvedNugetPackageReference = await restoreContext.GetResolvedNugetPackageReference(package.PackageName);

                            await context.HandlingKernel.SendAsync(
                                new LoadExtensionsInDirectory(
                                    resolvedNugetPackageReference.PackageRoot,
                                    addedAssemblyPaths));
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        var errors = $"{string.Join(Environment.NewLine, result.Errors)}";

                        switch (result)
                        {
                        case AddNugetPackageResult _:
                            context.Publish(
                                new ErrorProduced(
                                    $"Failed to add reference to package {package.PackageName}{Environment.NewLine}{errors}"));
                            break;

                        case AddNugetRestoreSourcesResult _:
                            context.Publish(
                                new ErrorProduced(
                                    $"Failed to apply RestoreSources {package.RestoreSources}{Environment.NewLine}{errors}"));
                            break;

                        default:
                            break;
                        }
                    }

                    context.Complete();
                };

                await pipelineContext.HandlingKernel.SendAsync(addPackage);
            });

            kernel.AddDirective(command);

            return(kernel);
        }