public StandardIOKernelServerTests(ITestOutputHelper output) { var displayIdSeed = 0; Kernel.DisplayIdGenerator = () => Interlocked.Increment(ref displayIdSeed).ToString(); var kernel = new CompositeKernel { new CSharpKernel() .UseKernelHelpers() .UseNugetDirective() .UseDefaultFormatting() .UseDefaultMagicCommands() }; _standardIOKernelServer = new StandardIOKernelServer( kernel, new StreamReader(new MemoryStream()), new StringWriter()); _kernelEvents = _standardIOKernelServer .Output .Where(s => !string.IsNullOrWhiteSpace(s)) .Select(KernelEventEnvelope.Deserialize) .ToSubscribedList(); _disposables.Add(_standardIOKernelServer); _disposables.Add(output.SubscribeToPocketLogger()); _disposables.Add(kernel.LogEventsToPocketLogger()); _disposables.Add(kernel); _disposables.Add(() => Kernel.DisplayIdGenerator = null); }
public static Task WriteAsync( this StandardIOKernelServer server, KernelCommand command) { var json = KernelCommandEnvelope.Serialize( KernelCommandEnvelope.Create(command)); return(server.WriteAsync(json)); }
internal static StandardIOKernelServer CreateServer(Kernel kernel, IConsole console) { var server = new StandardIOKernelServer( kernel, Console.In, Console.Out); kernel.RegisterForDisposal(server); return(server); }
public static Task WriteAsync( this StandardIOKernelServer server, IKernelCommand kernelCommand, int correlationId = -1) { return(server.WriteAsync( new StreamKernelCommand { Id = correlationId, CommandType = kernelCommand.GetType().Name, Command = kernelCommand }.Serialize())); }
internal static StandardIOKernelServer CreateServer(KernelBase kernel, IConsole console) { var server = new StandardIOKernelServer( kernel, Console.In, Console.Out); if (kernel is KernelBase kernelBase) { kernelBase.RegisterForDisposal(server); } return(server); }
public static Parser Create( IServiceCollection services, StartServer startServer = null, Jupyter jupyter = null, StartKernelServer startKernelServer = null, ITelemetry telemetry = null, IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } startServer ??= ((startupOptions, invocationContext) => Program.ConstructWebHost(startupOptions).Run()); jupyter ??= JupyterCommand.Do; startKernelServer ??= (async(startupOptions, kernel, console) => { var disposable = Program.StartToolLogging(startupOptions); if (kernel is KernelBase kernelBase) { kernelBase.RegisterForDisposal(disposable); } var server = new StandardIOKernelServer( kernel, Console.In, Console.Out); await server.Input.LastAsync(); }); // Setup first time use notice sentinel. firstTimeUseNoticeSentinel ??= new FirstTimeUseNoticeSentinel(VersionSensor.Version().AssemblyInformationalVersion); // Setup telemetry. telemetry ??= new Telemetry.Telemetry( VersionSensor.Version().AssemblyInformationalVersion, firstTimeUseNoticeSentinel); var filter = new TelemetryFilter(Sha256Hasher.HashWithNormalizedCasing); void Track(ParseResult o) => telemetry.SendFiltered(filter, o); var verboseOption = new Option <bool>( "--verbose", "Enable verbose logging to the console"); var logPathOption = new Option <DirectoryInfo>( "--log-path", "Enable file logging to the specified directory"); var defaultKernelOption = new Option <string>( "--default-kernel", description: "The default language for the kernel", getDefaultValue: () => "csharp"); var rootCommand = DotnetInteractive(); rootCommand.AddCommand(Jupyter()); rootCommand.AddCommand(KernelServer()); return(new CommandLineBuilder(rootCommand) .UseDefaults() .UseMiddleware(async(context, next) => { // If sentinel does not exist, print the welcome message showing the telemetry notification. if (!firstTimeUseNoticeSentinel.Exists() && !Telemetry.Telemetry.SkipFirstTimeExperience) { context.Console.Out.WriteLine(); context.Console.Out.WriteLine(Telemetry.Telemetry.WelcomeMessage); firstTimeUseNoticeSentinel.CreateIfNotExists(); } await next(context); }) .Build()); RootCommand DotnetInteractive() { var command = new RootCommand { Name = "dotnet-interactive", Description = "Interactive programming for .NET." }; command.AddOption(logPathOption); command.AddOption(verboseOption); return(command); } Command Jupyter() { var jupyterCommand = new Command("jupyter", "Starts dotnet-interactive as a Jupyter kernel") { defaultKernelOption, logPathOption, verboseOption, new Argument <FileInfo> { Name = "connection-file" }.ExistingOnly() }; jupyterCommand.Handler = CommandHandler.Create <StartupOptions, JupyterOptions, IConsole, InvocationContext>((startupOptions, options, console, context) => { Track(context.ParseResult); services .AddSingleton(c => ConnectionInformation.Load(options.ConnectionFile)) .AddSingleton( c => { return(CommandScheduler .Create <JupyterRequestContext>(delivery => c.GetRequiredService <ICommandHandler <JupyterRequestContext> >() .Trace() .Handle(delivery))); }) .AddSingleton(c => CreateKernel(options.DefaultKernel)) .AddSingleton(c => new JupyterRequestContextHandler(c.GetRequiredService <IKernel>()) .Trace()) .AddSingleton <IHostedService, Shell>() .AddSingleton <IHostedService, Heartbeat>(); return(jupyter(startupOptions, console, startServer, context)); }); var installCommand = new Command("install", "Install the .NET kernel for Jupyter") { logPathOption, verboseOption }; installCommand.Handler = CommandHandler.Create <IConsole, InvocationContext>((console, context) => { Track(context.ParseResult); return(new JupyterInstallCommand(console, new JupyterKernelSpec()).InvokeAsync()); }); jupyterCommand.AddCommand(installCommand); return(jupyterCommand); } Command KernelServer() { var startKernelServerCommand = new Command("kernel-server", "Starts dotnet-interactive with kernel functionality exposed over standard I/O") { defaultKernelOption, logPathOption, }; startKernelServerCommand.Handler = CommandHandler.Create <StartupOptions, KernelServerOptions, IConsole, InvocationContext>( (startupOptions, options, console, context) => { Track(context.ParseResult); return(startKernelServer(startupOptions, CreateKernel(options.DefaultKernel), console)); }); return(startKernelServerCommand); } }
public static Parser Create( IServiceCollection services, StartServer startServer = null, Jupyter jupyter = null, StartKernelServer startKernelServer = null, ITelemetry telemetry = null, IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } startServer ??= (startupOptions, invocationContext) => Program.ConstructWebHost(startupOptions).Run(); jupyter ??= JupyterCommand.Do; startKernelServer ??= async(startupOptions, kernel, console) => { var disposable = Program.StartToolLogging(startupOptions); if (kernel is KernelBase kernelBase) { kernelBase.RegisterForDisposal(disposable); } var server = new StandardIOKernelServer( kernel, Console.In, Console.Out); await server.Input.LastAsync(); }; // Setup first time use notice sentinel. firstTimeUseNoticeSentinel ??= new FirstTimeUseNoticeSentinel(VersionSensor.Version().AssemblyInformationalVersion); // Setup telemetry. telemetry ??= new Telemetry.Telemetry( VersionSensor.Version().AssemblyInformationalVersion, firstTimeUseNoticeSentinel); var filter = new TelemetryFilter(Sha256Hasher.HashWithNormalizedCasing); var verboseOption = new Option <bool>( "--verbose", "Enable verbose logging to the console"); var httpPortOption = new Option <int>( "--http-port", "Specifies the port on which to enable HTTP services"); var httpPortRangeOption = new Option <PortRange>( "--http-port-range", parseArgument: result => { if (result.Parent.Parent.Children.FirstOrDefault(c => c.Symbol == httpPortOption) is OptionResult conflictingOption) { var parsed = result.Parent as OptionResult; result.ErrorMessage = $"Cannot specify both {conflictingOption.Token.Value} and {parsed.Token.Value} together"; return(null); } var source = result.Tokens[0].Value; if (string.IsNullOrWhiteSpace(source)) { result.ErrorMessage = "Must specify a port range"; return(null); } var parts = source.Split(new[] { "-" }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2) { result.ErrorMessage = "Must specify a port range"; return(null); } if (!int.TryParse(parts[0], out var start) || !int.TryParse(parts[1], out var end)) { result.ErrorMessage = "Must specify a port range as StartPort-EndPort"; return(null); } if (start > end) { result.ErrorMessage = "Start port must be lower then end port"; return(null); } var pr = new PortRange(start, end); return(pr); }, description: "Specifies the range of port to use to enable HTTP services"); var logPathOption = new Option <DirectoryInfo>( "--log-path", "Enable file logging to the specified directory"); var pathOption = new Option <DirectoryInfo>( "--path", "Installs the kernelspecs to the specified directory") .ExistingOnly(); var defaultKernelOption = new Option <string>( "--default-kernel", description: "The default language for the kernel", getDefaultValue: () => "csharp"); var rootCommand = DotnetInteractive(); rootCommand.AddCommand(Jupyter()); rootCommand.AddCommand(KernelServer()); rootCommand.AddCommand(HttpServer()); return(new CommandLineBuilder(rootCommand) .UseDefaults() .UseMiddleware(async(context, next) => { if (context.ParseResult.Errors.Count == 0) { telemetry.SendFiltered(filter, context.ParseResult); } // If sentinel does not exist, print the welcome message showing the telemetry notification. if (!firstTimeUseNoticeSentinel.Exists() && !Telemetry.Telemetry.SkipFirstTimeExperience) { context.Console.Out.WriteLine(); context.Console.Out.WriteLine(Telemetry.Telemetry.WelcomeMessage); firstTimeUseNoticeSentinel.CreateIfNotExists(); } await next(context); }) .Build()); RootCommand DotnetInteractive() { var command = new RootCommand { Name = "dotnet-interactive", Description = "Interactive programming for .NET." }; command.AddOption(logPathOption); command.AddOption(verboseOption); command.AddOption(httpPortOption); return(command); } Command Jupyter() { var jupyterCommand = new Command("jupyter", "Starts dotnet-interactive as a Jupyter kernel") { defaultKernelOption, logPathOption, verboseOption, httpPortOption, httpPortRangeOption, new Argument <FileInfo> { Name = "connection-file" }.ExistingOnly() }; jupyterCommand.Handler = CommandHandler.Create <StartupOptions, JupyterOptions, IConsole, InvocationContext>(JupyterHandler); var installCommand = new Command("install", "Install the .NET kernel for Jupyter") { logPathOption, verboseOption, httpPortRangeOption, pathOption }; installCommand.Handler = CommandHandler.Create <IConsole, InvocationContext, PortRange, DirectoryInfo>(InstallHandler); jupyterCommand.AddCommand(installCommand); return(jupyterCommand); Task <int> JupyterHandler(StartupOptions startupOptions, JupyterOptions options, IConsole console, InvocationContext context) { services.AddSingleton(c => ConnectionInformation.Load(options.ConnectionFile)) .AddSingleton(_ => { var frontendEnvironment = new BrowserFrontendEnvironment { ApiUri = new Uri($"http://localhost:{startupOptions.HttpPort}") }; return(frontendEnvironment); }) .AddSingleton <FrontendEnvironment>(c => c.GetService <BrowserFrontendEnvironment>()) .AddSingleton(c => { return(CommandScheduler.Create <JupyterRequestContext>(delivery => c.GetRequiredService <ICommandHandler <JupyterRequestContext> >() .Trace() .Handle(delivery))); }) .AddSingleton(c => { var frontendEnvironment = c.GetRequiredService <BrowserFrontendEnvironment>(); var kernel = CreateKernel(options.DefaultKernel, frontendEnvironment, startupOptions, c.GetRequiredService <HttpProbingSettings>()); return(kernel); }) .AddSingleton(c => new JupyterRequestContextHandler( c.GetRequiredService <IKernel>()) .Trace()) .AddSingleton <IHostedService, Shell>() .AddSingleton <IHostedService, Heartbeat>() ; return(jupyter(startupOptions, console, startServer, context)); } Task <int> InstallHandler(IConsole console, InvocationContext context, PortRange httpPortRange, DirectoryInfo location) => new JupyterInstallCommand(console, jupyterKernelSpecInstaller: new JupyterKernelSpecInstaller(console), httpPortRange, location).InvokeAsync(); } Command HttpServer() { var startKernelHttpCommand = new Command("http", "Starts dotnet-interactive with kernel functionality exposed over http") { defaultKernelOption, httpPortOption, logPathOption, httpPortRangeOption }; startKernelHttpCommand.Handler = CommandHandler.Create <StartupOptions, KernelHttpOptions, IConsole, InvocationContext>( (startupOptions, options, console, context) => { var frontendEnvironment = new BrowserFrontendEnvironment(); services .AddSingleton(_ => frontendEnvironment) .AddSingleton(c => CreateKernel(options.DefaultKernel, frontendEnvironment, startupOptions, null)); return(jupyter(startupOptions, console, startServer, context)); }); return(startKernelHttpCommand); } Command KernelServer() { var startKernelServerCommand = new Command( "stdio", "Starts dotnet-interactive with kernel functionality exposed over standard I/O") { defaultKernelOption, logPathOption, }; startKernelServerCommand.Handler = CommandHandler.Create <StartupOptions, KernelServerOptions, IConsole, InvocationContext>( (startupOptions, options, console, context) => startKernelServer( startupOptions, CreateKernel(options.DefaultKernel, new BrowserFrontendEnvironment(), startupOptions, null), console)); return(startKernelServerCommand); } }