Skip to content

Commit

Permalink
(chocolatey#508) Update snapshot service to respect version normaliza…
Browse files Browse the repository at this point in the history
…tion

This commit updates the file snapshot service to honor version normalization
with fallbacks to old version methods.

This is done as we are moving towards normalizing version numbers in most of
the code, which causes a conflict when installing, upgrading or uninstalling
packages that sometimes it would use non-normalized version number,
and other times it would use normalized version numbers.
Now with this change it will prefer to use normalized version number, but will
continue to read/update package versions that was installed using
non-normalized version numbers.
  • Loading branch information
AdmiringWorm authored and corbob committed May 10, 2023
1 parent a716ece commit 20d6dc1
Showing 1 changed file with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public ChocolateyPackageInformation Get(IPackageMetadata package)
return packageInformation;
}

var pkgStorePath = _fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(package.Id, package.Version.ToStringSafe()));
var pkgStorePath = GetStorePath(_fileSystem, package.Id, package.Version);

if (!_fileSystem.DirectoryExists(pkgStorePath))
{
return packageInformation;
Expand Down Expand Up @@ -176,7 +177,8 @@ public void Save(ChocolateyPackageInformation packageInformation)
return;
}

var pkgStorePath = _fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(packageInformation.Package.Id, packageInformation.Package.Version.ToStringSafe()));
var pkgStorePath = GetStorePath(_fileSystem, packageInformation.Package.Id, packageInformation.Package.Version);

_fileSystem.EnsureDirectoryExists(pkgStorePath);

if (packageInformation.RegistrySnapshot != null)
Expand Down Expand Up @@ -250,7 +252,7 @@ public void Save(ChocolateyPackageInformation packageInformation)

public void Remove(IPackageMetadata package)
{
var pkgStorePath = _fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(package.Id, package.Version.ToStringSafe()));
var pkgStorePath = GetStorePath(_fileSystem, package.Id, package.Version);
if (_config.RegularOutput) this.Log().Info("Removing Package Information for {0}".FormatWith(pkgStorePath));
_fileSystem.DeleteDirectoryChecked(pkgStorePath, recursive: true);
}
Expand All @@ -268,5 +270,51 @@ public void save_package_information(ChocolateyPackageInformation packageInforma
public void remove_package_information(IPackageMetadata package)
=> Remove(package);
#pragma warning restore IDE1006

private static string GetStorePath(IFileSystem fileSystem, string id, NuGetVersion version)
{
var preferredStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, version.ToNormalizedStringChecked()));

if (fileSystem.DirectoryExists(preferredStorePath))
{
return preferredStorePath;
}

var pkgStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, version.ToStringSafe()));

if (fileSystem.DirectoryExists(pkgStorePath))
{
return pkgStorePath;
}

// Legacy handling for package version that was installed originally as 4.0.0.0

var versionFull = version.IsPrerelease
? "{0}-{1}".FormatWith(version.Version.ToStringSafe(), version.Release)
: version.Version.ToStringSafe();
pkgStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, versionFull));

if (fileSystem.DirectoryExists(pkgStorePath))
{
return pkgStorePath;
}

// Legacy handling for package versions that was installed originally as "4.2"

if (version.Version.Revision == 0 && version.Version.Build == 0)
{
versionFull = version.IsPrerelease
? "{0}.{1}-{2}".FormatWith(version.Major, version.Minor, version.Release)
: "{0}.{1}".FormatWith(version.Major, version.Minor);
pkgStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, versionFull));

if (fileSystem.DirectoryExists(pkgStorePath))
{
return pkgStorePath;
}
}

return preferredStorePath;
}
}
}

0 comments on commit 20d6dc1

Please sign in to comment.