예제 #1
0
        public StoredPackage GetPackage(PhysicalPackageMetadata metadata)
        {
            fileSystem.EnsureDirectoryExists(rootDirectory);

            foreach (var file in PackageFiles(metadata.PackageAndVersionSearchPattern))
            {
                var storedPackage = GetPackage(file);
                if (storedPackage == null)
                {
                    continue;
                }

                if (!string.Equals(storedPackage.Metadata.PackageId, metadata.PackageId, StringComparison.OrdinalIgnoreCase) ||
                    !VersionFactory.TryCreateVersion(storedPackage.Metadata.Version, out IVersion packageVersion, metadata.VersionFormat) ||
                    !packageVersion.Equals(VersionFactory.CreateVersion(metadata.Version, metadata.VersionFormat)))
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(metadata.Hash))
                {
                    return(storedPackage);
                }

                if (metadata.Hash == storedPackage.Metadata.Hash)
                {
                    return(storedPackage);
                }
            }

            return(null);
        }
예제 #2
0
        // ReSharper disable UnusedParameter.Local
        static void CheckArguments(
            string packageId,
            string packageVersion,
            string feedId,
            string feedUri,
            string feedUsername,
            string feedPassword,
            string maxDownloadAttempts,
            string attemptBackoffSeconds,
            out IVersion version,
            out Uri uri,
            out int parsedMaxDownloadAttempts,
            out TimeSpan parsedAttemptBackoff)
        {
            Guard.NotNullOrWhiteSpace(packageId, "No package ID was specified. Please pass --packageId YourPackage");
            Guard.NotNullOrWhiteSpace(packageVersion, "No package version was specified. Please pass --packageVersion 1.0.0.0");
            Guard.NotNullOrWhiteSpace(feedId, "No feed ID was specified. Please pass --feedId feed-id");
            Guard.NotNullOrWhiteSpace(feedUri, "No feed URI was specified. Please pass --feedUri https://url/to/nuget/feed");

            var packageMetadata = new MetadataFactory().GetMetadataFromPackageID(packageId);

            if (!VersionFactory.TryCreateVersion(packageVersion, out version, packageMetadata.VersionFormat))
            {
                throw new CommandException($"Package version '{packageVersion}' specified is not a valid version string");
            }

            if (!Uri.TryCreate(feedUri, UriKind.Absolute, out uri))
            {
                throw new CommandException($"URI specified '{feedUri}' is not a valid URI");
            }

            if (!String.IsNullOrWhiteSpace(feedUsername) && String.IsNullOrWhiteSpace(feedPassword))
            {
                throw new CommandException("A username was specified but no password was provided. Please pass --feedPassword \"FeedPassword\"");
            }

            if (!int.TryParse(maxDownloadAttempts, out parsedMaxDownloadAttempts))
            {
                throw new CommandException($"The requested number of download attempts '{maxDownloadAttempts}' is not a valid integer number");
            }

            if (parsedMaxDownloadAttempts <= 0)
            {
                throw new CommandException("The requested number of download attempts should be more than zero");
            }

            int parsedAttemptBackoffSeconds;

            if (!int.TryParse(attemptBackoffSeconds, out parsedAttemptBackoffSeconds))
            {
                throw new CommandException($"Retry requested download attempt retry backoff '{attemptBackoffSeconds}' is not a valid integer number of seconds");
            }

            if (parsedAttemptBackoffSeconds < 0)
            {
                throw new CommandException("The requested download attempt retry backoff should be a positive integer number of seconds");
            }

            parsedAttemptBackoff = TimeSpan.FromSeconds(parsedAttemptBackoffSeconds);
        }
예제 #3
0
 /// <summary>
 /// Matches a file to a package id and version
 /// </summary>
 /// <param name="file">The path of the file we are checking</param>
 /// <param name="packageId">The desired package id</param>
 /// <param name="version">The desired version</param>
 /// <returns>true if the file matches the pacakge id and version, and false otherwise</returns>
 bool FileMatchesDetails(string file, string packageId, IVersion version)
 {
     return(PackageIdParser.TryGetMetadataFromServerPackageName(file).ToEnumerable()
            .Where(meta => meta != Maybe <PackageMetadata> .None)
            .Where(meta => meta.Value.PackageId == packageId)
            .Any(meta => VersionFactory.TryCreateVersion(meta.Value.Version.ToString(),
                                                         out IVersion packageVersion, meta.Value.VersionFormat) &&
                 version.Equals(packageVersion)));
 }
        static bool FileNameMatchesPattern(string packageId, IVersion version, string path)
        {
            var name = Path.GetFileNameWithoutExtension(path);

            // When matching by pattern, we will always have a version token. Packages without versions would be matched early on by the version-less path resolver
            // when doing an exact match.
            return(name.Length > packageId.Length &&
                   VersionFactory.TryCreateVersion(
                       name.Substring(packageId.Length + 1),
                       out IVersion parsedVersion,
                       PackageMetadataFactory.GetMetadataFromPackageID(packageId).VersionFormat) &&
                   parsedVersion.Equals(version));
        }
        private void AttemptToGetPackageFromCache(string packageId, IVersion version, string cacheDirectory,
                                                  out LocalNuGetPackage downloaded, out string downloadedTo)
        {
            downloaded   = null;
            downloadedTo = null;

            Log.VerboseFormat("Checking package cache for package {0} {1}", packageId, version.ToString());

            var name = GetNameOfPackage(packageId, version.ToString());

            fileSystem.EnsureDirectoryExists(cacheDirectory);

            var files = fileSystem.EnumerateFilesRecursively(cacheDirectory, name + "*.nupkg");

            foreach (var file in files)
            {
                var package = ReadPackageFile(file);
                if (package == null)
                {
                    continue;
                }


                var idMatches         = string.Equals(package.Metadata.Id, packageId, StringComparison.OrdinalIgnoreCase);
                var versionExactMatch = string.Equals(package.Metadata.Version.ToString(), version.ToString(),
                                                      StringComparison.OrdinalIgnoreCase);

                var packageMetadata = new MetadataFactory().GetMetadataFromPackageID(packageId);
                var nugetVerMatches = VersionFactory.TryCreateVersion(package.Metadata.Version.ToString(),
                                                                      out IVersion packageVersion, packageMetadata.VersionFormat) &&
                                      version.Equals(packageVersion);

                if (idMatches && (nugetVerMatches || versionExactMatch))
                {
                    downloaded   = package;
                    downloadedTo = file;
                    return;
                }
            }
        }