コード例 #1
0
        /// <summary>Processes a package that was added to a repository.</summary>
        /// <param name="package">The package that was added to the repository.</param>
        /// <returns>CommandResult.</returns>
        protected virtual CommandResult ProcessPackageAdded(Package package)
        {
            try
            {
                var packageEntity = GetorAddPackageEntity(package);
                // ReSharper disable once UnusedVariable
                var packageVersionEntity = GetorAddPackageVersionEntity(package, packageEntity);
                // ReSharper disable once UnusedVariable
                var packageEnvironmentEntity = GetOrAddPackageEnvironmentEntity(Action.SourceRepository, packageEntity, packageVersionEntity);

                dbContext.SaveChanges();
                return(new CommandResult(this, true, $"{package.Id} was cataloged successfully"));
            }
            catch (DbEntityValidationException ex)
            {
                Logger.LogError($"Entity Validation Error Cataloging package {package.Id}. Error: {ex.Message}. Reverting changes.");
                dbContext.RevertChanges(ex);
                return(new CommandResult(this, false, $"Entity Validation Error Cataloging package {package.Id}. Error: {ex.Message}."));
            }
            catch (Exception ex)
            {
                Logger.LogError($"Error Cataloging package {package.Id}. Error: {ex.Message}.");
                return(new CommandResult(this, false, $"Error Cataloging package {package.Id}. Error: {ex.Message}."));
            }
        }
コード例 #2
0
        private PackageVersion GetorAddPackageVersionEntity(Package package, Entity.Package packageEntity)
        {
            var packageVersionEntity = GetPackageVersionEntity(package, packageEntity);

            // This version of the package was already processed, don't process again
            if (packageVersionEntity != null)
            {
                Logger.LogDebug($"{package.Id}.{package.Version} package version already exists. Ignoring.");
                return(packageVersionEntity);
            }

            DbContextTransaction transaction = null;

            try
            {
                transaction = dbContext.Database.BeginTransaction();

                // Get package contents from repository
                var tempVersion = new Version(package.Version);
                packageVersionEntity = new PackageVersion
                {
                    Version         = package.Version,
                    MajorVersion    = Math.Max(0, tempVersion.Major),
                    MinorVersion    = Math.Max(0, tempVersion.Minor),
                    BuildVersion    = Math.Max(0, tempVersion.Build),
                    RevisionVersion = Math.Max(0, tempVersion.Revision)
                };
                packageEntity.PackageVersions.Add(packageVersionEntity);
                dbContext.SaveChanges();

                Logger.LogInformation($"{package.Id}.{package.Version} package version added to database.");

                PopulatePackageAssembliesFromPackageContent(package, packageEntity, packageVersionEntity);

                transaction.Commit();

                return(packageVersionEntity);
            }
            catch
            {
                Logger.LogError("Exception thrown.  Rolling back changes.");

                transaction?.Rollback();
                throw;
            }
        }
コード例 #3
0
        /// <summary>The method that executes the appropriate command to process the package.</summary>
        /// <param name="package">The package for the command to handle.</param>
        /// <param name="packageEvent">The event associated with the package.</param>
        /// <returns>Returns the CommandResult for the package.</returns>
        public override CommandResult Execute(Package package, PackageEvent packageEvent)
        {
            Debug.Assert(package != null);

            if (new NuGetVersion(package.Version).IsPrerelease)
            {
                Logger.LogWarning($"The {package} skipped because it is a pre-release.");
            }
            else if (packageEvent == PackageEvent.Deleted)
            {
                return(ProcessPackageDeleted(package));
            }
            else if (packageEvent == PackageEvent.Added || packageEvent == PackageEvent.Promoted || packageEvent == PackageEvent.Processed)
            {
                return(ProcessPackageAdded(package));
            }

            return(new CommandResult(this));
        }
コード例 #4
0
        private Entity.Package GetorAddPackageEntity(Package package)
        {
            // Get package from database or add it if not found
            var packageEntity = GetPackageEntity(package);

            if (packageEntity == null)
            {
                // Package not found in database. Add it.
                packageEntity = new Entity.Package
                {
                    Name  = package.Id,
                    Title = string.IsNullOrEmpty(package.Title) ? package.Id : package.Title
                };
                dbContext.Packages.Add(packageEntity);
                dbContext.SaveChanges();

                Logger.LogInformation($"Package {packageEntity.Name} added to database");
            }

            return(packageEntity);
        }
コード例 #5
0
        private CommandResult ProcessPackageDeleted(Package package)
        {
            try
            {
                var packageEntity = GetPackageEntity(package);
                if (packageEntity == null)
                {
                    return(new CommandResult(this));
                }

                // ReSharper disable once UnusedVariable
                var packageVersionEntity = GetPackageVersionEntity(package, packageEntity);
                if (packageVersionEntity == null)
                {
                    return(new CommandResult(this));
                }

                // ReSharper disable once UnusedVariable
                var packageEnvironmentEntity = GetPackageEnvironmentEntity(Action.SourceRepository, packageVersionEntity);
                if (packageEnvironmentEntity == null)
                {
                    return(new CommandResult(this));
                }

                packageVersionEntity.PackageEnvironments.Remove(packageEnvironmentEntity);
                dbContext.SaveChanges();
                return(new CommandResult(this, true, $"{package.Id} in {Action.SourceRepository} was removed from catalog successfully"));
            }
            catch (DbEntityValidationException ex)
            {
                Logger.LogError($"Entity Validation Error Cataloging package {package.Id}. Error: {ex.Message}. Reverting changes.");
                dbContext.RevertChanges(ex);
                return(new CommandResult(this, false, $"Entity Validation Error Cataloging package {package.Id}. Error: {ex.Message}."));
            }
            catch (Exception ex)
            {
                Logger.LogError($"Error Cataloging package {package.Id}. Error: {ex.Message}.");
                return(new CommandResult(this, false, $"Error Cataloging package {package.Id}. Error: {ex.Message}."));
            }
        }
コード例 #6
0
        private void PopulatePackageAssembliesFromPackageContent(Package package, Entity.Package packageEntity, PackageVersion packageVersionEntity)
        {
            var uniqueAssemblies = new Dictionary <string, AssemblyName>();
            var coreAssembly     = typeof(object).Assembly;

            using var byteStream = new MemoryStream(package.Content);
            using var archive    = ArchiveFactory.Open(byteStream);
            using var lc         = new MetadataLoadContext(new ZipAssemblyResolver(archive, coreAssembly), coreAssembly.FullName);
            foreach (var archiveEntry in archive.Entries)
            {
                if (archiveEntry.IsDirectory ||
                    (!archiveEntry.Key.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) && !archiveEntry.Key.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase)))
                {
                    continue;
                }

                System.Reflection.Assembly assembly;

                using var entryStream = archiveEntry.ExtractToStream();
                try
                {
                    assembly = lc.LoadFromStream(entryStream);
                }
                catch (FileLoadException)
                {
                    Logger.LogError($"{packageEntity.Name} (v{packageVersionEntity.Version}) - {archiveEntry.Key} - could not be loaded.");
                    continue;
                }
                catch (Exception e)
                {
                    Logger.LogError(e, $"{packageEntity.Name} (v{packageVersionEntity.Version}) - {archiveEntry.Key} - threw an exception.");
                    continue;
                }

                foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
                {
                    if (!uniqueAssemblies.ContainsKey(referencedAssembly.FullName))
                    {
                        uniqueAssemblies.Add(referencedAssembly.FullName, referencedAssembly);
                    }
                }

                var assemblyName = assembly.GetName();
                Logger.LogDebug($"Processing assembly {assemblyName.Name}, version={assemblyName.Version}");

                var assemblyEntity               = GetOrAddAssemblyEntity(assemblyName);
                var assemblyVersionEntity        = GetOrAddAssemblyVersionEntity(assemblyEntity, assemblyName);
                var packageAssemblyVersionEntity = GetOrAddPackageAssemblyVersionEntity(packageEntity, packageVersionEntity, assemblyEntity, assemblyVersionEntity);
                packageAssemblyVersionEntity.ReferenceIncluded = true;
            }

            foreach (var uniqueAssembly in uniqueAssemblies.Values)
            {
                Logger.LogDebug($"Processing referenced assembly {uniqueAssembly.Name}, version={uniqueAssembly.Version}");

                var assemblyEntity        = GetOrAddAssemblyEntity(uniqueAssembly);
                var assemblyVersionEntity = GetOrAddAssemblyVersionEntity(assemblyEntity, uniqueAssembly);

                GetOrAddPackageAssemblyVersionEntity(packageEntity, packageVersionEntity, assemblyEntity, assemblyVersionEntity);
            }
        }
コード例 #7
0
 private static PackageVersion GetPackageVersionEntity(Package package, Entity.Package packageEntity)
 {
     return(packageEntity.PackageVersions.FirstOrDefault(s => s.PackageId == packageEntity.PackageId && s.Version == package.Version));
 }
コード例 #8
0
 private Entity.Package GetPackageEntity(Package package)
 {
     return(dbContext.Packages.FirstOrDefault(s => s.Name == package.Id));
 }