From 451f77d86a4ee36a39fbdeab30d2d164237421af Mon Sep 17 00:00:00 2001 From: Ravi Chande Date: Mon, 20 May 2019 17:11:22 -0700 Subject: [PATCH 1/5] Introduce packageSource type --- .../CommandLine/CommandLineParserTests.cs | 5 +-- .../CommandLine/PackCommandTests.cs | 2 +- .../LocalToolPackageDiscoveryStrategyTests.cs | 3 +- .../CodeBlockAnnotationExtensionTests.cs | 2 +- MLS.Agent.Tests/WorkspaceDiscoveryTests.cs | 3 +- MLS.Agent/CommandLine/InstallOptions.cs | 5 +-- MLS.Agent/CommandLine/StartupOptions.cs | 5 +-- WorkspaceServer.Tests/Create.cs | 2 +- .../PrebuiltBlazorPackageLocatorTests.cs | 2 +- WorkspaceServer/Dotnet.cs | 2 +- WorkspaceServer/PackageRegistry.cs | 9 +++-- WorkspaceServer/PackageSource.cs | 35 +++++++++++++++++++ ...lToolInstallingPackageDiscoveryStrategy.cs | 4 +-- .../Packaging/WebAssemblyAssetFinder.cs | 4 +-- 14 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 WorkspaceServer/PackageSource.cs diff --git a/MLS.Agent.Tests/CommandLine/CommandLineParserTests.cs b/MLS.Agent.Tests/CommandLine/CommandLineParserTests.cs index 4bf33db2f..1c03a38fe 100644 --- a/MLS.Agent.Tests/CommandLine/CommandLineParserTests.cs +++ b/MLS.Agent.Tests/CommandLine/CommandLineParserTests.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using FluentAssertions; using MLS.Agent.CommandLine; +using WorkspaceServer; using WorkspaceServer.Tests.TestUtility; using Xunit; using Xunit.Abstractions; @@ -23,7 +24,7 @@ public class CommandLineParserTests : IDisposable private TryGitHubOptions _tryGitHubOptions; private PackOptions _packOptions; private InstallOptions _installOptions; - private DirectoryInfo _install_packageSource; + private PackageSource _install_packageSource; private VerifyOptions _verifyOptions; private DemoOptions _demoOptions; private JupyterOptions _jupyter_Options; @@ -305,7 +306,7 @@ public async Task Install_parses_source_option() await _parser.InvokeAsync($"install --add-source {expectedPackageSource} the-package", console); _installOptions.PackageName.Should().Be("the-package"); - _install_packageSource.FullName.Should().Be(expectedPackageSource); + _install_packageSource.ToString().Should().Be(expectedPackageSource); } [Fact] diff --git a/MLS.Agent.Tests/CommandLine/PackCommandTests.cs b/MLS.Agent.Tests/CommandLine/PackCommandTests.cs index 256b4ffcb..b8e175480 100644 --- a/MLS.Agent.Tests/CommandLine/PackCommandTests.cs +++ b/MLS.Agent.Tests/CommandLine/PackCommandTests.cs @@ -64,7 +64,7 @@ public async Task Pack_project_blazor_contents() var dotnet = new Dotnet(asset.Directory); - var result = await dotnet.ToolInstall(packageName, asset.Directory, asset.Directory); + var result = await dotnet.ToolInstall(packageName, asset.Directory, new PackageSource(asset.Directory.FullName)); var exe = Path.Combine(asset.Directory.FullName, packageName); diff --git a/MLS.Agent.Tests/LocalToolPackageDiscoveryStrategyTests.cs b/MLS.Agent.Tests/LocalToolPackageDiscoveryStrategyTests.cs index be19ca3ef..5e6235225 100644 --- a/MLS.Agent.Tests/LocalToolPackageDiscoveryStrategyTests.cs +++ b/MLS.Agent.Tests/LocalToolPackageDiscoveryStrategyTests.cs @@ -9,6 +9,7 @@ using WorkspaceServer.Tests; using Xunit; using Xunit.Abstractions; +using WorkspaceServer; namespace MLS.Agent.Tests { @@ -58,7 +59,7 @@ public async Task Installs_tool_from_package_source_when_requested() var console = new TestConsole(); var asset = await LocalToolHelpers.CreateTool(console); - var strategy = new LocalToolInstallingPackageDiscoveryStrategy(asset, asset); + var strategy = new LocalToolInstallingPackageDiscoveryStrategy(asset, new PackageSource(asset.FullName)); var package = await strategy.Locate(new PackageDescriptor("blazor-console")); package.Should().NotBeNull(); } diff --git a/MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs b/MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs index d52d8dd54..7d00a9fe3 100644 --- a/MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs +++ b/MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs @@ -30,7 +30,7 @@ public CodeBlockAnnotationExtensionTests() var console = new TestConsole(); _package = new AsyncLazy<(PackageRegistry, string)>( async () => { var dir = await LocalToolHelpers.CreateTool(console); - var strategy = new LocalToolInstallingPackageDiscoveryStrategy(dir, dir); + var strategy = new LocalToolInstallingPackageDiscoveryStrategy(dir, new PackageSource(dir.FullName)); return (new PackageRegistry(true, null, additionalStrategies: strategy), "console"); } ); diff --git a/MLS.Agent.Tests/WorkspaceDiscoveryTests.cs b/MLS.Agent.Tests/WorkspaceDiscoveryTests.cs index 44407c22c..67f7f203c 100644 --- a/MLS.Agent.Tests/WorkspaceDiscoveryTests.cs +++ b/MLS.Agent.Tests/WorkspaceDiscoveryTests.cs @@ -16,6 +16,7 @@ using Xunit.Abstractions; using Buffer = Microsoft.DotNet.Try.Protocol.Buffer; using File = Microsoft.DotNet.Try.Protocol.File; +using WorkspaceServer; namespace MLS.Agent.Tests { @@ -34,7 +35,7 @@ public async Task Local_tool_workspace_can_be_discovered() var output = Guid.NewGuid().ToString(); var requestJson = Create.SimpleWorkspaceRequestAsJson(output, packageName); - var response = await CallRun(requestJson, options: new StartupOptions(addPackageSource: packageLocation, dir: new DirectoryInfo(Directory.GetCurrentDirectory()))); + var response = await CallRun(requestJson, options: new StartupOptions(addPackageSource: new PackageSource(packageLocation.FullName), dir: new DirectoryInfo(Directory.GetCurrentDirectory()))); var result = await response .EnsureSuccess() .DeserializeAs(); diff --git a/MLS.Agent/CommandLine/InstallOptions.cs b/MLS.Agent/CommandLine/InstallOptions.cs index 137bc3a29..ae6457183 100644 --- a/MLS.Agent/CommandLine/InstallOptions.cs +++ b/MLS.Agent/CommandLine/InstallOptions.cs @@ -2,20 +2,21 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.IO; +using WorkspaceServer; using WorkspaceServer.Packaging; namespace MLS.Agent.CommandLine { public class InstallOptions { - public InstallOptions(DirectoryInfo addSource, string packageName, DirectoryInfo location = null) + public InstallOptions(PackageSource addSource, string packageName, DirectoryInfo location = null) { AddSource = addSource; PackageName = packageName; Location = location ?? Package.DefaultPackagesDirectory; } - public DirectoryInfo AddSource { get; } + public PackageSource AddSource { get; } public string PackageName { get; } public DirectoryInfo Location { get; } diff --git a/MLS.Agent/CommandLine/StartupOptions.cs b/MLS.Agent/CommandLine/StartupOptions.cs index ac3b88c87..9883f8cdf 100644 --- a/MLS.Agent/CommandLine/StartupOptions.cs +++ b/MLS.Agent/CommandLine/StartupOptions.cs @@ -6,6 +6,7 @@ using System.CommandLine.Invocation; using System.IO; using Microsoft.DotNet.Try.Markdown; +using WorkspaceServer; namespace MLS.Agent.CommandLine { @@ -34,7 +35,7 @@ public StartupOptions( string id = null, string regionId = null, DirectoryInfo dir = null, - DirectoryInfo addPackageSource = null, + PackageSource addPackageSource = null, Uri uri = null, DirectoryInfo logPath = null, bool verbose = false, @@ -64,7 +65,7 @@ public StartupOptions( public string Id { get; } public string RegionId { get; } public DirectoryInfo Dir { get; } - public DirectoryInfo AddPackageSource { get; } + public PackageSource AddPackageSource { get; } public Uri Uri { get; set; } public bool Production { get; } public bool IsLanguageService { get; set; } diff --git a/WorkspaceServer.Tests/Create.cs b/WorkspaceServer.Tests/Create.cs index 45c3ff1f4..d4db9a38a 100644 --- a/WorkspaceServer.Tests/Create.cs +++ b/WorkspaceServer.Tests/Create.cs @@ -71,7 +71,7 @@ public static async Task InstalledPackageWithBlazorEnabled([CallerMemb { var (packageName, addSource) = await NupkgWithBlazorEnabled(testName); var destination = Package.DefaultPackagesDirectory; - await InstallCommand.Do(new InstallOptions(addSource, packageName, destination), new TestConsole()); + await InstallCommand.Do(new InstallOptions(new PackageSource(addSource.FullName), packageName, destination), new TestConsole()); var strategy = new WebAssemblyAssetFinder(destination); return await strategy.Find(packageName); diff --git a/WorkspaceServer.Tests/PrebuiltBlazorPackageLocatorTests.cs b/WorkspaceServer.Tests/PrebuiltBlazorPackageLocatorTests.cs index 21aca6eda..a6d67ea45 100644 --- a/WorkspaceServer.Tests/PrebuiltBlazorPackageLocatorTests.cs +++ b/WorkspaceServer.Tests/PrebuiltBlazorPackageLocatorTests.cs @@ -28,7 +28,7 @@ public PrebuiltBlazorPackageLocatorTests(ITestOutputHelper output) public async Task Discovers_built_blazor_package() { var (packageName, addSource) = await Create.NupkgWithBlazorEnabled(); - await InstallCommand.Do(new InstallOptions(addSource, packageName), new TestConsole()); + await InstallCommand.Do(new InstallOptions(new PackageSource(addSource.FullName), packageName), new TestConsole()); var locator = new PrebuiltBlazorPackageLocator(); var asset = await locator.Locate(packageName); diff --git a/WorkspaceServer/Dotnet.cs b/WorkspaceServer/Dotnet.cs index 12a7e81f1..ba7375a06 100644 --- a/WorkspaceServer/Dotnet.cs +++ b/WorkspaceServer/Dotnet.cs @@ -90,7 +90,7 @@ public async Task> ToolList(DirectoryInfo directory, Budget public Task ToolInstall( string packageName, DirectoryInfo toolPath, - DirectoryInfo addSource = null, + PackageSource addSource = null, Budget budget = null) { var args = $@"{packageName} --tool-path ""{toolPath.FullName}"" --version 1.0.0"; diff --git a/WorkspaceServer/PackageRegistry.cs b/WorkspaceServer/PackageRegistry.cs index 8822015b6..204cb1711 100644 --- a/WorkspaceServer/PackageRegistry.cs +++ b/WorkspaceServer/PackageRegistry.cs @@ -26,7 +26,7 @@ public class PackageRegistry : public PackageRegistry( bool createRebuildablePackages = false, - DirectoryInfo addSource = null, + PackageSource addSource = null, IEnumerable packageFinders = null, params IPackageDiscoveryStrategy[] additionalStrategies) : this(addSource, @@ -39,7 +39,7 @@ public PackageRegistry( } private PackageRegistry( - DirectoryInfo addSource, + PackageSource addSource, IEnumerable strategies, IEnumerable packageFinders = null) { @@ -134,7 +134,7 @@ private Task GetPackageFromPackageBuilder(string packageName, Budge }); } - public static PackageRegistry CreateForTryMode(DirectoryInfo project, DirectoryInfo addSource = null) + public static PackageRegistry CreateForTryMode(DirectoryInfo project, PackageSource addSource = null) { var registry = new PackageRegistry( true, @@ -235,12 +235,11 @@ public IEnumerator> GetEnumerator() => IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - private static IEnumerable GetDefaultPackageFinders(DirectoryInfo addSource) + private static IEnumerable GetDefaultPackageFinders(PackageSource addSource) { yield return new PackageNameIsFullyQualifiedPath(); yield return new FindPackageInDefaultLocation(new FileSystemDirectoryAccessor(Package.DefaultPackagesDirectory)); yield return new WebAssemblyAssetFinder(Package.DefaultPackagesDirectory, addSource); - //yield return new LocalToolInstallingPackageDiscoveryStrategy(Package.DefaultPackagesDirectory, addSource); } Task IPackageFinder.Find(PackageDescriptor descriptor) diff --git a/WorkspaceServer/PackageSource.cs b/WorkspaceServer/PackageSource.cs new file mode 100644 index 000000000..f099c5ae2 --- /dev/null +++ b/WorkspaceServer/PackageSource.cs @@ -0,0 +1,35 @@ +// 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; + +namespace WorkspaceServer +{ + public class PackageSource + { + DirectoryInfo _directory; + Uri _uri; + + public PackageSource(string value) + { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out _uri)) + { + } + else + { + _directory = new DirectoryInfo(value); + } + } + + public override string ToString() + { + return _directory?.ToString() ?? _uri.ToString(); + } + } +} diff --git a/WorkspaceServer/Packaging/LocalToolInstallingPackageDiscoveryStrategy.cs b/WorkspaceServer/Packaging/LocalToolInstallingPackageDiscoveryStrategy.cs index 9c39f1ba0..5fbb6e9eb 100644 --- a/WorkspaceServer/Packaging/LocalToolInstallingPackageDiscoveryStrategy.cs +++ b/WorkspaceServer/Packaging/LocalToolInstallingPackageDiscoveryStrategy.cs @@ -13,9 +13,9 @@ public class LocalToolInstallingPackageDiscoveryStrategy : IPackageDiscoveryStra { private readonly DirectoryInfo _workingDirectory; private readonly ToolPackageLocator _locator; - private readonly DirectoryInfo _addSource; + private readonly PackageSource _addSource; - public LocalToolInstallingPackageDiscoveryStrategy(DirectoryInfo workingDirectory, DirectoryInfo addSource = null) + public LocalToolInstallingPackageDiscoveryStrategy(DirectoryInfo workingDirectory, PackageSource addSource = null) { _workingDirectory = workingDirectory; _locator = new ToolPackageLocator(workingDirectory); diff --git a/WorkspaceServer/Packaging/WebAssemblyAssetFinder.cs b/WorkspaceServer/Packaging/WebAssemblyAssetFinder.cs index ef32b5fdc..f8fbddc0d 100644 --- a/WorkspaceServer/Packaging/WebAssemblyAssetFinder.cs +++ b/WorkspaceServer/Packaging/WebAssemblyAssetFinder.cs @@ -14,9 +14,9 @@ public class WebAssemblyAssetFinder : IPackageFinder { private readonly DirectoryInfo _workingDirectory; private readonly ToolPackageLocator _locator; - private readonly DirectoryInfo _addSource; + private readonly PackageSource _addSource; - public WebAssemblyAssetFinder(DirectoryInfo workingDirectory, DirectoryInfo addSource = null) + public WebAssemblyAssetFinder(DirectoryInfo workingDirectory, PackageSource addSource = null) { _workingDirectory = workingDirectory; _locator = new ToolPackageLocator(workingDirectory); From 1524d07a700d2ca792712ca836d522b539096ff4 Mon Sep 17 00:00:00 2001 From: Ravi Chande Date: Mon, 20 May 2019 18:13:12 -0700 Subject: [PATCH 2/5] move fixer around --- WorkspaceServer/PackageRegistry.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/WorkspaceServer/PackageRegistry.cs b/WorkspaceServer/PackageRegistry.cs index 204cb1711..7b2e5fdb0 100644 --- a/WorkspaceServer/PackageRegistry.cs +++ b/WorkspaceServer/PackageRegistry.cs @@ -53,7 +53,7 @@ private PackageRegistry( _strategies.Add(strategy); } - _packageFinders = packageFinders?.ToList() ?? GetDefaultPackageFinders(addSource).ToList(); + _packageFinders = packageFinders?.ToList() ?? GetDefaultPackageFinders().ToList(); } public void Add(string name, Action configure) @@ -136,9 +136,11 @@ private Task GetPackageFromPackageBuilder(string packageName, Budge public static PackageRegistry CreateForTryMode(DirectoryInfo project, PackageSource addSource = null) { + var finders = GetDefaultPackageFinders().Append(new WebAssemblyAssetFinder(Package.DefaultPackagesDirectory, addSource)); var registry = new PackageRegistry( true, addSource, + finders, additionalStrategies: new LocalToolInstallingPackageDiscoveryStrategy(Package.DefaultPackagesDirectory, addSource)); registry.Add(project.Name, builder => @@ -153,8 +155,7 @@ public static PackageRegistry CreateForTryMode(DirectoryInfo project, PackageSou public static PackageRegistry CreateForHostedMode() { var registry = new PackageRegistry( - false, - additionalStrategies: new LocalToolInstallingPackageDiscoveryStrategy(Package.DefaultPackagesDirectory)); + false); registry.Add("console", packageBuilder => @@ -235,11 +236,10 @@ public IEnumerator> GetEnumerator() => IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - private static IEnumerable GetDefaultPackageFinders(PackageSource addSource) + private static IEnumerable GetDefaultPackageFinders() { yield return new PackageNameIsFullyQualifiedPath(); yield return new FindPackageInDefaultLocation(new FileSystemDirectoryAccessor(Package.DefaultPackagesDirectory)); - yield return new WebAssemblyAssetFinder(Package.DefaultPackagesDirectory, addSource); } Task IPackageFinder.Find(PackageDescriptor descriptor) From 1cd074749d4ffd2bdc1d5912d1880bcd3219f059 Mon Sep 17 00:00:00 2001 From: Ravi Chande Date: Tue, 21 May 2019 10:25:55 -0700 Subject: [PATCH 3/5] Fix tests --- MLS.Agent/CommandLine/CommandLineParser.cs | 6 +++--- WorkspaceServer/PackageSource.cs | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/MLS.Agent/CommandLine/CommandLineParser.cs b/MLS.Agent/CommandLine/CommandLineParser.cs index d99d3e7bf..796f7c259 100644 --- a/MLS.Agent/CommandLine/CommandLineParser.cs +++ b/MLS.Agent/CommandLine/CommandLineParser.cs @@ -137,10 +137,10 @@ RootCommand StartInTryMode() command.AddOption(new Option( "--add-package-source", "Specify an additional NuGet package source", - new Argument(new DirectoryInfo(Directory.GetCurrentDirectory())) + new Argument(new PackageSource(Directory.GetCurrentDirectory())) { Name = "NuGet source" - }.ExistingOnly())); + })); command.AddOption(new Option( "--package", @@ -339,7 +339,7 @@ Command Install() installCommand.IsHidden = true; var option = new Option("--add-source", - argument: new Argument().ExistingOnly()); + argument: new Argument()); installCommand.AddOption(option); diff --git a/WorkspaceServer/PackageSource.cs b/WorkspaceServer/PackageSource.cs index f099c5ae2..63b89ac27 100644 --- a/WorkspaceServer/PackageSource.cs +++ b/WorkspaceServer/PackageSource.cs @@ -18,7 +18,10 @@ public PackageSource(string value) throw new ArgumentNullException(nameof(value)); } - if (Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out _uri)) + // Uri.IsWellFormed will return false for path-like strings: + // (https://docs.microsoft.com/en-us/dotnet/api/system.uri.iswellformeduristring?view=netcore-2.2) + if (Uri.IsWellFormedUriString(value, UriKind.Absolute) && + Uri.TryCreate(value, UriKind.Absolute, out _uri)) { } else From 09894a1ca5a12c61f0c4da862d1b93b8e0b77792 Mon Sep 17 00:00:00 2001 From: Ravi Chande Date: Tue, 21 May 2019 11:11:41 -0700 Subject: [PATCH 4/5] Fix test --- WorkspaceServer/PackageRegistry.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WorkspaceServer/PackageRegistry.cs b/WorkspaceServer/PackageRegistry.cs index 7b2e5fdb0..8ba301ffa 100644 --- a/WorkspaceServer/PackageRegistry.cs +++ b/WorkspaceServer/PackageRegistry.cs @@ -34,7 +34,8 @@ public PackageRegistry( { new ProjectFilePackageDiscoveryStrategy(createRebuildablePackages), new DirectoryPackageDiscoveryStrategy(createRebuildablePackages) - }.Concat(additionalStrategies)) + }.Concat(additionalStrategies), + packageFinders) { } From c6e56db03741a9010ea410646987057113430d6a Mon Sep 17 00:00:00 2001 From: Ravi Chande Date: Tue, 21 May 2019 12:34:43 -0700 Subject: [PATCH 5/5] Fix tests xplat --- WorkspaceServer/PackageSource.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WorkspaceServer/PackageSource.cs b/WorkspaceServer/PackageSource.cs index 63b89ac27..a99f64502 100644 --- a/WorkspaceServer/PackageSource.cs +++ b/WorkspaceServer/PackageSource.cs @@ -21,8 +21,10 @@ public PackageSource(string value) // Uri.IsWellFormed will return false for path-like strings: // (https://docs.microsoft.com/en-us/dotnet/api/system.uri.iswellformeduristring?view=netcore-2.2) if (Uri.IsWellFormedUriString(value, UriKind.Absolute) && - Uri.TryCreate(value, UriKind.Absolute, out _uri)) + Uri.TryCreate(value, UriKind.Absolute, out var uri) + && uri.Scheme != null) { + _uri = uri; } else {