コード例 #1
0
ファイル: DeleteCommand.cs プロジェクト: gmfx/Sleet
        public static async Task <bool> RunAsync(LocalSettings settings, ISleetFileSystem source, string packageId, string version, string reason, bool force, ILogger log)
        {
            var success = true;

            var token = CancellationToken.None;
            var now   = DateTimeOffset.UtcNow;

            log.LogMinimal($"Reading feed {source.BaseURI.AbsoluteUri}");

            // Check if already initialized
            using (var feedLock = await SourceUtility.VerifyInitAndLock(settings, source, log, token))
            {
                // Validate source
                await SourceUtility.ValidateFeedForClient(source, log, token);

                // Get sleet.settings.json
                var sourceSettings = await FeedSettingsUtility.GetSettingsOrDefault(source, log, token);

                // Settings context used for all operations
                var context = new SleetContext()
                {
                    LocalSettings  = settings,
                    SourceSettings = sourceSettings,
                    Log            = log,
                    Source         = source,
                    Token          = token
                };

                var packageIndex = new PackageIndex(context);

                var packages = new List <PackageIdentity>();

                if (!string.IsNullOrEmpty(version))
                {
                    // Delete a single version of the package
                    var packageVersion = NuGetVersion.Parse(version);

                    packages.Add(new PackageIdentity(packageId, packageVersion));
                }
                else
                {
                    // Delete all versions of the package
                    packages.AddRange(await packageIndex.GetPackagesByIdAsync(packageId));
                }

                if (string.IsNullOrEmpty(reason))
                {
                    reason = string.Empty;
                }

                foreach (var package in packages)
                {
                    var exists = await packageIndex.Exists(package);

                    var symbolsExists = await packageIndex.SymbolsExists(package);

                    if (!exists && !symbolsExists)
                    {
                        log.LogInformation($"{package.ToString()} does not exist.");

                        if (force)
                        {
                            // ignore failures
                            continue;
                        }
                        else
                        {
                            throw new InvalidOperationException($"Package does not exists: {package.ToString()}");
                        }
                    }

                    var message = $"Removing {package.ToString()}";

                    if (exists && symbolsExists)
                    {
                        message = $"Removing {package.ToString()} and symbols package for {package.ToString()}";
                    }
                    else if (symbolsExists)
                    {
                        message = $"Removing symbols package {package.ToString()}";
                    }

                    await log.LogAsync(LogLevel.Information, message);

                    await SleetUtility.RemovePackage(context, package);
                }

                // Save all
                log.LogMinimal($"Committing changes to {source.BaseURI.AbsoluteUri}");

                success &= await source.Commit(log, token);
            }

            if (success)
            {
                log.LogMinimal($"Successfully deleted packages.");
            }
            else
            {
                log.LogError($"Failed to delete packages.");
            }

            return(success);
        }
コード例 #2
0
        private static async Task PushPackage(PackageInput package, SleetContext context, PackageIndex packageIndex, bool force, bool skipExisting, ILogger log)
        {
            var packageString = $"{package.Identity.Id} {package.Identity.Version.ToFullString()}";

            if (package.IsSymbolsPackage)
            {
                packageString += " Symbols";

                if (!context.SourceSettings.SymbolsEnabled)
                {
                    await log.LogAsync(LogLevel.Warning, $"Skipping {packageString}, to push symbols packages enable the symbols server on this feed.");

                    return;
                }
            }

            await log.LogAsync(LogLevel.Minimal, $"Pushing {packageString}");

            await log.LogAsync(LogLevel.Information, $"Checking if package exists.");

            var exists = false;

            if (package.IsSymbolsPackage)
            {
                exists = await packageIndex.SymbolsExists(package.Identity);
            }
            else
            {
                exists = await packageIndex.Exists(package.Identity);
            }

            if (exists)
            {
                if (skipExisting)
                {
                    await log.LogAsync(LogLevel.Minimal, $"Package already exists, skipping {packageString}");

                    return;
                }
                else if (force)
                {
                    await log.LogAsync(LogLevel.Information, $"Package already exists, removing {packageString}");

                    // Avoid removing both the symbols and non-symbols packages, this should only
                    // remove the package we are going to replace.
                    if (package.IsSymbolsPackage)
                    {
                        await SleetUtility.RemoveSymbolsPackage(context, package.Identity);
                    }
                    else
                    {
                        await SleetUtility.RemoveNonSymbolsPackage(context, package.Identity);
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Package already exists: {packageString}.");
                }
            }

            await log.LogAsync(LogLevel.Information, $"Adding {packageString}");

            using (package)
            {
                package.Zip     = new ZipArchive(File.OpenRead(package.PackagePath), ZipArchiveMode.Read, leaveOpen: false);
                package.Package = new PackageArchiveReader(package.Zip);

                await SleetUtility.AddPackage(context, package);
            }
        }