/// <summary> /// Gets a NugetPackage from the NuGet server that matches (or is in range of) the <see cref="NugetPackageIdentifier"/> given. /// If an exact match isn't found, it selects the next closest version available. /// </summary> /// <param name="package">The <see cref="NugetPackageIdentifier"/> containing the ID and Version of the package to get.</param> /// <returns>The retrieved package, if there is one. Null if no matching package was found.</returns> public List <NugetPackage> FindPackagesById(NugetPackageIdentifier package) { List <NugetPackage> foundPackages = null; if (IsLocalPath) { // determine local path string localPackagePath; if (IsLocalPathAndVersion33) { localPackagePath = NugetPackage.PathLocal33Get(ExpandedPath, package.Id, package.Version); } else { localPackagePath = NugetPackage.PathLocalGet(ExpandedPath, package.Id, package.Version); } if (File.Exists(localPackagePath)) { NugetPackage localPackage = NugetPackage.FromNupkgFile(localPackagePath); foundPackages = new List <NugetPackage> { localPackage }; } else { // TODO: Sort the local packages? Currently assuming they are in alphabetical order due to the filesystem. // TODO: Optimize to no longer use GetLocalPackages, since that loads the .nupkg itself // Try to find later versions of the same package var packages = GetLocalPackages(package.Id, true, true); foundPackages = new List <NugetPackage>(packages.SkipWhile(x => !package.InRange(x))); } } else { // See here: http://www.odata.org/documentation/odata-version-2-0/uri-conventions/ string url = string.Empty; // We used to rely on expressions such as &$filter=Version ge '9.0.1' to find versions in a range, but the results were sorted alphabetically. This // caused version 10.0.0 to be less than version 9.0.0. In order to work around this issue, we'll request all versions and perform filtering ourselves. url = string.Format("{0}FindPackagesById()?$orderby=Version asc&id='{1}'", ExpandedPath, package.Id); try { foundPackages = GetPackagesFromUrl(url, ExpandedPassword); } catch (System.Exception e) { foundPackages = new List <NugetPackage>(); Debug.LogErrorFormat("Unable to retrieve package list from {0}\n{1}", url, e.ToString()); } foundPackages.Sort(); if (foundPackages.Exists(p => package.InRange(p))) { // Return all the packages in the range of versions specified by 'package'. foundPackages.RemoveAll(p => !package.InRange(p)); } else { // There are no packages in the range of versions specified by 'package'. // Return the most recent version after the version specified by 'package'. foundPackages.RemoveAll(p => package.CompareVersion(p.Version) < 0); if (foundPackages.Count > 0) { foundPackages.RemoveRange(1, foundPackages.Count - 1); } } } if (foundPackages != null) { foreach (NugetPackage foundPackage in foundPackages) { foundPackage.PackageSource = this; } } return(foundPackages); }
/// <summary> /// Gets a NugetPackage from the NuGet server that matches (or is in range of) the <see cref="NugetPackageIdentifier"/> given. /// </summary> /// <param name="package">The <see cref="NugetPackageIdentifier"/> containing the ID and Version of the package to get.</param> /// <returns>The retrieved package, if there is one. Null if no matching package was found.</returns> public List <NugetPackage> FindPackagesById(NugetPackageIdentifier package) { List <NugetPackage> foundPackages = null; if (IsLocalPath) { // determine local path string localPackagePath; if (IsLocalPathAndVersion33) { localPackagePath = NugetPackage.PathLocal33Get(ExpandedPath, package.Id, package.Version); } else { localPackagePath = NugetPackage.PathLocalGet(ExpandedPath, package.Id, package.Version); } if (File.Exists(localPackagePath)) { string localPackagePath = Path.Combine(ExpandedPath, string.Format("./{0}.{1}.nupkg", package.Id, package.Version)); if (File.Exists(localPackagePath)) { NugetPackage localPackage = NugetPackage.FromNupkgFile(localPackagePath); foundPackages = new List <NugetPackage> { localPackage }; } else { foundPackages = new List <NugetPackage>(); } } else { // Try to find later versions of the same package var packages = GetLocalPackages(package.Id, true, true); foundPackages = new List <NugetPackage>(packages.SkipWhile(x => !package.InRange(x))); } } else { // See here: http://www.odata.org/documentation/odata-version-2-0/uri-conventions/ // Note: without $orderby=Version, the Version filter below will not work string url = string.Format("{0}FindPackagesById()?id='{1}'&$orderby=Version asc", ExpandedPath, package.Id); // Are we looking for a specific package? if (!package.HasVersionRange) { url = string.Format("{0}&$filter=Version eq '{1}'", url, package.Version); } try { foundPackages = GetPackagesFromUrl(url, UserName, ExpandedPassword); } catch (Exception e) { foundPackages = new List <NugetPackage>(); Debug.LogErrorFormat("Unable to retrieve package list from {0}\n{1}", url, e.ToString()); } } if (foundPackages != null) { // Return all the packages in the range of versions specified by 'package'. foundPackages.RemoveAll(p => !package.InRange(p)); foundPackages.Sort(); foreach (NugetPackage foundPackage in foundPackages) { foundPackage.PackageSource = this; } } return(foundPackages); }
/// <summary> /// Gets a NugetPackage from the NuGet server that matches (or is in range of) the <see cref="NugetPackageIdentifier"/> given. /// If an exact match isn't found, it selects the next closest version available. /// </summary> /// <param name="package">The <see cref="NugetPackageIdentifier"/> containing the ID and Version of the package to get.</param> /// <returns>The retrieved package, if there is one. Null if no matching package was found.</returns> public NugetPackage GetSpecificPackage(NugetPackageIdentifier package) { NugetPackage foundPackage = null; if (IsLocalPath) { string localPackagePath = System.IO.Path.Combine(Path, string.Format("./{0}.{1}.nupkg", package.Id, package.Version)); if (File.Exists(localPackagePath)) { foundPackage = NugetPackage.FromNupkgFile(localPackagePath); } else { // TODO: Sort the local packages? Currently assuming they are in alphabetical order due to the filesystem. // TODO: Optimize to no longer use GetLocalPackages, since that loads the .nupkg itself // Try to find later versions of the same package var packages = GetLocalPackages(package.Id, true, true); foundPackage = packages.SkipWhile(x => !package.InRange(x)).FirstOrDefault(); } } else { // See here: http://www.odata.org/documentation/odata-version-2-0/uri-conventions/ string url = string.Empty; if (!package.HasVersionRange) { url = string.Format("{0}FindPackagesById()?$orderby=Version asc&id='{1}'&$filter=Version ge '{2}'", Path, package.Id, package.Version); } else { url = string.Format("{0}FindPackagesById()?$orderby=Version asc&id='{1}'&$filter=", Path, package.Id); bool hasMin = false; if (!string.IsNullOrEmpty(package.MinimumVersion)) { // Some packages append extraneous ".0"s to the end of the version number for dependencies // For example, the actual package version is "1.0", but the dependency is listed as "1.0.0" // In these cases the NuGet server fails to return the "1.0" version when that version string is queried // While this seems to be a flaw in the NuGet server, we shall fix it here by removing the last .0, if there is one // Note that it only removes the last one and not all, this can be made to loop if additional problems are found in other packages var minVersion = package.MinimumVersion.EndsWith(".0") ? package.MinimumVersion.Remove(package.MinimumVersion.LastIndexOf(".0", StringComparison.Ordinal)) : package.MinimumVersion; hasMin = true; if (package.IsMinInclusive) { url += string.Format("Version ge '{0}'", minVersion); } else { url += string.Format("Version gt '{0}'", minVersion); } } if (!string.IsNullOrEmpty(package.MaximumVersion)) { if (hasMin) { url += " and "; } if (package.IsMaxInclusive) { url += string.Format("Version le '{0}'", package.MaximumVersion); } else { url += string.Format("Version lt '{0}'", package.MaximumVersion); } } else { if (package.IsMaxInclusive) { // if there is no MaxVersion specified, but the Max is Inclusive, then it is an EXACT version match with the stored MINIMUM url += string.Format(" and Version le '{0}'", package.MinimumVersion); } } } foundPackage = GetPackagesFromUrl(url).FirstOrDefault(); } if (foundPackage != null) { foundPackage.PackageSource = this; } return(foundPackage); }
/// <summary> /// Gets a NugetPackage from the NuGet server that matches (or is in range of) the <see cref="NugetPackageIdentifier"/> given. /// </summary> /// <param name="package">The <see cref="NugetPackageIdentifier"/> containing the ID and Version of the package to get.</param> /// <returns>The retrieved package, if there is one. Null if no matching package was found.</returns> public List <NugetPackage> FindPackagesById(NugetPackageIdentifier package) { List <NugetPackage> foundPackages = null; if (IsLocalPath) { if (!package.HasVersionRange) { string localPackagePath = Path.Combine(ExpandedPath, string.Format("./{0}.{1}.nupkg", package.Id, package.Version)); if (File.Exists(localPackagePath)) { NugetPackage localPackage = NugetPackage.FromNupkgFile(localPackagePath); foundPackages = new List <NugetPackage> { localPackage }; } else { foundPackages = new List <NugetPackage>(); } } else { // TODO: Optimize to no longer use GetLocalPackages, since that loads the .nupkg itself foundPackages = GetLocalPackages(package.Id, true, true); } } else { // See here: http://www.odata.org/documentation/odata-version-2-0/uri-conventions/ string url = string.Format("{0}FindPackagesById()?id='{1}'", ExpandedPath, package.Id); // Are we looking for a specific package? if (!package.HasVersionRange) { url = string.Format("{0}&$filter=Version eq '{1}'", url, package.Version); } try { foundPackages = GetPackagesFromUrl(url, UserName, ExpandedPassword); } catch (Exception e) { foundPackages = new List <NugetPackage>(); Debug.LogErrorFormat("Unable to retrieve package list from {0}\n{1}", url, e.ToString()); } } if (foundPackages != null) { // Return all the packages in the range of versions specified by 'package'. foundPackages.RemoveAll(p => !package.InRange(p)); foundPackages.Sort(); foreach (NugetPackage foundPackage in foundPackages) { foundPackage.PackageSource = this; } } return(foundPackages); }