Skip to content

Commit

Permalink
update packages (#51)
Browse files Browse the repository at this point in the history
* update packages

* basic Markdown kernel, no rendering
  • Loading branch information
jonsequitur authored Jun 25, 2022
1 parent 789a369 commit 82d7df4
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 132 deletions.
1 change: 0 additions & 1 deletion src/dotnet-repl.Tests/OutputTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Reactive.Linq;
using FluentAssertions;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet-repl.Tests/ReplInteractionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public abstract class ReplInteractionTests : IDisposable
protected ReplInteractionTests(ITestOutputHelper output)
{
Kernel = Repl.CreateKernel(new StartupOptions("csharp"));
AnsiConsole = new AnsiConsoleFactory().Create(new AnsiConsoleSettings

AnsiConsole = Spectre.Console.AnsiConsole.Create(new AnsiConsoleSettings
{
Out = new AnsiConsoleOutput(Out),
Ansi = AnsiSupport.Yes
Expand Down
5 changes: 2 additions & 3 deletions src/dotnet-repl.Tests/StartWithNotebookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
namespace dotnet_repl.Tests;

public class StartWithNotebookTests : IDisposable

{
private readonly IAnsiConsole _ansiConsole;

private readonly StringWriter _writer;

public StartWithNotebookTests()
{
_ansiConsole = new AnsiConsoleFactory().Create(new AnsiConsoleSettings
_ansiConsole = AnsiConsole.Create(new AnsiConsoleSettings
{
Ansi = AnsiSupport.Yes,
Interactive = InteractionSupport.Yes,
Expand All @@ -30,7 +29,7 @@ public StartWithNotebookTests()

public void Dispose() => _writer.Dispose();

[Fact(Skip = "Later")]
[Fact]
public async Task when_an_ipynb_is_specified_it_runs_it()
{
using var disposables = new CompositeDisposable();
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-repl.Tests/Utility/TestUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
using FluentAssertions.Execution;
using FluentAssertions.Primitives;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Parsing;
using Microsoft.DotNet.Interactive.Server;
using Newtonsoft.Json;

namespace dotnet_repl.Tests;
Expand Down
10 changes: 8 additions & 2 deletions src/dotnet-repl.Tests/dotnet-repl.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="fluentassertions" Version="6.6.0" />
<PackageReference Include="fluentassertions" Version="6.7.0" />
<PackageReference Include="microsoft.extensions.dependencyinjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="pocketlogger.for.xunit" Version="0.6.1">
Expand All @@ -20,7 +20,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -34,4 +34,10 @@
<ProjectReference Include="..\dotnet-repl\dotnet-repl.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="test.ipynb">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
128 changes: 72 additions & 56 deletions src/dotnet-repl.Tests/test.ipynb
Original file line number Diff line number Diff line change
@@ -1,61 +1,77 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"outputs": [
"cells": [
{
"data": {
"text/plain": "Hello from C#"
},
"output_type": "unknown"
}
],
"source": [
"Console.Write(\"Hello from C#\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
}
},
"outputs": [
"cell_type": "markdown",
"metadata": {},
"source": [
"# Here is a Markdown heading\n",
"\n",
"And here is some text."
]
},
{
"data": {
"text/plain": "Hello from F#"
},
"output_type": "unknown"
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"vscode": {
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello from C#"
]
}
],
"source": [
"Console.Write(\"Hello from C#\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "dotnet-interactive.fsharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello from F#"
]
}
],
"source": [
"\"Hello from F#\" |> Console.Write"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
],
"source": [
"\"Hello from F#\" |> Console.Write"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "9.0"
},
"orig_nbformat": 2
},
"nbformat": 4,
"nbformat_minor": 2
}
"nbformat": 4,
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion src/dotnet-repl/AnsiConsoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static IRenderable GetErrorDisplay(
CommandFailed failed,
KernelSpecificTheme theme,
string header = "❌") =>
new Panel(failed.Exception.GetRenderable())
new Panel(failed.Exception?.GetRenderable() ?? new Text(failed.Message ?? ""))
.Header(header)
.Expand()
.RoundedBorder()
Expand Down
15 changes: 15 additions & 0 deletions src/dotnet-repl/Bind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.CommandLine.Binding;

namespace dotnet_repl;

internal static class Bind
{
public static ServiceProviderBinder<T> FromServiceProvider<T>() => ServiceProviderBinder<T>.Instance;

internal class ServiceProviderBinder<T> : BinderBase<T>
{
public static ServiceProviderBinder<T> Instance { get; } = new();

protected override T GetBoundValue(BindingContext bindingContext) => (T)bindingContext.GetService(typeof(T));
}
}
31 changes: 20 additions & 11 deletions src/dotnet-repl/CommandLineParser.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Documents;
using Pocket;
Expand Down Expand Up @@ -51,7 +51,7 @@ public static class CommandLineParser

public static Parser Create(
IAnsiConsole? ansiConsole = null,
Func<StartupOptions, IAnsiConsole, CancellationToken, Task<IDisposable>>? startRepl = null,
Func<StartupOptions, IAnsiConsole, InvocationContext, Task<IDisposable>>? startRepl = null,
Action<IDisposable>? registerForDisposal = null)
{
var rootCommand = new RootCommand("dotnet-repl")
Expand All @@ -65,13 +65,19 @@ public static Parser Create(

startRepl ??= StartRepl;

rootCommand.SetHandler<StartupOptions, CancellationToken>(
(options, token) =>
rootCommand.SetHandler(
async (options, context) =>
{
var repl = startRepl(options, ansiConsole ?? AnsiConsole.Console, token);
registerForDisposal?.Invoke(repl);
return repl;
}, new StartupOptionsBinder(DefaultKernelOption, WorkingDirOption, NotebookOption, LogPathOption, ExitAfterRun));
var disposable = await startRepl(options, ansiConsole ?? AnsiConsole.Console, context);
registerForDisposal?.Invoke(disposable);
},
new StartupOptionsBinder(
DefaultKernelOption,
WorkingDirOption,
NotebookOption,
LogPathOption,
ExitAfterRun),
Bind.FromServiceProvider<InvocationContext>());

return new CommandLineBuilder(rootCommand)
.UseDefaults()
Expand All @@ -82,7 +88,7 @@ public static Parser Create(
public static async Task<IDisposable> StartRepl(
StartupOptions options,
IAnsiConsole ansiConsole,
CancellationToken cancellationToken)
InvocationContext context)
{
var theme = KernelSpecificTheme.GetTheme(options.DefaultKernelName);

Expand All @@ -109,9 +115,12 @@ public static async Task<IDisposable> StartRepl(
disposable.Add(repl);
disposable.Add(kernel);

cancellationToken.Register(() => disposable.Dispose());
context.GetCancellationToken().Register(() => disposable.Dispose());

await repl.RunAsync(notebook, options.ExitAfterRun);
await repl.RunAsync(
i => context.ExitCode = i,
notebook,
options.ExitAfterRun);

return disposable;
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-repl/DocumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static async Task<InteractiveDocument> ReadFileAsInteractiveDocument(
FileInfo file,
CompositeKernel kernel)
{
using var stream = file.OpenRead();
await using var stream = file.OpenRead();

List<KernelName> kernelNames = new();

Expand Down
34 changes: 19 additions & 15 deletions src/dotnet-repl/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static T UseAboutMagicCommand<T>(this T kernel)
{
var about = new Command("#!about", "Show version and build information");

about.SetHandler<KernelInvocationContext>(context => context.Display(VersionSensor.Version()));
about.SetHandler(context => context.Display(VersionSensor.Version()),
Bind.FromServiceProvider<KernelInvocationContext>());

kernel.AddDirective(about);

Expand All @@ -45,24 +46,27 @@ public static TKernel UseDebugDirective<TKernel>(this TKernel kernel)
{
var debug = new Command("#!debug");

debug.SetHandler<KernelInvocationContext, IConsole, CancellationToken>(async (context, console, cancellationToken) =>
{
await Attach();
debug.SetHandler(async (context, console, cancellationToken) =>
{
await Attach();

async Task Attach()
{
var process = Process.GetCurrentProcess();
async Task Attach()
{
var process = Process.GetCurrentProcess();

var processId = process.Id;
var processId = process.Id;

context.Display($"Attach your debugger to process {processId} ({process.ProcessName}).");
context.Display($"Attach your debugger to process {processId} ({process.ProcessName}).");

while (!Debugger.IsAttached)
{
await Task.Delay(500, cancellationToken);
}
}
});
while (!Debugger.IsAttached)
{
await Task.Delay(500, cancellationToken);
}
}
},
Bind.FromServiceProvider<KernelInvocationContext>(),
Bind.FromServiceProvider<IConsole>(),
Bind.FromServiceProvider<CancellationToken>());

kernel.AddDirective(debug);

Expand Down
Loading

0 comments on commit 82d7df4

Please sign in to comment.