Nov 17, 2011 at 3:44 PM
Edited Nov 17, 2011 at 3:50 PM
I've noticed in the Nuget source that for a ZipPackage object that the return value for IsLatestVersion is always true which is kind of weird so figured I'd just post the info I have about it.
Say I create 2 nupkg packages with the same name but different versions and put them in my files system for a local package repository and I want to show in a UI which one of the 2 is the latest version, this will not be possible by using the packages IsLatest
property. I'm sure there's some reasons for this to be hard coded but in my case i need to know this information, i guess the reason would be because you'd have to look up all packages to determine this information even if you just wanted to query for one?
In my case howevre, my UI lists all of my packages in my local repository so I already have all of the info from all packages. I've then just created a custom PackageModel which contains a boolean called IsLatestVersion and have a helper method
to check against all packages as to whether the current one I'm iterating through is the latest. Perhaps there's a better approach to this but this seems to work.
var packages = PackageContext.LocalPackageManager.SourceRepository
Func<IPackage, bool> checkLatest = (p) =>
var packagesForId = packages.Where(x => x.Id == p.Id).ToArray();
//need to check major/minor version first, then need to go by DatePublished.
var biggestMajor = packagesForId.Max(x => x.Version.Major);
if (p.Version.Major < biggestMajor) return false;
//this means the major versions are the same, so now check minor
var biggestMinor = packagesForId
.Where(x => x.Version.Major == biggestMajor)
.Max(x => x.Version.Minor);
if (p.Version.Minor < biggestMinor) return false;
//this means the minor version are the same, so now check published date
var published = packagesForId
.Where(x => x.Version.Major == biggestMajor
&& x.Version.Minor == biggestMinor
if (!p.Published.HasValue && published.Count(x => x.Published.HasValue) > 0)
var maxPublish = published.Max(x => x.Published.Value);
if (p.Published.Value < maxPublish) return false;
var models = packages.Select(x => new PackageModel
IsLatestVersion = checkLatest(x),
IsVersionInstalled = PackageContext.LocalPackageManager.LocalRepository.FindPackage(x.Id, x.Version) != null,
Metadata = x,
IsPackageInstalled = x.IsInstalled(PackageContext.LocalPackageManager.LocalRepository)