MyGetMirror.NuGetPackageMirrorCommand.MirrorAsync C# (CSharp) Method

MirrorAsync() public method

public MirrorAsync ( PackageIdentity identity, CancellationToken token ) : Task
identity PackageIdentity
token System.Threading.CancellationToken
return Task
        public async Task<bool> MirrorAsync(PackageIdentity identity, CancellationToken token)
        {
            // Publish the package itself.
            var publishPackage = true;

            if (!_overwriteExisting)
            {
                publishPackage = !(await _existenceChecker.PackageExistsAsync(identity, token));
            }

            if (publishPackage)
            {
                var downloadResult = await _sourceDownloadResource.GetDownloadResourceResultAsync(
                    identity,
                    _settings,
                    _logger,
                    token);

                using (downloadResult)
                {
                    if (downloadResult.Status != DownloadResourceResultStatus.Available)
                    {
                        throw new InvalidOperationException($"The NuGet package '{identity}' is not available on the source.");
                    }

                    await _packagePusher.PushAsync(downloadResult.PackageStream, token);
                }
            }

            // Publish the symbols package.
            var publishSymbolsPackage = _includeSymbols;

            if (!_overwriteExisting)
            {
                publishSymbolsPackage = !(await _existenceChecker.SymbolsPackageExistsAsync(identity, token));
            }

            if (publishSymbolsPackage)
            {
                publishSymbolsPackage = await _symbolsPackageDownloader.ProcessAsync(
                    identity,
                    async streamResult =>
                    {
                        if (!streamResult.IsAvailable)
                        {
                            // The package has no symbols package.
                            return false;
                        }

                        await _packagePusher.PushAsync(streamResult.Stream, token);

                        return true;
                    },
                    token);
            }

            return publishPackage || publishSymbolsPackage;
        }
    }

Usage Example

Exemplo n.º 1
0
        private async Task ProcessPackageIdentitiesAsync(string taskName, ConcurrentBag <PackageIdentity> packageIdentities, ManualResetEventSlim isFullyEnumerated, CancellationToken token)
        {
            while (true)
            {
                PackageIdentity identity;

                if (packageIdentities.TryTake(out identity))
                {
                    var stopwatch = Stopwatch.StartNew();
                    var pushed    = await _mirror.MirrorAsync(identity, token);

                    if (pushed)
                    {
                        _logger.LogInformationSummary($"[ {taskName} ] NuGet package {identity} took {stopwatch.Elapsed.TotalSeconds:0.00} seconds to publish.");
                    }
                    else
                    {
                        _logger.LogInformationSummary($"[ {taskName} ] NuGet package {identity} took {stopwatch.Elapsed.TotalSeconds:0.00} seconds to detect no push was necessary.");
                    }

                    _logger.LogInformationSummary($"[ {taskName} ] {packageIdentities.Count} NuGet package(s) remain in the queue.");
                }
                else
                {
                    if (isFullyEnumerated.IsSet)
                    {
                        // The concurrent bag is empty and we are done enumerating, so the task
                        // can safely terminate.
                        break;
                    }
                    else
                    {
                        // The enumeration is not done yet, so wait a little bit for more work to
                        // become available.
                        await Task.Delay(TimeSpan.FromMilliseconds(50));
                    }
                }
            }
        }