这是indexloc提供的服务,不要输入任何密码
Skip to content
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
17 changes: 17 additions & 0 deletions MLS.Agent.Tests/ApiViaHttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using WorkspaceServer.Tests.TestUtility;
using CodeManipulation = WorkspaceServer.Tests.CodeManipulation;
using SourceFile = Microsoft.DotNet.Try.Protocol.ClientApi.SourceFile;
using MLS.Agent.Tools;

namespace MLS.Agent.Tests
{
Expand Down Expand Up @@ -823,6 +824,22 @@ public async Task Can_serve_from_webassembly_controller()
result = await response.Content.ReadAsStringAsync();
result.Should().Contain("DotNet.invokeMethodAsync");
}

// Now do the same thing in hosted mode using the already installed package
using (var agent = new AgentService(StartupOptions.FromCommandLine("hosted")))
{
var response = await agent.GetAsync($@"/LocalCodeRunner/{name}");

response.EnsureSuccess();
var result = await response.Content.ReadAsStringAsync();
result.Should().Contain("Loading...");

response = await agent.GetAsync($@"/LocalCodeRunner/{name}/interop.js");

response.EnsureSuccess();
result = await response.Content.ReadAsStringAsync();
result.Should().Contain("DotNet.invokeMethodAsync");
}
}

[Fact]
Expand Down
6 changes: 4 additions & 2 deletions WorkspaceServer/PackageRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private Task<IPackage> GetPackageFromPackageBuilder(string packageName, Budget b

public static PackageRegistry CreateForTryMode(DirectoryInfo project, PackageSource addSource = null)
{
var finders = GetDefaultPackageFinders().Append(new WebAssemblyAssetFinder(Package.DefaultPackagesDirectory, addSource));
var finders = GetDefaultPackageFinders().Append(new PackageInstallingWebAssemblyAssetFinder(Package.DefaultPackagesDirectory, addSource));
var registry = new PackageRegistry(
true,
addSource,
Expand All @@ -169,8 +169,10 @@ public static PackageRegistry CreateForTryMode(DirectoryInfo project, PackageSou

public static PackageRegistry CreateForHostedMode()
{
var finders = GetDefaultPackageFinders().Append(new WebAssemblyAssetFinder(Package.DefaultPackagesDirectory));
var registry = new PackageRegistry(
false);
createRebuildablePackages: false,
packageFinders: finders);

registry.Add("console",
packageBuilder =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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.IO;
using System.Threading.Tasks;
using Clockwise;
using Pocket;
using WorkspaceServer.WorkspaceFeatures;

namespace WorkspaceServer.Packaging
{
public class PackageInstallingWebAssemblyAssetFinder : WebAssemblyAssetFinder, IPackageFinder
{
private readonly PackageSource _addSource;

public PackageInstallingWebAssemblyAssetFinder(DirectoryInfo workingDirectory, PackageSource addSource = null)
: base(workingDirectory)
{
_addSource = addSource;
}

async Task<TPackage> IPackageFinder.Find<TPackage>(PackageDescriptor descriptor)
{
if (descriptor.IsPathSpecified)
{
return null;
}

var candidate = new PackageTool(descriptor.Name, _workingDirectory);
if (candidate.Exists)
{
var package = await CreatePackage(descriptor, candidate);
return package as TPackage;
}

return await TryInstallAndLocateTool(descriptor) as TPackage;
}

private async Task<IPackage> TryInstallAndLocateTool(PackageDescriptor packageDesciptor)
{
var dotnet = new Dotnet();

var installationResult = await dotnet.ToolInstall(
packageDesciptor.Name,
_workingDirectory,
_addSource,
new Budget());

if (installationResult.ExitCode != 0)
{
Logger<LocalToolInstallingPackageDiscoveryStrategy>.Log.Warning($"Tool not installed: {packageDesciptor.Name}");
return null;
}

var tool = new PackageTool(packageDesciptor.Name, _workingDirectory);
if (tool.Exists)
{
return await CreatePackage(packageDesciptor, tool);
}

return null;
}
}
}
38 changes: 4 additions & 34 deletions WorkspaceServer/Packaging/WebAssemblyAssetFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Clockwise;
using Pocket;
using WorkspaceServer.WorkspaceFeatures;

namespace WorkspaceServer.Packaging
{
public class WebAssemblyAssetFinder : IPackageFinder
{
private readonly DirectoryInfo _workingDirectory;
private readonly ToolPackageLocator _locator;
private readonly PackageSource _addSource;
protected readonly DirectoryInfo _workingDirectory;

public WebAssemblyAssetFinder(DirectoryInfo workingDirectory, PackageSource addSource = null)
public WebAssemblyAssetFinder(DirectoryInfo workingDirectory)
{
_workingDirectory = workingDirectory;
_locator = new ToolPackageLocator(workingDirectory);
_addSource = addSource;
}

async Task<TPackage> IPackageFinder.Find<TPackage>(PackageDescriptor descriptor)
Expand All @@ -37,35 +31,10 @@ async Task<TPackage> IPackageFinder.Find<TPackage>(PackageDescriptor descriptor)
return package as TPackage;
}

return (await TryInstallAndLocateTool(descriptor) as TPackage);
}

private async Task<IPackage> TryInstallAndLocateTool(PackageDescriptor packageDesciptor)
{
var dotnet = new Dotnet();

var installationResult = await dotnet.ToolInstall(
packageDesciptor.Name,
_workingDirectory,
_addSource,
new Budget());

if (installationResult.ExitCode != 0)
{
Logger<LocalToolInstallingPackageDiscoveryStrategy>.Log.Warning($"Tool not installed: {packageDesciptor.Name}");
return null;
}

var tool = new PackageTool(packageDesciptor.Name, _workingDirectory);
if (tool.Exists)
{
return await CreatePackage(packageDesciptor, tool);
}

return null;
}

private async Task<IPackage> CreatePackage(PackageDescriptor descriptor, PackageTool tool)
protected async Task<IPackage> CreatePackage(PackageDescriptor descriptor, PackageTool tool)
{
await tool.Prepare();
var wasmAsset = await tool.LocateWasmAsset();
Expand All @@ -78,5 +47,6 @@ private async Task<IPackage> CreatePackage(PackageDescriptor descriptor, Package

return null;
}

}
}