Beispiel #1
0
        private static async Task RemovePackages(bool force, ILogger log, SleetContext context, PackageSets existingPackageSets, HashSet <PackageIdentity> packages)
        {
            var toRemove        = new HashSet <PackageIdentity>();
            var toRemoveSymbols = new HashSet <PackageIdentity>();

            foreach (var package in packages)
            {
                var exists        = existingPackageSets.Packages.Exists(package);
                var symbolsExists = existingPackageSets.Symbols.Exists(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()}");
                    }
                }

                if (exists)
                {
                    toRemove.Add(package);
                }

                if (symbolsExists)
                {
                    toRemoveSymbols.Add(package);
                }

                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);
            }

            // Update feed
            await log.LogAsync(LogLevel.Information, "Removing packages from feed locally");

            // Add/Remove packages
            var changeContext = SleetOperations.CreateDelete(existingPackageSets, toRemove, toRemoveSymbols);
            await SleetUtility.ApplyPackageChangesAsync(context, changeContext);
        }
Beispiel #2
0
        public static async Task <int> RunCore(LocalSettings settings, ISleetFileSystem source, List <string> inputs, bool force, ILogger log)
        {
            var exitCode = 0;

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

            // Get packages
            var packages = GetPackageInputs(inputs, now, log);

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

                // Get sleet.settings.json
                var sourceSettings = new SourceSettings();

                // 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);

                foreach (var package in packages)
                {
                    if (await packageIndex.Exists(package.Identity))
                    {
                        if (force)
                        {
                            log.LogInformation($"Package already exists, removing {package.ToString()}");
                            await SleetUtility.RemovePackage(context, package.Identity);
                        }
                        else
                        {
                            throw new InvalidOperationException($"Package already exists: '{package.Identity}'.");
                        }
                    }

                    log.LogInformation($"Adding {package.Identity.ToString()}");
                    await SleetUtility.AddPackage(context, package);
                }

                // Save all
                await source.Commit(log, token);
            }

            return(exitCode);
        }
Beispiel #3
0
        public Task AddPackagesAsync(IEnumerable <PackageInput> packageInputs)
        {
            var byId  = SleetUtility.GetPackageSetsById(packageInputs, e => e.Identity.Id);
            var tasks = new List <Func <Task> >();

            // Create page details pages and index pages in parallel.
            tasks.AddRange(byId.Select(e => new Func <Task>(() => CreatePackageIndexAsync(e.Key, e.Value))));
            tasks.AddRange(packageInputs.Select(e => new Func <Task>(() => CreatePackagePageAsync(e))));

            return(TaskUtils.RunAsync(tasks));
        }
Beispiel #4
0
        public Task RemovePackagesAsync(IEnumerable <PackageIdentity> packagesToDelete)
        {
            var byId  = SleetUtility.GetPackageSetsById(packagesToDelete, e => e.Id);
            var tasks = new List <Func <Task> >();

            foreach (var pair in byId)
            {
                var packageId = pair.Key;
                var versions  = new HashSet <NuGetVersion>(pair.Value.Select(e => e.Version));
                tasks.Add(new Func <Task>(() => RemovePackagesFromIndexAsync(packageId, versions)));
            }

            return(TaskUtils.RunAsync(tasks));
        }
Beispiel #5
0
        /// <summary>
        /// Remove packages from feed without committing
        /// </summary>
        private static async Task RemovePackages(SleetContext context, PackageSets existingPackageSets, HashSet <PackageIdentity> packagesToRemove, bool dryRun, ILogger log)
        {
            var toRemove        = new HashSet <PackageIdentity>();
            var toRemoveSymbols = new HashSet <PackageIdentity>();

            foreach (var package in packagesToRemove)
            {
                var exists        = existingPackageSets.Packages.Exists(package);
                var symbolsExists = existingPackageSets.Symbols.Exists(package);

                if (exists)
                {
                    toRemove.Add(package);
                }

                if (symbolsExists)
                {
                    toRemoveSymbols.Add(package);
                }

                if (exists || symbolsExists)
                {
                    await log.LogAsync(LogLevel.Information, $"Pruning {package.ToString()}");
                }
            }

            if (toRemove.Count < 1 && toRemoveSymbols.Count < 1)
            {
                await log.LogAsync(LogLevel.Information, $"No packages need pruning.");
            }
            else if (!dryRun)
            {
                // Add/Remove packages
                var changeContext = SleetOperations.CreateDelete(existingPackageSets, toRemove, toRemoveSymbols);
                await SleetUtility.ApplyPackageChangesAsync(context, changeContext);
            }
        }
Beispiel #6
0
        private static async Task PushPackages(List<PackageInput> packageInputs, SleetContext context, PackageIndex packageIndex, bool force, bool skipExisting, ILogger log)
        {
            var toAdd = new List<PackageInput>();
            var toRemove = new List<PackageInput>();
            var existingPackageSets = await packageIndex.GetPackageSetsAsync();

            foreach (var package in packageInputs)
            {
                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.");

                        // Skip this package
                        continue;
                    }
                }

                var exists = false;

                if (package.IsSymbolsPackage)
                {
                    exists = existingPackageSets.Symbols.Exists(package.Identity);
                }
                else
                {
                    exists = existingPackageSets.Packages.Exists(package.Identity);
                }

                if (exists)
                {
                    if (skipExisting)
                    {
                        await log.LogAsync(LogLevel.Minimal, $"Skip exisiting package: {packageString}");
                        continue;
                    }
                    else if (force)
                    {
                        toRemove.Add(package);
                        await log.LogAsync(LogLevel.Information, $"Replace existing package: {packageString}");
                    }
                    else
                    {
                        throw new InvalidOperationException($"Package already exists: {packageString}.");
                    }
                }
                else
                {
                    await log.LogAsync(LogLevel.Minimal, $"Add new package: {packageString}");
                }

                // Add to list of packages to push
                toAdd.Add(package);
            }

            await log.LogAsync(LogLevel.Minimal, $"Processing feed changes");

            // Add/Remove packages
            var changeContext = SleetOperations.Create(existingPackageSets, toAdd, toRemove);
            await SleetUtility.ApplyPackageChangesAsync(context, changeContext);
        }
Beispiel #7
0
        public static async Task <int> RunCore(LocalSettings settings, ISleetFileSystem source, string packageId, string version, string reason, bool force, ILogger log)
        {
            var exitCode = 0;

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

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

                // Get sleet.settings.json
                var sourceSettings = new SourceSettings();

                // 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.GetPackagesById(packageId));
                }

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

                foreach (var package in packages)
                {
                    if (!await packageIndex.Exists(package))
                    {
                        log.LogInformation($"{package.ToString()} does not exist.");

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

                    log.LogInformation($"Removing {package.ToString()}");
                    await SleetUtility.RemovePackage(context, package);
                }

                // Save all
                await source.Commit(log, token);
            }

            return(exitCode);
        }
Beispiel #8
0
        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);
        }
Beispiel #9
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);
            }
        }
Beispiel #10
0
        public static async Task <bool> PushPackages(LocalSettings settings, ISleetFileSystem source, List <string> inputs, bool force, bool skipExisting, ILogger log, CancellationToken token)
        {
            var exitCode = true;
            var now      = DateTimeOffset.UtcNow;
            var packages = new List <PackageInput>();

            try
            {
                // Get packages
                packages.AddRange(GetPackageInputs(inputs, now, log));

                // Get sleet.settings.json
                await log.LogAsync(LogLevel.Minimal, "Reading feed");

                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
                };

                // Fetch feed
                await SleetUtility.FetchFeed(context);

                await log.LogAsync(LogLevel.Information, "Reading existing package index");

                var packageIndex = new PackageIndex(context);

                foreach (var package in packages)
                {
                    await PushPackage(package, context, packageIndex, force, skipExisting, log);
                }

                // Save all
                await log.LogAsync(LogLevel.Minimal, $"Committing changes to {source.BaseURI.AbsoluteUri}");

                await source.Commit(log, token);
            }
            finally
            {
                // Close all zip readers
                foreach (var package in packages)
                {
                    package.Dispose();
                }
            }

            if (exitCode)
            {
                await log.LogAsync(LogLevel.Minimal, "Successfully pushed packages.");
            }
            else
            {
                await log.LogAsync(LogLevel.Error, "Failed to push packages.");
            }

            return(exitCode);
        }