Ejemplo n.º 1
0
        public static async Task <NupkgEntry> OpenNupkgStreamAsync(
            HttpSource httpSource,
            PackageInfo package,
            TimeSpan cacheAgeLimit,
            Reports reports)
        {
            for (int retry = 0; retry != 3; ++retry)
            {
                try
                {
                    using (var data = await httpSource.GetAsync(
                               package.ContentUri,
                               cacheKey: $"nupkg_{package.Id}.{package.Version}",
                               cacheAgeLimit: retry == 0 ? cacheAgeLimit : TimeSpan.Zero,
                               ensureValidContents: stream => EnsureValidPackageContents(stream, package)))
                    {
                        return(new NupkgEntry
                        {
                            TempFileName = data.CacheFileName
                        });
                    }
                }
                catch (Exception ex)
                {
                    var isFinalAttempt = (retry == 2);
                    var message        = ex.Message;
                    if (ex is TaskCanceledException)
                    {
                        message = ErrorMessageUtils.GetFriendlyTimeoutErrorMessage(ex as TaskCanceledException, isFinalAttempt, ignoreFailure: false);
                    }

                    if (isFinalAttempt)
                    {
                        reports.Error.WriteLine(
                            $"Error: DownloadPackageAsync: {package.ContentUri}{Environment.NewLine}  {message}".Red().Bold());
                        throw;
                    }
                    else
                    {
                        reports.Information.WriteLine(
                            $"Warning: DownloadPackageAsync: {package.ContentUri}{Environment.NewLine}  {message}".Yellow().Bold());
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        public static async Task<NupkgEntry> OpenNupkgStreamAsync(
            HttpSource httpSource,
            PackageInfo package,
            TimeSpan cacheAgeLimit,
            Reports reports)
        {
            for (int retry = 0; retry != 3; ++retry)
            {
                try
                {
                    using (var data = await httpSource.GetAsync(
                        package.ContentUri,
                        cacheKey: $"nupkg_{package.Id}.{package.Version}",
                        cacheAgeLimit: retry == 0 ? cacheAgeLimit : TimeSpan.Zero,
                        ensureValidContents: stream => EnsureValidPackageContents(stream, package)))
                    {
                        return new NupkgEntry
                        {
                            TempFileName = data.CacheFileName
                        };
                    }
                }
                catch (Exception ex)
                {
                    var isFinalAttempt = (retry == 2);
                    var message = ex.Message;
                    if (ex is TaskCanceledException)
                    {
                        message = ErrorMessageUtils.GetFriendlyTimeoutErrorMessage(ex as TaskCanceledException, isFinalAttempt, ignoreFailure: false);
                    }

                    if (isFinalAttempt)
                    {
                        reports.Error.WriteLine(
                            $"Error: DownloadPackageAsync: {package.ContentUri}{Environment.NewLine}  {message}".Red().Bold());
                        throw;
                    }
                    else
                    {
                        reports.Information.WriteLine(
                            $"Warning: DownloadPackageAsync: {package.ContentUri}{Environment.NewLine}  {message}".Yellow().Bold());
                    }
                }
            }
            return null;
        }
Ejemplo n.º 3
0
        public static IPackageFeed CreatePackageFeed(PackageSource source, bool noCache, bool ignoreFailedSources,
            Reports reports)
        {
            if (new Uri(source.Source).IsFile)
            {
                return PackageFolderFactory.CreatePackageFolderFromPath(source.Source, ignoreFailedSources, reports);
            }
            else
            {
                var httpSource = new HttpSource(
                    source.Source,
                    source.UserName,
                    source.Password,
                    reports);

                Uri packageBaseAddress;
                if (NuGetv3Feed.DetectNuGetV3(httpSource, noCache, out packageBaseAddress))
                {
                    if (packageBaseAddress == null)
                    {
                        reports.Information.WriteLine(
                            $"Ignoring NuGet v3 feed {source.Source.Yellow().Bold()}, which doesn't provide PackageBaseAddress resource.");
                        return null;
                    }

                    httpSource = new HttpSource(
                        packageBaseAddress.AbsoluteUri,
                        source.UserName,
                        source.Password,
                        reports);

                    return new NuGetv3Feed(
                        httpSource,
                        noCache,
                        reports,
                        ignoreFailedSources);
                }

                return new NuGetv2Feed(
                    httpSource,
                    noCache,
                    reports,
                    ignoreFailedSources);
            }
        }
Ejemplo n.º 4
0
 public PackageFeed(
     string baseUri,
     string userName,
     string password,
     bool noCache,
     IReport report)
 {
     _baseUri = baseUri.EndsWith("/") ? baseUri : (baseUri + "/");
     _report = report;
     _httpSource = new HttpSource(baseUri, userName, password, report);
     if (noCache)
     {
         _cacheAgeLimitList = TimeSpan.Zero;
         _cacheAgeLimitNupkg = TimeSpan.Zero;
     }
     else
     {
         _cacheAgeLimitList = TimeSpan.FromMinutes(30);
         _cacheAgeLimitNupkg = TimeSpan.FromHours(24);
     }
 }
Ejemplo n.º 5
0
 internal NuGetv3Feed(
     HttpSource httpSource,
     bool noCache,
     Reports reports,
     bool ignoreFailure)
 {
     _baseUri = httpSource.BaseUri;
     _reports = reports;
     _httpSource = httpSource;
     _ignoreFailure = ignoreFailure;
     if (noCache)
     {
         _cacheAgeLimitList = TimeSpan.Zero;
         _cacheAgeLimitNupkg = TimeSpan.Zero;
     }
     else
     {
         _cacheAgeLimitList = TimeSpan.FromMinutes(30);
         _cacheAgeLimitNupkg = TimeSpan.FromHours(24);
     }
 }
Ejemplo n.º 6
0
 internal NuGetv2Feed(
     HttpSource httpSource,
     bool noCache,
     Reports reports,
     bool ignoreFailure)
 {
     _baseUri       = httpSource.BaseUri;
     _reports       = reports;
     _httpSource    = httpSource;
     _ignoreFailure = ignoreFailure;
     if (noCache)
     {
         _cacheAgeLimitList  = TimeSpan.Zero;
         _cacheAgeLimitNupkg = TimeSpan.Zero;
     }
     else
     {
         _cacheAgeLimitList  = TimeSpan.FromMinutes(30);
         _cacheAgeLimitNupkg = TimeSpan.FromHours(24);
     }
 }
Ejemplo n.º 7
0
 public PackageFeed(
     string baseUri,
     string userName,
     string password,
     bool noCache,
     IReport report)
 {
     _baseUri    = baseUri.EndsWith("/") ? baseUri : (baseUri + "/");
     _report     = report;
     _httpSource = new HttpSource(baseUri, userName, password, report);
     if (noCache)
     {
         _cacheAgeLimitList  = TimeSpan.Zero;
         _cacheAgeLimitNupkg = TimeSpan.Zero;
     }
     else
     {
         _cacheAgeLimitList  = TimeSpan.FromMinutes(30);
         _cacheAgeLimitNupkg = TimeSpan.FromHours(24);
     }
 }
Ejemplo n.º 8
0
 public static async Task<NupkgEntry> OpenNupkgStreamAsync(
     HttpSource httpSource,
     PackageInfo package,
     TimeSpan cacheAgeLimit,
     Reports reports)
 {
     for (int retry = 0; retry != 3; ++retry)
     {
         try
         {
             using (var data = await httpSource.GetAsync(
                 package.ContentUri,
                 cacheKey: $"nupkg_{package.Id}.{package.Version}",
                 cacheAgeLimit: retry == 0 ? cacheAgeLimit : TimeSpan.Zero,
                 ensureValidContents: stream => EnsureValidPackageContents(stream, package)))
             {
                 return new NupkgEntry
                 {
                     TempFileName = data.CacheFileName
                 };
             }
         }
         catch (Exception ex)
         {
             if (retry == 2)
             {
                 reports.Error.WriteLine(
                     $"Error: DownloadPackageAsync: {package.ContentUri}{Environment.NewLine}  {ex.Message}".Red().Bold());
                 throw;
             }
             else
             {
                 reports.Information.WriteLine(
                     $"Warning: DownloadPackageAsync: {package.ContentUri}{Environment.NewLine}  {ex.Message}".Yellow().Bold());
             }
         }
     }
     return null;
 }
Ejemplo n.º 9
0
        internal static bool DetectNuGetV3(HttpSource httpSource, bool noCache, out Uri packageBaseAddress)
        {
            var cacheAgeLimit = noCache ? TimeSpan.Zero : TimeSpan.FromDays(7);

            try
            {
                var result = httpSource.GetAsync(httpSource.BaseUri, "index_json", cacheAgeLimit).Result;
                using (var reader = new JsonTextReader(new StreamReader(result.Stream)))
                {
                    var indexJson = JObject.Load(reader);
                    foreach (var resource in indexJson["resources"])
                    {
                        var type = resource.Value <string>("@type");
                        var id   = resource.Value <string>("@id");

                        if (id != null && string.Equals(type, "PackageBaseAddress/3.0.0"))
                        {
                            try
                            {
                                packageBaseAddress = new Uri(id);
                                return(true);
                            }
                            catch (UriFormatException)
                            {
                                packageBaseAddress = new Uri(new Uri(httpSource.BaseUri), id);
                                return(true);
                            }
                        }
                    }
                }
                packageBaseAddress = null;
                return(true);
            }
            catch
            {
                packageBaseAddress = null;
                return(false);
            }
        }
Ejemplo n.º 10
0
        internal static bool DetectNuGetV3(HttpSource httpSource, bool noCache, out Uri packageBaseAddress)
        {
            var cacheAgeLimit = noCache ? TimeSpan.Zero : TimeSpan.FromDays(7);
            try
            {
                var result = httpSource.GetAsync(httpSource.BaseUri, "index_json", cacheAgeLimit).Result;
                using (var reader = new JsonTextReader(new StreamReader(result.Stream)))
                {
                    var indexJson = JObject.Load(reader);
                    foreach (var resource in indexJson["resources"])
                    {
                        var type = resource.Value<string>("@type");
                        var id = resource.Value<string>("@id");

                        if (id != null && string.Equals(type, "PackageBaseAddress/3.0.0"))
                        {
                            try
                            {
                                packageBaseAddress = new Uri(id);
                                return true;
                            }
                            catch (UriFormatException)
                            {
                                packageBaseAddress = new Uri(new Uri(httpSource.BaseUri), id);
                                return true;
                            }
                        }
                    }
                }
                packageBaseAddress = null;
                return true;
            }
            catch
            {
                packageBaseAddress = null;
                return false;
            }
        }
Ejemplo n.º 11
0
 public NuGetv2Feed(
     string baseUri,
     string userName,
     string password,
     bool noCache,
     Reports reports,
     bool ignoreFailure)
 {
     _baseUri       = baseUri.EndsWith("/") ? baseUri : (baseUri + "/");
     _reports       = reports;
     _httpSource    = new HttpSource(baseUri, userName, password, reports);
     _ignoreFailure = ignoreFailure;
     if (noCache)
     {
         _cacheAgeLimitList  = TimeSpan.Zero;
         _cacheAgeLimitNupkg = TimeSpan.Zero;
     }
     else
     {
         _cacheAgeLimitList  = TimeSpan.FromMinutes(30);
         _cacheAgeLimitNupkg = TimeSpan.FromHours(24);
     }
     Source = baseUri;
 }
Ejemplo n.º 12
0
 public NuGetv2Feed(
     string baseUri,
     string userName,
     string password,
     bool noCache,
     Reports reports,
     bool ignoreFailure)
 {
     _baseUri = baseUri.EndsWith("/") ? baseUri : (baseUri + "/");
     _reports = reports;
     _httpSource = new HttpSource(baseUri, userName, password, reports);
     _ignoreFailure = ignoreFailure;
     if (noCache)
     {
         _cacheAgeLimitList = TimeSpan.Zero;
         _cacheAgeLimitNupkg = TimeSpan.Zero;
     }
     else
     {
         _cacheAgeLimitList = TimeSpan.FromMinutes(30);
         _cacheAgeLimitNupkg = TimeSpan.FromHours(24);
     }
     Source = baseUri;
 }