From a6afa5f6fcb91c8b2f799423ea576e655a165bc4 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Thu, 10 Oct 2024 15:59:41 -0700 Subject: [PATCH] [REF] Use NuGet VersionFolderPathResolver --- .../NuGetPackageDownloader.cs | 19 ++++++++----------- .../ToolPackage/ToolPackageDownloader.cs | 7 ++++--- .../dotnet/ToolPackage/ToolPackageInstance.cs | 4 +++- .../ResolvePackageAssets.cs | 4 ++-- .../ResolvePackageDependencies.cs | 4 ++-- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs b/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs index 2710c2424416..a2238612a1d1 100644 --- a/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs +++ b/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs @@ -131,14 +131,10 @@ public async Task DownloadPackageAsync(PackageId packageId, string.Format(LocalizableStrings.IsNotFoundInNuGetFeeds, packageId, source.Source)); } - string nupkgPath = downloadFolder == null || !downloadFolder.HasValue - ? Path.Combine(_packageInstallDir.Value, packageId.ToString(), - resolvedPackageVersion.ToNormalizedString().ToLowerInvariant(), - $"{packageId}.{resolvedPackageVersion.ToNormalizedString().ToLowerInvariant()}.nupkg") - : Path.Combine(downloadFolder.Value.Value, - $"{packageId}.{resolvedPackageVersion.ToNormalizedString().ToLowerInvariant()}.nupkg"); - + var pathResolver = new VersionFolderPathResolver(downloadFolder == null || !downloadFolder.HasValue ? _packageInstallDir.Value : downloadFolder.Value.Value); + string nupkgPath = pathResolver.GetPackageFilePath(packageId.ToString(), resolvedPackageVersion); Directory.CreateDirectory(Path.GetDirectoryName(nupkgPath)); + using FileStream destinationStream = File.Create(nupkgPath); bool success = await ExponentialRetry.ExecuteWithRetryOnFailure(async () => await resource.CopyNupkgToStreamAsync( packageId.ToString(), @@ -218,7 +214,10 @@ public async Task GetPackageUrl(PackageId packageId, SourceRepository repository = GetSourceRepository(source); if (repository.PackageSource.IsLocal) { - return Path.Combine(repository.PackageSource.Source, $"{packageId}.{resolvedPackageVersion}.nupkg"); + return Path.Combine( + repository.PackageSource.Source, + new VersionFolderPathResolver(repository.PackageSource.Source).GetPackageFileName(packageId.ToString(), resolvedPackageVersion) + ); } ServiceIndexResourceV3 serviceIndexResource = repository.GetResourceAsync().Result; @@ -307,9 +306,7 @@ await GetPackageMetadataAsync(packageId.ToString(), packageVersion, packagesSour return (source, packageVersion); } - private string GetNupkgUrl(string baseUri, PackageId id, NuGetVersion version) => - baseUri + id.ToString() + "/" + version.ToNormalizedString().ToLowerInvariant() + "/" + id.ToString() + - "." + version.ToNormalizedString().ToLowerInvariant() + ".nupkg"; + private string GetNupkgUrl(string baseUri, PackageId id, NuGetVersion version) => new VersionFolderPathResolver(baseUri).GetPackageFilePath(id.ToString(), version); internal IEnumerable FindAllFilesNeedExecutablePermission(IEnumerable files, string targetPath) diff --git a/src/Cli/dotnet/ToolPackage/ToolPackageDownloader.cs b/src/Cli/dotnet/ToolPackage/ToolPackageDownloader.cs index a1675961d966..925fb9276dbb 100644 --- a/src/Cli/dotnet/ToolPackage/ToolPackageDownloader.cs +++ b/src/Cli/dotnet/ToolPackage/ToolPackageDownloader.cs @@ -107,11 +107,12 @@ public IToolPackage InstallPackage(PackageLocation packageLocation, PackageId pa } NuGetVersion packageVersion = nugetPackageDownloader.GetBestPackageVersionAsync(packageId, versionRange, packageSourceLocation).GetAwaiter().GetResult(); - rollbackDirectory = isGlobalTool ? toolDownloadDir.Value: Path.Combine(toolDownloadDir.Value, packageId.ToString(), packageVersion.ToNormalizedString().ToLowerInvariant()); + rollbackDirectory = isGlobalTool ? toolDownloadDir.Value: new VersionFolderPathResolver(toolDownloadDir.Value).GetInstallPath(packageId.ToString(), packageVersion); if (isGlobalTool) { - NuGetv3LocalRepository nugetPackageRootDirectory = new(Path.Combine(_toolPackageStore.GetRootPackageDirectory(packageId).ToString().Trim('"'), packageVersion.ToNormalizedString().ToLowerInvariant())); + // TODO: NuGetv3LocalRepository already has a VersionFolderPathResolver member, so this can probably be simplified + NuGetv3LocalRepository nugetPackageRootDirectory = new(new VersionFolderPathResolver(_toolPackageStore.Root.Value).GetInstallPath(packageId.ToString(), packageVersion)); var globalPackage = nugetPackageRootDirectory.FindPackage(packageId.ToString(), packageVersion); if (globalPackage != null) @@ -303,7 +304,7 @@ private static async Task DownloadAndExtractPackage( } // Extract the package - var nupkgDir = Path.Combine(packagesRootPath, packageId.ToString(), version.ToNormalizedString().ToLowerInvariant()); + var nupkgDir = new VersionFolderPathResolver(packagesRootPath).GetInstallPath(packageId.ToString(), version); await nugetPackageDownloader.ExtractPackageAsync(packagePath, new DirectoryPath(nupkgDir)); return version; diff --git a/src/Cli/dotnet/ToolPackage/ToolPackageInstance.cs b/src/Cli/dotnet/ToolPackage/ToolPackageInstance.cs index a842d2b50e38..fa6a0b35cb4d 100644 --- a/src/Cli/dotnet/ToolPackage/ToolPackageInstance.cs +++ b/src/Cli/dotnet/ToolPackage/ToolPackageInstance.cs @@ -6,6 +6,7 @@ using Microsoft.DotNet.Tools; using Microsoft.Extensions.EnvironmentAbstractions; using NuGet.Frameworks; +using NuGet.Packaging; using NuGet.ProjectModel; using NuGet.Versioning; @@ -85,7 +86,8 @@ public ToolPackageInstance(PackageId id, _lockFile = new Lazy( () => new LockFileFormat().Read(assetsJsonParentDirectory.WithFile(AssetsFileName).Value)); - var toolsPackagePath = Path.Combine(PackageDirectory.Value, Id.ToString(), Version.ToNormalizedString().ToLowerInvariant(), "tools"); + var installPath = new VersionFolderPathResolver(PackageDirectory.Value).GetInstallPath(Id.ToString(), Version); + var toolsPackagePath = Path.Combine(installPath, "tools"); Frameworks = Directory.GetDirectories(toolsPackagePath) .Select(path => NuGetFramework.ParseFolder(Path.GetFileName(path))); } diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs index d504fe68383b..a9d36b64c734 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs @@ -1442,8 +1442,8 @@ private void WritePackageDependenciesDesignTime() foreach (var package in _lockFile.Libraries) { - var packageVersion = package.Version.ToNormalizedString().ToLowerInvariant(); - string packageId = $"{package.Name}/{packageVersion}"; + var packageVersion = package.Version.ToNormalizedString(); + string packageId = $"{package.Name}/{packageVersion.ToLowerInvariant()}"; // Find PackageDefinitions that match our allowed item specs if (string.IsNullOrEmpty(package.Name) || !allowItemSpecs.Contains(packageId)) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs index 084ced81074d..8ff1e992210b 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs @@ -166,8 +166,8 @@ private void GetPackageAndFileDefinitions() foreach (var package in LockFile.Libraries) { var packageName = package.Name; - var packageVersion = package.Version.ToNormalizedString().ToLowerInvariant(); - string packageId = $"{packageName}/{packageVersion}"; + var packageVersion = package.Version.ToNormalizedString(); + string packageId = $"{packageName}/{packageVersion.ToLowerInvariant()}"; var item = new TaskItem(packageId); item.SetMetadata(MetadataKeys.Name, packageName); item.SetMetadata(MetadataKeys.Type, package.Type);