コード例 #1
0
        /// <summary>
        /// Gets a list of all available packages from a local source (not a web server) that match the given filters.
        /// </summary>
        /// <param name="searchTerm">The search term to use to filter packages. Defaults to the empty string.</param>
        /// <param name="includeAllVersions">True to include older versions that are not the latest version.</param>
        /// <param name="includePrerelease">True to include prerelease packages (alpha, beta, etc).</param>
        /// <param name="numberToGet">The number of packages to fetch.</param>
        /// <param name="numberToSkip">The number of packages to skip before fetching.</param>
        /// <returns>The list of available packages.</returns>
        private List <NugetPackage> GetLocalPackages(string searchTerm = "", bool includeAllVersions = false, bool includePrerelease = false, int numberToGet = 15, int numberToSkip = 0)
        {
            List <NugetPackage> localPackages = new List <NugetPackage>();

            if (numberToSkip != 0)
            {
                // we return the entire list the first time, so no more to add
                return(localPackages);
            }

            string path = ExpandedPath;

            if (Directory.Exists(path))
            {
                string[] packagePaths = Directory.GetFiles(path, string.Format("*{0}*.nupkg", searchTerm));

                foreach (var packagePath in packagePaths)
                {
                    var package = NugetPackage.FromNupkgFile(packagePath);
                    package.PackageSource = this;

                    if (package.IsPrerelease && !includePrerelease)
                    {
                        // if it's a prerelease package and we aren't supposed to return prerelease packages, just skip it
                        continue;
                    }

                    if (includeAllVersions)
                    {
                        // if all versions are being included, simply add it and move on
                        localPackages.Add(package);
                        //LogVerbose("Adding {0} {1}", package.Id, package.Version);
                        continue;
                    }

                    var existingPackage = localPackages.FirstOrDefault(x => x.Id == package.Id);
                    if (existingPackage != null)
                    {
                        // there is already a package with the same ID in the list
                        if (existingPackage < package)
                        {
                            // if the current package is newer than the existing package, swap them
                            localPackages.Remove(existingPackage);
                            localPackages.Add(package);
                        }
                    }
                    else
                    {
                        // there is no package with the same ID in the list yet
                        localPackages.Add(package);
                    }
                }
            }
            else
            {
                Debug.LogErrorFormat("Local folder not found: {0}", path);
            }

            return(localPackages);
        }
コード例 #2
0
        /// <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 NugetPackage GetSpecificPackage(NugetPackageIdentifier package)
        {
            if (package.HasVersionRange)
            {
                return(FindPackagesById(package).FirstOrDefault());
            }

            if (IsLocalPath)
            {
                string localPackagePath = Path.Combine(ExpandedPath, string.Format("./{0}.{1}.nupkg", package.Id, package.Version));
                if (File.Exists(localPackagePath))
                {
                    NugetPackage localPackage = NugetPackage.FromNupkgFile(localPackagePath);
                    return(localPackage);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                string url = string.Format("{0}Packages(Id='{1}',Version='{2}')", ExpandedPath, package.Id, package.Version);
                try
                {
                    return(GetPackagesFromUrl(url, UserName, ExpandedPassword).First());
                }
                catch (Exception e)
                {
                    Debug.LogErrorFormat("Unable to retrieve package from {0}\n{1}", url, e.ToString());
                    return(null);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Gets a list of all available packages from a local source (not a web server) that match the given filters.
        /// </summary>
        /// <param name="searchTerm">The search term to use to filter packages. Defaults to the empty string.</param>
        /// <param name="includeAllVersions">True to include older versions that are not the latest version.</param>
        /// <param name="includePrerelease">True to include prerelease packages (alpha, beta, etc).</param>
        /// <param name="numberToGet">The number of packages to fetch.</param>
        /// <param name="numberToSkip">The number of packages to skip before fetching.</param>
        /// <returns>The list of available packages.</returns>
        private List <NugetPackage> GetLocalPackages(string searchTerm = "", bool includeAllVersions = false, bool includePrerelease = false, int numberToGet = 15, int numberToSkip = 0)
        {
            NugetHelper.LogVerbose("{0}: Getting local packages {1}", typeof(NugetPackageSource).Name, ExpandedPath);

            List <NugetPackage> localPackages = new List <NugetPackage>();

            if (numberToSkip != 0)
            {
                // we return the entire list the first time, so no more to add
                return(localPackages);
            }

            string path = ExpandedPath;

            if (Directory.Exists(path))
            {
                List <string> packagePaths = new List <string>();

                if (!IsLocalPathAndVersion33)                                                                  // 3.3-
                {
                    packagePaths.AddRange(Directory.GetFiles(path, string.Format("*{0}*.nupkg", searchTerm))); // adding loose packages
                }
                else // 3.3+
                {
                    // https://docs.microsoft.com/en-us/nuget/hosting-packages/local-feeds

                    // hierarchy of local feed
                    // 0.\\myserver\packages
                    // 1. └─<packageID>
                    // 2. └─<version>
                    // 3.   ├─<packageID>.<version>.nupkg
                    // 4.   └─<other files>

                    string aPackage = "";

                    // nuget v3 support
                    foreach (DirectoryInfo aDirPackageID in new DirectoryInfo(path).GetDirectories(string.Format("*{0}*", searchTerm))) // 0; for each package directory which matches the terms
                    {
                        NugetHelper.LogVerbose("{0}: Found a matching directory {1}", typeof(NugetPackageSource).Name, aDirPackageID);

                        aPackage = aDirPackageID.Name; // example: sinedustries.collections

                        //~ could optimize version checks while iterating?

                        foreach (DirectoryInfo aDirVersion in aDirPackageID.GetDirectories()) // 1; for each version directory for a package directory
                        {
                            NugetHelper.LogVerbose("{0}: Found a version directory {1}", typeof(NugetPackageSource).Name, aDirVersion);

                            aPackage = NugetPackage.PathLocal33Get(path, aDirPackageID.Name, aDirVersion.Name); // 2; path for package;

                            if (File.Exists(aPackage))                                                          // expected package exists
                            {
                                packagePaths.Add(aPackage);                                                     // add the package to paths
                                NugetHelper.LogVerbose("{0}: Added package {1}", typeof(NugetPackageSource).Name, aPackage);
                            }
                            else // no find path
                            {
                                throw new FileNotFoundException("Could not find NuGet package: {0};", aPackage);
                            }
                        }
                    }
                }

                foreach (var packagePath in packagePaths)
                {
                    var package = NugetPackage.FromNupkgFile(packagePath);
                    package.PackageSource = this;

                    if (package.IsPrerelease && !includePrerelease)
                    {
                        // if it's a prerelease package and we aren't supposed to return prerelease packages, just skip it
                        continue;
                    }

                    if (includeAllVersions)
                    {
                        // if all versions are being included, simply add it and move on
                        localPackages.Add(package);
                        NugetHelper.LogVerbose("Adding {0} {1}", package.Id, package.Version);
                        continue;
                    }

                    var existingPackage = localPackages.FirstOrDefault(x => x.Id == package.Id);
                    if (existingPackage != null)
                    {
                        // there is already a package with the same ID in the list
                        if (existingPackage < package)
                        {
                            // if the current package is newer than the existing package, swap them
                            localPackages.Remove(existingPackage);
                            localPackages.Add(package);
                        }
                    }
                    else
                    {
                        // there is no package with the same ID in the list yet
                        localPackages.Add(package);
                    }
                }
            }
            else
            {
                Debug.LogErrorFormat("Local folder not found: {0}", path);
            }

            return(localPackages);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
        /// <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);
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
0
        /// <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);
        }