Exemplo n.º 1
0
        private async Task <IEnumerable <string> > GetPackageFileListAsync(DefaultConfigUpdater defaultConfigUpdater, PackageUpdateResult packageUpdate, CancellationToken?cancellationToken = null)
        {
            string?link = defaultConfigUpdater.GetPackageLink(packageUpdate.PackageId);

            if (link == null)
            {
                // We don't know where nuget package should be extracted to.
                return(Enumerable.Empty <string>());
            }

            SourceCacheContext cache          = new SourceCacheContext();
            NuGetVersion       packageVersion = new NuGetVersion(packageUpdate.NewVersion);

            // We have to specify some cancellation token, if caller doesn't provide one
            CancellationTokenSource?ownedCancellationToken = cancellationToken == null ? new CancellationTokenSource() : null;

            // Stream that will receive the package bytes
            using MemoryStream packageStream = new MemoryStream();

            try
            {
                SourceRepository rep = Repository.Factory.GetCoreV3(_feed, FeedType.HttpV3);
                if (_credentials != null)
                {
                    rep.PackageSource.Credentials = _credentials;
                }

                FindPackageByIdResource resource = await rep.GetResourceAsync <FindPackageByIdResource>().ConfigureAwait(false);

                Trace.WriteLine($"Downloading package {packageUpdate.PackageId}-{packageUpdate.NewVersion} into memory.");

                bool downloadResult = await resource.CopyNupkgToStreamAsync(
                    packageUpdate.PackageId,
                    packageVersion,
                    packageStream,
                    cache,
                    NullLogger.Instance,
                    cancellationToken ?? ownedCancellationToken !.Token)
                                      .ConfigureAwait(false);

                if (!downloadResult)
                {
                    Trace.WriteLine($"There is an issue downloading nuget package: {packageUpdate.PackageId}");
                    return(Enumerable.Empty <string>());
                }

                Trace.WriteLine($"Downloading complete for package {packageUpdate.PackageId}.");
                return(GetNugetFileList(packageStream, link));
            }
            catch (Exception e)
            {
                Trace.WriteLine($"There is an issue downloading nuget package.");
                Trace.WriteLine(e.ToString());
                return(Enumerable.Empty <string>());
            }
            finally
            {
                ownedCancellationToken?.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deduces the value of the variables in swr files
        /// </summary>
        /// <param name="defaultConfigUpdater"><see cref="DefaultConfigUpdater"/> that will provide extract locations for packages.</param>
        /// <param name="packages">Nuget packages that should be downloaded and used.</param>
        /// <param name="swrFiles">Files, containing the variables.</param>
        /// <param name="deducedVariablesList">List of variables, value of which was found.</param>
        /// <param name="outcomeDetails">Detail string, explaining the outcome of the operation.</param>
        /// <param name="maximumWaitDuration">Maximum duration to wait for nuget downloads to complete.</param>
        /// <returns>True if operation succeeded. False otherwise</returns>
        public bool DeduceVariableValues(DefaultConfigUpdater defaultConfigUpdater, IEnumerable <PackageUpdateResult> packages,
                                         SwrFile[] swrFiles, out List <PropsFileVariableReference> deducedVariablesList, out string outcomeDetails, TimeSpan?maximumWaitDuration = null)
        {
            using CancellationTokenSource tokenSource = new CancellationTokenSource();
            ConcurrentBag <PropsFileVariableReference> deducedVariables = new ConcurrentBag <PropsFileVariableReference>();

            TransformManyBlock <PackageUpdateResult, string> nugetDownloadBlock = new TransformManyBlock <PackageUpdateResult, string>(async(packageUpdate) =>
            {
                return(await GetPackageFileListAsync(defaultConfigUpdater, packageUpdate, tokenSource.Token).ConfigureAwait(false));
            });

            ActionBlock <string> filenameMatchBlock = new ActionBlock <string>((filename) =>
            {
                foreach (PropsFileVariableReference varReference in MatchFileNames(swrFiles, filename))
                {
                    deducedVariables.Add(varReference);
                }
            });

            nugetDownloadBlock.LinkTo(filenameMatchBlock, new DataflowLinkOptions()
            {
                PropagateCompletion = true
            });

            foreach (PackageUpdateResult package in packages)
            {
                nugetDownloadBlock.Post(package);
            }

            nugetDownloadBlock.Complete();
            bool executedToCompletion = filenameMatchBlock.Completion.Wait(maximumWaitDuration ?? TimeSpan.FromMilliseconds(-1));

            if (executedToCompletion == false)
            {
                outcomeDetails = "Operation timed out. Failed to download and process all the nuget packages in time.";
                tokenSource.Cancel();
            }
            else
            {
                outcomeDetails = string.Empty;
            }

            deducedVariablesList = deducedVariables.ToList();
            return(executedToCompletion);
        }