Skip to content

Commit

Permalink
scan all kernels to find the nuget package
Browse files Browse the repository at this point in the history
  • Loading branch information
colombod committed Sep 16, 2021
1 parent 5ab5eb7 commit 41f0f9c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 59 deletions.
33 changes: 3 additions & 30 deletions src/Microsoft.DotNet.Interactive.Kql/KqlKernelConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,10 @@ public override async Task<Kernel> CreateKernelAsync(
KernelInvocationContext context)
{
var connectionDetails = await BuildConnectionDetailsAsync(options);

var resolvedPackageReferences = ((ISupportNuget)context.HandlingKernel).ResolvedPackageReferences;
// Walk through the packages looking for the package that endswith the name "Microsoft.SqlToolsService"
// and grab the packageroot
var runtimePackageIdSuffix = "native.Microsoft.SqlToolsService";
var root = resolvedPackageReferences.FirstOrDefault(p => p.PackageName.EndsWith(runtimePackageIdSuffix, StringComparison.OrdinalIgnoreCase));
string pathToService = "";

if (root is not null)
{
// Extract the platform 'osx-x64' from the package name 'runtime.osx-x64.native.microsoft.sqltoolsservice'
string[] packageNameSegments = root.PackageName.Split(".");
if (packageNameSegments.Length > 2)
{
string platform = packageNameSegments[1];

// Build the path to the MicrosoftSqlToolsServiceLayer executable by reaching into the resolve nuget package
// assuming a convention for native binaries.
pathToService = Path.Combine(
root.PackageRoot,
"runtimes",
platform,
"native",
"MicrosoftKustoServiceLayer");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
pathToService += ".exe";
}
}
}
var root = Kernel.Root.FindResolvedPackageReference();

var pathToService = root.PathToService("MicrosoftKustoServiceLayer");

var sqlClient = new ToolsServiceClient(pathToService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Compile Include="..\Microsoft.DotNet.Interactive.SqlServer\MsSqlRpcObjects.cs">
<Link>MsSqlRpcObjects.cs</Link>
</Compile>
<Compile Include="..\Microsoft.DotNet.Interactive.SqlServer\SqlToolsServiceDiscovery.cs" Link="SqlToolsServiceDiscovery.cs" />
<Compile Include="..\Microsoft.DotNet.Interactive.SqlServer\ToolsServiceClient.cs">
<Link>ToolsServiceClient.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,10 @@ public override async Task<Kernel> CreateKernelAsync(
MsSqlConnectionOptions options,
KernelInvocationContext context)
{
var resolvedPackageReferences = ((ISupportNuget)context.HandlingKernel).ResolvedPackageReferences;
// Walk through the packages looking for the package that endswith the name "Microsoft.SqlToolsService"
// and grab the packageroot
var runtimePackageIdSuffix = "native.Microsoft.SqlToolsService";
var root = resolvedPackageReferences.FirstOrDefault(p => p.PackageName.EndsWith(runtimePackageIdSuffix, StringComparison.OrdinalIgnoreCase));
string pathToService = "";
if (root is not null)
{
// Extract the platform 'osx-x64' from the package name 'runtime.osx-x64.native.microsoft.sqltoolsservice'
string[] packageNameSegments = root.PackageName.Split(".");
if (packageNameSegments.Length > 2)
{
string platform = packageNameSegments[1];

// Build the path to the MicrosoftSqlToolsServiceLayer executable by reaching into the resolve nuget package
// assuming a convention for native binaries.
pathToService = Path.Combine(
root.PackageRoot,
"runtimes",
platform,
"native",
"MicrosoftSqlToolsServiceLayer");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
pathToService += ".exe";
}
}
}
var root = Kernel.Root.FindResolvedPackageReference();

var pathToService = root.PathToService("MicrosoftSqlToolsServiceLayer");

var sqlClient = new ToolsServiceClient(pathToService, $"--parent-pid {Process.GetCurrentProcess().Id}");

var kernel = new MsSqlKernel(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;

namespace Microsoft.DotNet.Interactive.SqlServer
{
internal static class SqlToolsServiceDiscovery
{
public static ResolvedPackageReference FindResolvedPackageReference(this Kernel rootKernel)
{
var runtimePackageIdSuffix = "native.Microsoft.SqlToolsService";
foreach (var kernel in rootKernel.SubkernelsAndSelf().OfType<ISupportNuget>())
{
var resolvedPackageReferences = kernel.ResolvedPackageReferences;

// Walk through the packages looking for the package that ends with the name "Microsoft.SqlToolsService"
// and grab the packageroot

var resolved = resolvedPackageReferences.FirstOrDefault(p => p.PackageName.EndsWith(runtimePackageIdSuffix, StringComparison.OrdinalIgnoreCase));

if (resolved is { })
{
return resolved;
}

}
;
return null;
}

public static string PathToService(this ResolvedPackageReference resolvedPackageReference, string serviceName)
{
var pathToService = "";
if (resolvedPackageReference is not null)
{
// Extract the platform 'osx-x64' from the package name 'runtime.osx-x64.native.microsoft.sqltoolsservice'
string[] packageNameSegments = resolvedPackageReference.PackageName.Split(".");
if (packageNameSegments.Length > 2)
{
string platform = packageNameSegments[1];

// Build the path to the MicrosoftSqlToolsServiceLayer executable by reaching into the resolve nuget package
// assuming a convention for native binaries.
pathToService = Path.Combine(
resolvedPackageReference.PackageRoot,
"runtimes",
platform,
"native",
serviceName);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
pathToService += ".exe";
}
}
}

return pathToService;
}
}
}
17 changes: 17 additions & 0 deletions src/Microsoft.DotNet.Interactive/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,5 +424,22 @@ public static async Task VisitSubkernelsAndSelfAsync(

await VisitSubkernelsAsync(kernel, onVisit, recursive);
}

public static IEnumerable<Kernel> SubkernelsAndSelf(
this Kernel kernel)
{
yield return kernel;

if (kernel is CompositeKernel compositeKernel)
{
foreach (var subKernel in compositeKernel.ChildKernels)
{
foreach (var recursive in subKernel.SubkernelsAndSelf())
{
yield return recursive;
}
}
}
}
}
}

0 comments on commit 41f0f9c

Please sign in to comment.