Skip to content

Commit

Permalink
[REF] Use NuGet VersionFolderPathResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
edvilme committed Oct 14, 2024
1 parent 950a079 commit a6afa5f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
19 changes: 8 additions & 11 deletions src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,10 @@ public async Task<string> 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(),
Expand Down Expand Up @@ -218,7 +214,10 @@ public async Task<string> 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<ServiceIndexResourceV3>().Result;
Expand Down Expand Up @@ -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<FilePath> FindAllFilesNeedExecutablePermission(IEnumerable<string> files,
string targetPath)
Expand Down
7 changes: 4 additions & 3 deletions src/Cli/dotnet/ToolPackage/ToolPackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -303,7 +304,7 @@ private static async Task<NuGetVersion> 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;
Expand Down
4 changes: 3 additions & 1 deletion src/Cli/dotnet/ToolPackage/ToolPackageInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.DotNet.Tools;
using Microsoft.Extensions.EnvironmentAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.ProjectModel;
using NuGet.Versioning;

Expand Down Expand Up @@ -85,7 +86,8 @@ public ToolPackageInstance(PackageId id,
_lockFile =
new Lazy<LockFile>(
() => 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)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a6afa5f

Please sign in to comment.