public static void DownloadPackage(string packageId, IVersion version, Uri feedUri, string targetFilePath)
        {
            if (!Directory.Exists(feedUri.LocalPath))
            {
                throw new Exception($"Path does not exist: '{feedUri}'");
            }

            // Lookup files which start with the name "<Id>." and attempt to match it with all possible version string combinations (e.g. 1.2.0, 1.2.0.0)
            // before opening the package. To avoid creating file name strings, we attempt to specifically match everything after the last path separator
            // which would be the file name and extension.
            var package = (from path in GetPackageLookupPaths(packageId, version, feedUri)
                           let p = new LocalNuGetPackage(path)
                                   where p.Metadata.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase) &&
                                   VersionFactory.CreateVersion(p.Metadata.Version.ToString(), PackageMetadataFactory.GetMetadataFromPackageID(packageId).VersionFormat).Equals(version)
                                   select p).FirstOrDefault();

            if (package == null)
            {
                throw new Exception($"Could not find package {packageId} {version} in feed: '{feedUri}'");
            }

            Log.VerboseFormat("Found package {0} version {1}", package.Metadata.Id, package.Metadata.Version);
            Log.Verbose("Downloading to: " + targetFilePath);

            using (var targetFile = File.Open(targetFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                package.GetStream(stream =>
                {
                    stream.CopyTo(targetFile);
                });
            }
        }
예제 #2
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);
        }