Example #1
0
        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 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);
        }