예제 #1
0
        public static CSharpRepl UseNugetDirective(this CSharpRepl repl)
        {
            var packageRefArg = new Argument <NugetPackageReference>((SymbolResult result, out NugetPackageReference reference) =>
                                                                     NugetPackageReference.TryParse(result.Token.Value, out reference))
            {
                Name = "package"
            };

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

            r.Handler = CommandHandler.Create <NugetPackageReference, KernelPipelineContext>(async(package, pipelineContext) =>
            {
                pipelineContext.OnExecute(async invocationContext =>
                {
                    invocationContext.OnNext(new NuGetPackageAdded(package));
                    invocationContext.OnCompleted();
                });
            });

            repl.AddDirective(r);

            return(repl);
        }
예제 #2
0
        public static CSharpKernel UseNugetDirective(this CSharpKernel kernel, INativeAssemblyLoadHelper helper = null)
        {
            var packageRefArg = new Argument <NugetPackageReference>((SymbolResult result, out NugetPackageReference reference) =>
                                                                     NugetPackageReference.TryParse(result.Token.Value, out reference))
            {
                Name = "package"
            };

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

            var restoreContext = new PackageRestoreContext();

            r.Handler = CommandHandler.Create <NugetPackageReference, KernelInvocationContext>(async(package, pipelineContext) =>
            {
                var addPackage = new AddNugetPackage(package)
                {
                    Handler = async context =>
                    {
                        var refs = await restoreContext.AddPackage(package.PackageName, package.PackageVersion);
                        helper?.Configure(await restoreContext.OutputPath());
                        if (refs != null)
                        {
                            foreach (var reference in refs)
                            {
                                if (reference is PortableExecutableReference peRef)
                                {
                                    helper?.Handle(peRef.FilePath);
                                }
                            }

                            kernel.AddMetadataReferences(refs);
                        }

                        context.Publish(new NuGetPackageAdded(package));
                        context.Complete();
                    }
                };

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

            kernel.AddDirective(r);

            return(kernel);
        }
예제 #3
0
        public static bool TryParse(string value, out NugetPackageReference reference)
        {
            var result = _regex.Match(value);

            if (!result.Success)
            {
                reference = null;
                return(false);
            }

            var packageName    = result.Groups["packageName"].Value;
            var packageVersion = result.Groups["packageVersion"].Value;

            reference = new NugetPackageReference(packageName, packageVersion);

            return(true);
        }
예제 #4
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 r = new Command("#r")
            {
                packageRefArg
            };

            var restoreContext = new PackageRestoreContext();

            r.Handler = CommandHandler.Create <NugetPackageReference, KernelInvocationContext>(async(package, pipelineContext) =>
            {
                var addPackage = new AddNugetPackage(package)
                {
                    Handler = async context =>
                    {
                        var refs = await restoreContext.AddPackage(package.PackageName, package.PackageVersion);
                        if (refs != null)
                        {
                            kernel.AddMetatadaReferences(refs);
                        }

                        context.OnNext(new NuGetPackageAdded(package));
                        context.OnCompleted();
                    }
                };

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

            kernel.AddDirective(r);

            return(kernel);
        }
예제 #5
0
        public static CSharpKernel UseNugetDirective(this CSharpKernel kernel, INativeAssemblyLoadHelper helper = null)
        {
            var packageRefArg = new Argument <NugetPackageReference>((SymbolResult result, out NugetPackageReference reference) =>
                                                                     NugetPackageReference.TryParse(result.Token.Value, out reference))
            {
                Name = "package"
            };

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

            var restoreContext = new PackageRestoreContext();

            r.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}";
                    }

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

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

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

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

                    var result = await installTask;
                    helper?.Configure(await restoreContext.OutputPath());

                    if (result.Succeeded)
                    {
                        foreach (var reference in result.NewReferences)
                        {
                            if (reference is PortableExecutableReference peRef)
                            {
                                helper?.Handle(peRef.FilePath);
                            }
                        }

                        kernel.AddMetadataReferences(result.NewReferences);

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

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

                        var nugetPackageDirectory = new FileSystemDirectoryAccessor(await restoreContext.GetDirectoryForPackage(package.PackageName));
                        await context.HandlingKernel.SendAsync(new LoadExtensionsInDirectory(nugetPackageDirectory, result.References.Select(r => r.Display)));
                    }
                    else
                    {
                        context.Publish(new DisplayedValueProduced($"Failed to add reference to package {package.PackageName}", context.Command));
                        context.Publish(new DisplayedValueProduced(result.DetailedErrors, context.Command));
                    }

                    context.Complete();
                };

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

            kernel.AddDirective(r);

            return(kernel);
        }
예제 #6
0
        public static CSharpKernel UseNugetDirective(
            this CSharpKernel kernel,
            Func <INativeAssemblyLoadHelper> getHelper = null)
        {
            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(kernel);

            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 = getHelper?.Invoke();

                    if (helper != null)
                    {
                        kernel.RegisterForDisposal(helper);
                    }

                    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);

                            var nugetPackageDirectory = new FileSystemDirectoryAccessor(resolvedNugetPackageReference.PackageRoot);
                            await context.HandlingKernel.SendAsync(
                                new LoadExtensionsInDirectory(
                                    nugetPackageDirectory,
                                    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);
        }