private IEnumerable <PackageIdentity> CreatePackageIdentityFromNupkgPath() { PackageIdentity identity = null; IPackage package = null; try { // Example: install-package2 https://az320820.vo.msecnd.net/packages/microsoft.aspnet.mvc.4.0.20505.nupkg if (_isHttp) { PackageIdentity packageIdentity = ParsePackageIdentityFromHttpSource(Id); IPackageCacheRepository packageCache = this.Projects.FirstOrDefault().TryGetFeature <IPackageCacheRepository>(); IPackageName packageName = CoreConverters.SafeToPackageName(packageIdentity); SemanticVersion packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); Uri downloadUri = new Uri(Id); PackageDownloader downloader = new PackageDownloader(); // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } else { // Example: install-package2 c:\temp\packages\jQuery.1.10.2.nupkg string fullPath = Path.GetFullPath(Id); package = new OptimizedZipPackage(fullPath); } if (package != null) { Id = package.Id; Version = package.Version.ToString(); identity = new PackageIdentity(Id, NuGetVersion.Parse(Version)); } } catch (Exception ex) { Log(MessageLevel.Error, Resources.Cmdlet_FailToParsePackages, Id, ex.Message); } return(new List <PackageIdentity>() { identity }); }
internal void EnsurePackage(IPackageCacheRepository cacheRepository) { // OData caches instances of DataServicePackage while updating their property values. As a result, // the ZipPackage that we downloaded may no longer be valid (as indicated by a newer hash). // When using MachineCache, once we've verified that the hashes match (which happens the first time around), // we'll simply verify the file exists between successive calls. IPackageMetadata packageMetadata = this; bool refreshPackage = _package == null || (_package is OptimizedZipPackage && !((OptimizedZipPackage)_package).IsValid) || !String.Equals(OldHash, PackageHash, StringComparison.OrdinalIgnoreCase) || (_usingMachineCache && !cacheRepository.Exists(Id, packageMetadata.Version)); if (refreshPackage && TryGetPackage(cacheRepository, packageMetadata, out _package) && _package.GetHash(HashProvider).Equals(PackageHash, StringComparison.OrdinalIgnoreCase)) { OldHash = PackageHash; // Reset the flag so that we no longer need to download the package since it exists and is valid. refreshPackage = false; // Make a note that the backing store for the ZipPackage is the machine cache. _usingMachineCache = true; } if (refreshPackage) { // We either do not have a package available locally or they are invalid. Download the package from the server. _usingMachineCache = cacheRepository.InvokeOnPackage(packageMetadata.Id, packageMetadata.Version, (stream) => Downloader.DownloadPackage(DownloadUrl, this, stream) ); if (_usingMachineCache) { _package = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version); Debug.Assert(_package != null); } else { // this can happen when access to the %LocalAppData% directory is blocked, e.g. on Windows Azure Web Site build using (var targetStream = new MemoryStream()) { Downloader.DownloadPackage(DownloadUrl, this, targetStream); targetStream.Seek(0, SeekOrigin.Begin); _package = new ZipPackage(targetStream); } } OldHash = PackageHash; } }
internal void EnsurePackage(IPackageCacheRepository cacheRepository) { IPackageMetadata packageMetadata = this; if ((((this._package == null) || ((this._package is OptimizedZipPackage) && !((OptimizedZipPackage)this._package).IsValid)) || !string.Equals(this.OldHash, this.PackageHash, StringComparison.OrdinalIgnoreCase)) || (this._usingMachineCache && !cacheRepository.Exists(this.Id, packageMetadata.Version))) { IPackage package = null; bool flag = false; bool flag2 = false; if (TryGetPackage(cacheRepository, packageMetadata, out package) && this.MatchPackageHash(package)) { flag2 = true; } else { if (cacheRepository.InvokeOnPackage(packageMetadata.Id, packageMetadata.Version, stream => this.Downloader.DownloadPackage(this.DownloadUrl, this, stream))) { package = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version); } else { using (MemoryStream stream = new MemoryStream()) { this.Downloader.DownloadPackage(this.DownloadUrl, this, stream); stream.Seek(0L, SeekOrigin.Begin); package = new ZipPackage(stream); } flag = true; } flag2 = true; } if (!flag2) { object[] args = new object[] { this.Version, this.Id }; throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, NuGetResources.Error_InvalidPackage, args)); } this._package = package; this.Id = this._package.Id; this.Version = this._package.Version.ToString(); this._usingMachineCache = !flag; this.OldHash = this.PackageHash; } }
internal void EnsurePackage(IPackageCacheRepository cacheRepository) { // OData caches instances of DataServicePackage while updating their property values. As a result, // the ZipPackage that we downloaded may no longer be valid (as indicated by a newer hash). // When using MachineCache, once we've verified that the hashes match (which happens the first time around), // we'll simply verify the file exists between successive calls. IPackageMetadata packageMetadata = this; if (_package == null || (_package is OptimizedZipPackage && !((OptimizedZipPackage)_package).IsValid) || !String.Equals(OldHash, PackageHash, StringComparison.OrdinalIgnoreCase) || (_usingMachineCache && !cacheRepository.Exists(Id, packageMetadata.Version))) { IPackage newPackage = null; bool inMemOnly = false; bool isValid = false; // If the package exists in the cache and has the correct hash then use it. Otherwise download it. if (TryGetPackage(cacheRepository, packageMetadata, out newPackage) && MatchPackageHash(newPackage)) { isValid = true; } else { // We either do not have a package available locally or they are invalid. Download the package from the server. if (cacheRepository.InvokeOnPackage(packageMetadata.Id, packageMetadata.Version, (stream) => Downloader.DownloadPackage(DownloadUrl, this, stream))) { newPackage = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version); Debug.Assert(newPackage != null); } else { // this can happen when access to the %LocalAppData% directory is blocked, e.g. on Windows Azure Web Site build using (var targetStream = new MemoryStream()) { Downloader.DownloadPackage(DownloadUrl, this, targetStream); targetStream.Seek(0, SeekOrigin.Begin); newPackage = new ZipPackage(targetStream); } inMemOnly = true; } // Because of CDN caching, the hash returned in odata feed // can be out of sync with the hash of the file itself. // So for now, we cannot call MatchPackageHash(newPackage) to // validate that the file downloaded has the right hash. isValid = true; } // apply the changes if the package hash was valid if (isValid) { _package = newPackage; // Make a note that the backing store for the ZipPackage is the machine cache. _usingMachineCache = !inMemOnly; OldHash = PackageHash; } else { // ensure package must end with a valid package, since we cannot load one we must throw. throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, NuGetResources.Error_InvalidPackage, Version, Id)); } } }
private IPackage DownloadToMachineCache( IPackageCacheRepository cacheRepository, PackageIdentity package, DataServicePackageRepository repository, Uri downloadUri, NuGet.Common.ILogger logger, CancellationToken token) { var packageName = new PackageNameWrapper(package); var version = SemanticVersion.Parse(package.Version.ToString()); IPackage newPackage = null; FileInfo tmpFile = null; FileStream tmpFileStream = null; // Create a v2 http client var downloadClient = new HttpClient(downloadUri) { UserAgent = UserAgent.UserAgentString }; EventHandler <ProgressEventArgs> progressHandler = (sender, progress) => { // Throw if this was canceled. This will stop the download. token.ThrowIfCancellationRequested(); }; Exception downloadException = null; Action <Stream> downloadAction = (stream) => { try { repository.PackageDownloader.ProgressAvailable += progressHandler; repository.PackageDownloader.DownloadPackage(downloadClient, packageName, stream); } catch (Exception ex) when(ex is OperationCanceledException || ex is IOException && ex.InnerException is SocketException) { // The task was canceled. To avoid writing a partial file to the machine cache // we need to clear out the current tmp file stream so that it will be ignored. stream.SetLength(0); // If the machine cache is using the physical file system we can find the // path of temp file and clean it up. Otherwise NuGet.Core will just leave the temp file. tmpFileStream = stream as FileStream; if (tmpFileStream != null) { tmpFile = new FileInfo(tmpFileStream.Name); } downloadException = ex; } catch (Exception ex) { downloadException = ex; } finally { repository.PackageDownloader.ProgressAvailable -= progressHandler; } }; // We either do not have a package available locally or they are invalid. // Download the package from the server. if (cacheRepository.InvokeOnPackage(package.Id, version, (stream) => downloadAction(stream))) { if (!token.IsCancellationRequested) { newPackage = cacheRepository.FindPackage(package.Id, version); Debug.Assert(newPackage != null); } } // After the stream is no longer in use, delete the tmp file // NuGet.Core does not properly clean these up since it does not have cancel support. if (tmpFile != null && token.IsCancellationRequested && tmpFile.Exists) { try { tmpFile.Delete(); } catch { // Ignore exceptions for tmp file clean up } } if (downloadException != null) { throw downloadException; } return(newPackage); }
internal static IPackage GetPackage(IPackageCacheRepository packageCache, PackageIdentity packageIdentity, Uri downloadUri) { var packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); var packageName = CoreConverters.SafeToPackageName(packageIdentity); var downloader = new PackageDownloader(); IPackage package = null; if (packageCache != null) { package = packageCache.FindPackage(packageName.Id, packageSemVer); if (package != null) { NuGetTraceSources.ActionExecutor.Info( "download/cachehit", "[{0}] Download: Cache Hit!", packageIdentity); // Success! return(package); } if (downloadUri == null) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Strings.DownloadActionHandler_NoDownloadUrl, packageIdentity)); } // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { NuGetTraceSources.ActionExecutor.Info( "download/downloadedtocache", "[{0}] Download: Downloaded to cache", packageName); // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } // Either: // 1. We failed to load the package into the cache, which can happen when // access to the %LocalAppData% directory is blocked, // e.g. on Windows Azure Web Site build OR // B. It was purged from the cache before it could be retrieved again. // Regardless, the cache isn't working for us, so download it in to memory. if (package == null) { NuGetTraceSources.ActionExecutor.Info( "download/cachefailing", "[{0}] Download: Cache isn't working. Downloading to RAM", packageName); using (var targetStream = new MemoryStream()) { downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream); targetStream.Seek(0, SeekOrigin.Begin); package = new ZipPackage(targetStream); } } return(package); }
internal static IPackage GetPackage(IPackageCacheRepository packageCache, PackageIdentity packageIdentity, Uri downloadUri) { var packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version); var packageName = CoreConverters.SafeToPackageName(packageIdentity); var downloader = new PackageDownloader(); IPackage package = null; if (packageCache != null) { package = packageCache.FindPackage(packageName.Id, packageSemVer); if (package != null) { NuGetTraceSources.ActionExecutor.Info( "download/cachehit", "[{0}] Download: Cache Hit!", packageIdentity); // Success! return package; } if (downloadUri == null) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Strings.DownloadActionHandler_NoDownloadUrl, packageIdentity)); } // Try to download the package through the cache. bool success = packageCache.InvokeOnPackage( packageIdentity.Id, packageSemVer, (targetStream) => downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream)); if (success) { NuGetTraceSources.ActionExecutor.Info( "download/downloadedtocache", "[{0}] Download: Downloaded to cache", packageName); // Try to get it from the cache again package = packageCache.FindPackage(packageIdentity.Id, packageSemVer); } } // Either: // 1. We failed to load the package into the cache, which can happen when // access to the %LocalAppData% directory is blocked, // e.g. on Windows Azure Web Site build OR // B. It was purged from the cache before it could be retrieved again. // Regardless, the cache isn't working for us, so download it in to memory. if (package == null) { NuGetTraceSources.ActionExecutor.Info( "download/cachefailing", "[{0}] Download: Cache isn't working. Downloading to RAM", packageName); using (var targetStream = new MemoryStream()) { downloader.DownloadPackage( new HttpClient(downloadUri), packageName, targetStream); targetStream.Seek(0, SeekOrigin.Begin); package = new ZipPackage(targetStream); } } return package; }