Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a couple small API cleanups #3193

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ Microsoft.DotNet.Interactive
public System.Threading.Tasks.Task EndInvoke(System.IAsyncResult result)
public System.Threading.Tasks.Task Invoke(Microsoft.DotNet.Interactive.Commands.KernelCommand command, KernelInvocationContext context, KernelPipelineContinuation next)
public class KernelCommandResult
.ctor(Microsoft.DotNet.Interactive.Commands.KernelCommand command)
public Microsoft.DotNet.Interactive.Commands.KernelCommand Command { get;}
public System.Collections.Generic.IReadOnlyList<Microsoft.DotNet.Interactive.Events.KernelEvent> Events { get;}
public class KernelCommandScheduler : KernelScheduler<Microsoft.DotNet.Interactive.Commands.KernelCommand,KernelCommandResult>, IKernelScheduler<Microsoft.DotNet.Interactive.Commands.KernelCommand,KernelCommandResult>, System.IDisposable
Expand Down Expand Up @@ -390,7 +389,6 @@ Microsoft.DotNet.Interactive.Commands
public System.CommandLine.Parsing.ParseResult KernelChooserParseResult { get;}
public System.Uri OriginUri { get; set;}
public KernelCommand Parent { get;}
public System.Collections.Generic.IDictionary<System.String,System.Object> Properties { get;}
public Microsoft.DotNet.Interactive.CommandRoutingSlip RoutingSlip { get;}
public System.String TargetKernelName { get;}
public System.Boolean Equals(KernelCommand other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public void All_command_types_are_round_trip_serializable(KernelCommand command)
.Should()
.BeEquivalentToRespectingRuntimeTypes(
originalEnvelope,
o => o.Excluding(e => e.Command.Properties)
.Excluding(e => e.Command.Handler));
o => o.Excluding(e => e.Command.Handler));
}

[Theory]
Expand Down Expand Up @@ -84,8 +83,7 @@ public void All_event_types_are_round_trip_serializable(KernelEvent @event)
.Should()
.BeEquivalentToRespectingRuntimeTypes(
originalEnvelope,
o => o.Excluding(envelope => envelope.Event.Command.Properties)
.Excluding(memberInfo => ignoredProperties.Contains($"{memberInfo.DeclaringType.Name}.{memberInfo.Name}"))
o => o.Excluding(memberInfo => ignoredProperties.Contains($"{memberInfo.DeclaringType.Name}.{memberInfo.Name}"))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public static T UseNugetDirective<T>(this T kernel, bool useResultsCache = true)
return kernel;
}

private static readonly string installPackagesPropertyName = "commandIHandler.InstallPackages";

private static Command i()
{
var iDirective = new Command("#i")
Expand Down Expand Up @@ -152,20 +150,7 @@ private static bool EndsInDirectorySeparator(string path)
{
return path.Length > 0 && path.EndsWith(Path.DirectorySeparatorChar);
}

private static void CreateOrUpdateDisplayValue(KernelInvocationContext context, string name, object content)
{
if (!context.Command.Properties.TryGetValue(name, out var displayed))
{
displayed = context.Display(content);
context.Command.Properties.Add(name, displayed);
}
else
{
(displayed as DisplayedValue)?.Update(content);
}
}


internal static KernelCommandInvocation DoNugetRestore()
{
return async (_, invocationContext) =>
Expand All @@ -183,8 +168,8 @@ async Task Restore(KernelInvocationContext context)

var installMessage = new InstallPackagesMessage(requestedSources, requestedPackages, Array.Empty<string>(), 0);

CreateOrUpdateDisplayValue(context, installPackagesPropertyName, installMessage);

var displayedValue = context.Display(installMessage);
var restorePackagesTask = kernel.RestoreAsync();
var delay = 500;
while (await Task.WhenAny(Task.Delay(delay), restorePackagesTask) != restorePackagesTask)
Expand All @@ -195,7 +180,8 @@ async Task Restore(KernelInvocationContext context)
}

installMessage.Progress++;
CreateOrUpdateDisplayValue(context, installPackagesPropertyName, installMessage);

displayedValue.Update(installMessage);
}

var result = await restorePackagesTask;
Expand All @@ -204,10 +190,10 @@ async Task Restore(KernelInvocationContext context)
requestedSources,
Array.Empty<string>(),
kernel.ResolvedPackageReferences
.Where(r => requestedPackages.Contains(r.PackageName, StringComparer.OrdinalIgnoreCase))
.Select(s => $"{s.PackageName}, {s.PackageVersion}")
.OrderBy(s => s)
.ToList(),
.Where(r => requestedPackages.Contains(r.PackageName, StringComparer.OrdinalIgnoreCase))
.Select(s => $"{s.PackageName}, {s.PackageVersion}")
.OrderBy(s => s)
.ToList(),
0);

if (result.Succeeded)
Expand All @@ -218,12 +204,12 @@ async Task Restore(KernelInvocationContext context)
context.Publish(new PackageAdded(resolvedReference, context.Command));
}

CreateOrUpdateDisplayValue(context, installPackagesPropertyName, resultMessage);
displayedValue.Update(resultMessage);
}
else
{
var errors = string.Join(Environment.NewLine, result.Errors);
CreateOrUpdateDisplayValue(context, installPackagesPropertyName, resultMessage);
displayedValue.Update(resultMessage);
context.Fail(context.Command, message: errors);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ public void All_command_types_are_round_trip_serializable(KernelCommand command)
.Should()
.BeEquivalentToRespectingRuntimeTypes(
originalEnvelope,
o => o.Excluding(e => e.Command.Properties)
.Excluding(e => e.Command.Handler)
.Excluding(info => ignoredProperties.Contains($"{info.DeclaringType.Name}.{info.Name}")));
o => o.Excluding(e => e.Command.Handler)
.Excluding(info => ignoredProperties.Contains($"{info.DeclaringType.Name}.{info.Name}")));
}

[Theory]
Expand Down Expand Up @@ -85,8 +84,7 @@ public void All_event_types_are_round_trip_serializable(KernelEvent @event)
.Should()
.BeEquivalentToRespectingRuntimeTypes(
originalEnvelope,
o => o.Excluding(envelope => envelope.Event.Command.Properties)
.Excluding(info => ignoredProperties.Contains($"{info.DeclaringType.Name}.{info.Name}")));
o => o.Excluding(info => ignoredProperties.Contains($"{info.DeclaringType.Name}.{info.Name}")));
}

[Theory]
Expand Down
8 changes: 1 addition & 7 deletions src/Microsoft.DotNet.Interactive/Commands/KernelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.CommandLine.Parsing;
using System.Threading.Tasks;
using System.Text.Json.Serialization;
Expand All @@ -15,10 +14,8 @@ public abstract class KernelCommand : IEquatable<KernelCommand>
private KernelCommand _parent;
private string _token;

protected KernelCommand(
string targetKernelName = null)
protected KernelCommand(string targetKernelName = null)
{
Properties = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
TargetKernelName = targetKernelName;
RoutingSlip = new CommandRoutingSlip();
}
Expand Down Expand Up @@ -52,9 +49,6 @@ public void SetParent(KernelCommand parent)
}
}

[JsonIgnore]
public IDictionary<string, object> Properties { get; }

public string TargetKernelName { get; internal set; }

internal static KernelCommand None => new NoCommand();
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.Interactive/KernelCommandResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class KernelCommandResult
{
private readonly List<KernelEvent> _events = new();

public KernelCommandResult(KernelCommand command)
internal KernelCommandResult(KernelCommand command)
{
Command = command;
}
Expand Down