diff --git a/src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs b/src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs index 3069edb..711a9a1 100644 --- a/src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs +++ b/src/Pharmacist.Core/NuGet/NuGetPackageHelper.cs @@ -54,11 +54,6 @@ static NuGetPackageHelper() _globalPackagesPath = SettingsUtility.GetGlobalPackagesFolder(machineWideSettings.Settings.LastOrDefault() ?? (ISettings)NullSettings.Instance); } - /// - /// Gets the directory where the packages will be stored. - /// - public static string PackageDirectory { get; } = Path.Combine(Path.GetTempPath(), "ReactiveUI.Pharmacist"); - /// /// Gets the providers for the nuget resources. /// @@ -72,6 +67,7 @@ static NuGetPackageHelper() /// Optional v3 nuget source. Will default to default nuget.org servers. /// If we should get the dependencies. /// Directories to package folders. Will be lib/build/ref if not defined. + /// A directory where to store the files, if null a random location will be used. /// A cancellation token. /// The directory where the NuGet packages are unzipped to. Also the files contained within the requested package only. public static async Task files)>> DownloadPackageFilesAndFolder( @@ -80,6 +76,7 @@ static NuGetPackageHelper() PackageSource nugetSource = null, bool getDependencies = true, IReadOnlyCollection packageFolders = null, + string packageDirectory = null, CancellationToken token = default) { // If the user hasn't selected a default framework to extract, select .NET Standard 2.0 @@ -90,7 +87,7 @@ static NuGetPackageHelper() var packages = await Task.WhenAll(libraryIdentities.Select(x => GetBestMatch(x, sourceRepository, token))).ConfigureAwait(false); - return await DownloadPackageFilesAndFolder(packages, frameworks, sourceRepository, getDependencies, packageFolders, token).ConfigureAwait(false); + return await DownloadPackageFilesAndFolder(packages, frameworks, sourceRepository, getDependencies, packageFolders, packageDirectory, token).ConfigureAwait(false); } /// @@ -101,6 +98,7 @@ static NuGetPackageHelper() /// Optional v3 nuget source. Will default to default nuget.org servers. /// If we should get the dependencies. /// Directories to package folders. Will be lib/build/ref if not defined. + /// A directory where to store the files, if null a random location will be used. /// A cancellation token. /// The directory where the NuGet packages are unzipped to. Also the files contained within the requested package only. public static Task files)>> DownloadPackageFilesAndFolder( @@ -109,6 +107,7 @@ static NuGetPackageHelper() PackageSource nugetSource = null, bool getDependencies = true, IReadOnlyCollection packageFolders = null, + string packageDirectory = null, CancellationToken token = default) { // If the user hasn't selected a default framework to extract, select .NET Standard 2.0 @@ -117,7 +116,7 @@ static NuGetPackageHelper() // Use the provided nuget package source, or use nuget.org var sourceRepository = new SourceRepository(nugetSource ?? new PackageSource(DefaultNuGetSource), Providers); - return DownloadPackageFilesAndFolder(packageIdentities, frameworks, sourceRepository, getDependencies, packageFolders, token); + return DownloadPackageFilesAndFolder(packageIdentities, frameworks, sourceRepository, getDependencies, packageFolders, packageDirectory, token); } /// @@ -146,6 +145,7 @@ public static async Task GetBestMatch(LibraryRange identity, So /// Nuget source repository. Will default to default nuget.org servers. /// If we should get the dependencies. /// Directories to package folders. Will be lib/build/ref if not defined. + /// A directory where to store the files, if null a random location will be used. /// A cancellation token. /// The directory where the NuGet packages are unzipped to. Also the files contained within the requested package only. private static async Task files)>> DownloadPackageFilesAndFolder( @@ -154,11 +154,14 @@ public static async Task GetBestMatch(LibraryRange identity, So SourceRepository sourceRepository, bool getDependencies = true, IReadOnlyCollection packageFolders = null, + string packageDirectory = null, CancellationToken token = default) { var librariesToCopy = await GetPackagesToCopy(packageIdentities, sourceRepository, frameworks.First(), getDependencies, token).ConfigureAwait(false); - return CopyPackageFiles(librariesToCopy, frameworks, packageFolders ?? DefaultFoldersToGrab, token); + packageDirectory = packageDirectory ?? GetRandomPackageDirectory(); + + return CopyPackageFiles(librariesToCopy, frameworks, packageFolders ?? DefaultFoldersToGrab, packageDirectory, token); } private static async Task> GetPackagesToCopy( @@ -223,13 +226,13 @@ public static async Task GetBestMatch(LibraryRange identity, So return packagesToCopy.Select(x => (x.Value.packageIdentity, x.Value.downloadResourceResult, x.Value.includeFilesInOutput)); } - private static IReadOnlyCollection<(string folder, IReadOnlyCollection files)> CopyPackageFiles(IEnumerable<(PackageIdentity packageIdentity, DownloadResourceResult downloadResourceResult, bool includeFilesInOutput)> packagesToProcess, IReadOnlyCollection frameworks, IReadOnlyCollection packageFolders, CancellationToken token) + private static IReadOnlyCollection<(string folder, IReadOnlyCollection files)> CopyPackageFiles(IEnumerable<(PackageIdentity packageIdentity, DownloadResourceResult downloadResourceResult, bool includeFilesInOutput)> packagesToProcess, IReadOnlyCollection frameworks, IReadOnlyCollection packageFolders, string packageDirectory, CancellationToken token) { var output = new List<(string folder, IReadOnlyCollection files)>(); foreach (var packageToProcess in packagesToProcess) { var (packageIdentity, downloadResourceResults, includeFilesInOutput) = packageToProcess; - var directory = Path.Combine(PackageDirectory, packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); + var directory = Path.Combine(packageDirectory, packageIdentity.Id, packageIdentity.Version.ToNormalizedString()); EnsureDirectory(directory); @@ -260,6 +263,12 @@ public static async Task GetBestMatch(LibraryRange identity, So return output; } + private static string GetRandomPackageDirectory() + { + var packageDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + return packageDirectory; + } + /// /// Gets dependency packages that matches our framework (current version or below). /// diff --git a/src/Pharmacist.Tests/API/ApiApprovalTests.EventBuilderProject.net472.approved.txt b/src/Pharmacist.Tests/API/ApiApprovalTests.EventBuilderProject.net472.approved.txt index 439af79..f9c0b1d 100644 --- a/src/Pharmacist.Tests/API/ApiApprovalTests.EventBuilderProject.net472.approved.txt +++ b/src/Pharmacist.Tests/API/ApiApprovalTests.EventBuilderProject.net472.approved.txt @@ -99,16 +99,15 @@ namespace Pharmacist.Core.NuGet public class static NuGetPackageHelper { public const string DefaultNuGetSource = "https://api.nuget.org/v3/index.json"; - public static string PackageDirectory { get; } public static System.Collections.Generic.List> Providers { get; } [return: System.Runtime.CompilerServices.TupleElementNamesAttribute(new string[] { "folder", "files"})] - public static System.Threading.Tasks.Task>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection libraryIdentities, System.Collections.Generic.IReadOnlyCollection frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection packageFolders = null, System.Threading.CancellationToken token = null) { } + public static System.Threading.Tasks.Task>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection libraryIdentities, System.Collections.Generic.IReadOnlyCollection frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection packageFolders = null, string packageDirectory = null, System.Threading.CancellationToken token = null) { } [return: System.Runtime.CompilerServices.TupleElementNamesAttribute(new string[] { "folder", "files"})] - public static System.Threading.Tasks.Task>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection packageIdentities, System.Collections.Generic.IReadOnlyCollection frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection packageFolders = null, System.Threading.CancellationToken token = null) { } + public static System.Threading.Tasks.Task>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection packageIdentities, System.Collections.Generic.IReadOnlyCollection frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection packageFolders = null, string packageDirectory = null, System.Threading.CancellationToken token = null) { } public static System.Threading.Tasks.Task GetBestMatch(NuGet.LibraryModel.LibraryRange identity, NuGet.Protocol.Core.Types.SourceRepository sourceRepository, System.Threading.CancellationToken token) { } } } diff --git a/src/Pharmacist.Tests/API/ApiApprovalTests.EventBuilderProject.netcoreapp2.2.approved.txt b/src/Pharmacist.Tests/API/ApiApprovalTests.EventBuilderProject.netcoreapp2.2.approved.txt index 879ef52..c85e8cb 100644 --- a/src/Pharmacist.Tests/API/ApiApprovalTests.EventBuilderProject.netcoreapp2.2.approved.txt +++ b/src/Pharmacist.Tests/API/ApiApprovalTests.EventBuilderProject.netcoreapp2.2.approved.txt @@ -99,16 +99,15 @@ namespace Pharmacist.Core.NuGet public class static NuGetPackageHelper { public const string DefaultNuGetSource = "https://api.nuget.org/v3/index.json"; - public static string PackageDirectory { get; } public static System.Collections.Generic.List> Providers { get; } [return: System.Runtime.CompilerServices.TupleElementNamesAttribute(new string[] { "folder", "files"})] - public static System.Threading.Tasks.Task>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection libraryIdentities, System.Collections.Generic.IReadOnlyCollection frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection packageFolders = null, System.Threading.CancellationToken token = null) { } + public static System.Threading.Tasks.Task>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection libraryIdentities, System.Collections.Generic.IReadOnlyCollection frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection packageFolders = null, string packageDirectory = null, System.Threading.CancellationToken token = null) { } [return: System.Runtime.CompilerServices.TupleElementNamesAttribute(new string[] { "folder", "files"})] - public static System.Threading.Tasks.Task>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection packageIdentities, System.Collections.Generic.IReadOnlyCollection frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection packageFolders = null, System.Threading.CancellationToken token = null) { } + public static System.Threading.Tasks.Task>>> DownloadPackageFilesAndFolder(System.Collections.Generic.IReadOnlyCollection packageIdentities, System.Collections.Generic.IReadOnlyCollection frameworks = null, NuGet.Configuration.PackageSource nugetSource = null, bool getDependencies = True, System.Collections.Generic.IReadOnlyCollection packageFolders = null, string packageDirectory = null, System.Threading.CancellationToken token = null) { } public static System.Threading.Tasks.Task GetBestMatch(NuGet.LibraryModel.LibraryRange identity, NuGet.Protocol.Core.Types.SourceRepository sourceRepository, System.Threading.CancellationToken token) { } } } diff --git a/src/Pharmacist.Tests/NuGetPackageHelperTests.cs b/src/Pharmacist.Tests/NuGetPackageHelperTests.cs index 71af77c..eaed9c7 100644 --- a/src/Pharmacist.Tests/NuGetPackageHelperTests.cs +++ b/src/Pharmacist.Tests/NuGetPackageHelperTests.cs @@ -118,17 +118,6 @@ public class NuGetPackageHelperTests "Tizen.NET.API4" + Path.DirectorySeparatorChar + "4.0.1.14152" }; - /// - /// Initializes a new instance of the class. - /// - public NuGetPackageHelperTests() - { - if (Directory.Exists(NuGetPackageHelper.PackageDirectory)) - { - Directory.Delete(NuGetPackageHelper.PackageDirectory, true); - } - } - /// /// Check to make sure that the tizen packages produce the correct files. /// @@ -157,7 +146,7 @@ public async Task CanGetNuGetProtocolAndDependencies() var frameworks = new[] { FrameworkConstants.CommonFrameworks.NetStandard20 }; var result = (await NuGetPackageHelper - .DownloadPackageFilesAndFolder(package, frameworks: frameworks) + .DownloadPackageFilesAndFolder(package, frameworks) .ConfigureAwait(false)).ToList(); result.ShouldNotBeEmpty(); @@ -168,8 +157,9 @@ private static async Task GetAndCheckTizenPackage() var package = new[] { new PackageIdentity("Tizen.NET.API4", new NuGetVersion("4.0.1.14152")) }; var frameworks = new[] { FrameworkConstants.CommonFrameworks.NetStandard20 }; + var packageDirectory = Path.Combine(Path.GetTempPath(), "Pharmacist.Tests"); var result = (await NuGetPackageHelper - .DownloadPackageFilesAndFolder(package, frameworks: frameworks) + .DownloadPackageFilesAndFolder(package, frameworks, packageDirectory: packageDirectory) .ConfigureAwait(false)).ToList(); var actualFiles = result.SelectMany(x => x.files).Where(x => x.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase)).ToList(); @@ -178,7 +168,7 @@ private static async Task GetAndCheckTizenPackage() Assert.True(actualDirectories.All(Directory.Exists)); var actualFileNames = actualFiles.Select(Path.GetFileName).ToList(); - var actualDirectoryNames = actualDirectories.Select(x => x.Replace(NuGetPackageHelper.PackageDirectory + Path.DirectorySeparatorChar, string.Empty)).ToList(); + var actualDirectoryNames = actualDirectories.Select(x => x.Replace(packageDirectory + Path.DirectorySeparatorChar, string.Empty)).ToList(); ExpectedTizenFiles.ShouldHaveSameContents(actualFileNames); ExpectedTizenDirectories.ShouldHaveSameContents(actualDirectoryNames); }