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