public static List<Package> GetPackages(string sqlConnectionString, List<int> packageKeys, TextWriter log = null, bool verbose = false) { log = log ?? DefaultTraceWriter; EntitiesContext context = new EntitiesContext(sqlConnectionString, readOnly: true); IEntityRepository<Package> packageRepository = new EntityRepository<Package>(context); IQueryable<Package> set = packageRepository.GetAll(); set = set.Where(p => packageKeys.Contains(p.Key)); set = set.OrderBy(p => p.Key); set = set .Include(p => p.PackageRegistration) .Include(p => p.PackageRegistration.Owners) .Include(p => p.SupportedFrameworks) .Include(p => p.Dependencies); return ExecuteQuery(set, log, verbose); }
private Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, IPackage nugetPackage) { var package = packageRegistration.Packages .Where(pv => pv.Version == nugetPackage.Version.ToString()) .SingleOrDefault(); if (package != null) { switch (package.Status) { case PackageStatusType.Rejected : throw new EntityException( string.Format( "This package has been {0} and can no longer be submitted.", package.Status.GetDescriptionOrValue().ToLower())); case PackageStatusType.Submitted : //continue on break; default : throw new EntityException( "A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version); } } var now = DateTime.UtcNow; var packageFileStream = nugetPackage.GetStream(); //if new package versus updating an existing package. if (package == null) package = new Package(); package.Version = nugetPackage.Version.ToString(); package.Description = nugetPackage.Description; package.ReleaseNotes = nugetPackage.ReleaseNotes; package.RequiresLicenseAcceptance = nugetPackage.RequireLicenseAcceptance; package.HashAlgorithm = Constants.Sha512HashAlgorithmId; package.Hash = cryptoSvc.GenerateHash(packageFileStream.ReadAllBytes()); package.PackageFileSize = packageFileStream.Length; package.Created = now; package.Language = nugetPackage.Language; package.LastUpdated = now; package.Published = now; package.Copyright = nugetPackage.Copyright; package.IsPrerelease = !nugetPackage.IsReleaseVersion(); package.Listed = false; package.Status = PackageStatusType.Submitted; package.SubmittedStatus = PackageSubmittedStatusType.Ready; package.ApprovedDate = null; if (package.ReviewedDate.HasValue) package.SubmittedStatus = PackageSubmittedStatusType.Updated; //we don't moderate prereleases if (package.IsPrerelease) { package.Listed = true; package.Status = PackageStatusType.Exempted; } if (packageRegistration.IsTrusted) { package.Listed = true; package.Status = PackageStatusType.Approved; package.ReviewedDate = now; package.ApprovedDate = now; } package.IconUrl = nugetPackage.IconUrl == null ? string.Empty : nugetPackage.IconUrl.ToString(); package.LicenseUrl = nugetPackage.LicenseUrl == null ? string.Empty : nugetPackage.LicenseUrl.ToString(); package.ProjectUrl = nugetPackage.ProjectUrl == null ? string.Empty : nugetPackage.ProjectUrl.ToString(); package.ProjectSourceUrl = nugetPackage.ProjectSourceUrl == null ? string.Empty : nugetPackage.ProjectSourceUrl.ToString(); package.PackageSourceUrl = nugetPackage.PackageSourceUrl == null ? string.Empty : nugetPackage.PackageSourceUrl.ToString(); package.DocsUrl = nugetPackage.DocsUrl == null ? string.Empty : nugetPackage.DocsUrl.ToString(); package.MailingListUrl = nugetPackage.MailingListUrl == null ? string.Empty : nugetPackage.MailingListUrl.ToString(); package.BugTrackerUrl = nugetPackage.BugTrackerUrl == null ? string.Empty : nugetPackage.BugTrackerUrl.ToString(); package.Summary = nugetPackage.Summary ?? string.Empty; package.Tags = nugetPackage.Tags ?? string.Empty; package.Title = nugetPackage.Title ?? string.Empty; foreach (var item in package.Authors.OrEmptyListIfNull().ToList()) { packageAuthorRepo.DeleteOnCommit(item); } packageAuthorRepo.CommitChanges(); foreach (var author in nugetPackage.Authors) { package.Authors.Add( new PackageAuthor { Name = author }); } foreach (var item in package.SupportedFrameworks.OrEmptyListIfNull().ToList()) { packageFrameworksRepo.DeleteOnCommit(item); } packageFrameworksRepo.CommitChanges(); var supportedFrameworks = GetSupportedFrameworks(nugetPackage).Select(fn => fn.ToShortNameOrNull()).ToArray(); if (!supportedFrameworks.AnySafe(sf => sf == null)) { foreach (var supportedFramework in supportedFrameworks) { package.SupportedFrameworks.Add( new PackageFramework { TargetFramework = supportedFramework }); } } foreach (var item in package.Dependencies.OrEmptyListIfNull().ToList()) { packageDependenciesRepo.DeleteOnCommit(item); } packageDependenciesRepo.CommitChanges(); foreach (var dependencySet in nugetPackage.DependencySets) { if (dependencySet.Dependencies.Count == 0) { package.Dependencies.Add( new PackageDependency { Id = null, VersionSpec = null, TargetFramework = dependencySet.TargetFramework.ToShortNameOrNull() }); } else { foreach (var dependency in dependencySet.Dependencies.Select( d => new { d.Id, d.VersionSpec, dependencySet.TargetFramework })) { package.Dependencies.Add( new PackageDependency { Id = dependency.Id, VersionSpec = dependency.VersionSpec == null ? null : dependency.VersionSpec.ToString(), TargetFramework = dependency.TargetFramework.ToShortNameOrNull() }); } } } foreach (var item in package.Files.OrEmptyListIfNull().ToList()) { packageFilesRepo.DeleteOnCommit(item); } packageFilesRepo.CommitChanges(); foreach (var packageFile in nugetPackage.GetFiles().OrEmptyListIfNull()) { var filePath = packageFile.Path; var fileContent = " "; IList<string> extensions = new List<string>(); var approvedExtensions = Configuration.ReadAppSettings("PackageFileTextExtensions"); if (!string.IsNullOrWhiteSpace(approvedExtensions)) { foreach (var extension in approvedExtensions.Split(',', ';')) { extensions.Add("." + extension); } } try { var extension = Path.GetExtension(filePath); if (extension != null) { if (extensions.Contains(extension)) fileContent = packageFile.GetStream().ReadToEnd(); else if (extension.Equals(".exe", StringComparison.InvariantCultureIgnoreCase)) { var bytes = packageFile.GetStream().ReadAllBytes(); var md5Hash = BitConverter.ToString(Convert.FromBase64String(cryptoSvc.GenerateHash(bytes, "MD5"))) .Replace("-", string.Empty); var sha1Hash = BitConverter.ToString(Convert.FromBase64String(cryptoSvc.GenerateHash(bytes, "SHA1"))) .Replace("-", string.Empty); fileContent = string.Format("md5: {0} | sha1: {1}", md5Hash, sha1Hash); } } } catch (Exception ex) { // Log but swallow the exception ErrorSignal.FromCurrentContext().Raise(ex); } package.Files.Add( new PackageFile { FilePath = filePath, FileContent = fileContent, }); } package.FlattenedAuthors = package.Authors.Flatten(); package.FlattenedDependencies = package.Dependencies.Flatten(); return package; }
Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, IPackage nugetPackage) { var package = packageRegistration.Packages .Where(pv => pv.Version == nugetPackage.Version.ToString()) .SingleOrDefault(); if (package != null) throw new EntityException("A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version); var now = DateTime.UtcNow; var packageFileStream = nugetPackage.GetStream(); package = new Package { Version = nugetPackage.Version.ToString(), Description = nugetPackage.Description, ReleaseNotes = nugetPackage.ReleaseNotes, RequiresLicenseAcceptance = nugetPackage.RequireLicenseAcceptance, HashAlgorithm = Constants.Sha512HashAlgorithmId, Hash = cryptoSvc.GenerateHash(packageFileStream.ReadAllBytes()), PackageFileSize = packageFileStream.Length, Created = now, Language = nugetPackage.Language, LastUpdated = now, Published = now, Copyright = nugetPackage.Copyright, IsPrerelease = !nugetPackage.IsReleaseVersion(), Listed = true, }; if (nugetPackage.IconUrl != null) package.IconUrl = nugetPackage.IconUrl.ToString(); if (nugetPackage.LicenseUrl != null) package.LicenseUrl = nugetPackage.LicenseUrl.ToString(); if (nugetPackage.ProjectUrl != null) package.ProjectUrl = nugetPackage.ProjectUrl.ToString(); if (nugetPackage.Summary != null) package.Summary = nugetPackage.Summary; if (nugetPackage.Tags != null) package.Tags = nugetPackage.Tags; if (nugetPackage.Title != null) package.Title = nugetPackage.Title; foreach (var author in nugetPackage.Authors) package.Authors.Add(new PackageAuthor { Name = author }); var supportedFrameworks = GetSupportedFrameworks(nugetPackage).Select(fn => fn.ToShortNameOrNull()).ToArray(); if (!supportedFrameworks.AnySafe(sf => sf == null)) foreach (var supportedFramework in supportedFrameworks) package.SupportedFrameworks.Add(new PackageFramework { TargetFramework = supportedFramework }); foreach (var dependencySet in nugetPackage.DependencySets) { if (dependencySet.Dependencies.Count == 0) package.Dependencies.Add(new PackageDependency { Id = null, VersionSpec = null, TargetFramework = dependencySet.TargetFramework.ToShortNameOrNull() }); else foreach (var dependency in dependencySet.Dependencies.Select(d => new { d.Id, d.VersionSpec, dependencySet.TargetFramework })) package.Dependencies.Add(new PackageDependency { Id = dependency.Id, VersionSpec = dependency.VersionSpec == null ? null : dependency.VersionSpec.ToString(), TargetFramework = dependency.TargetFramework.ToShortNameOrNull() }); } foreach (var packageFile in nugetPackage.GetFiles().OrEmptyListIfNull()) { var filePath = packageFile.Path; var fileContent = " "; IList<string> extensions = new List<string>(); var approvedExtensions = Configuration.ReadAppSettings("PackageFileTextExtensions"); if (!string.IsNullOrWhiteSpace(approvedExtensions)) { foreach (var extension in approvedExtensions.Split(',', ';')) { extensions.Add("." + extension); } } try { if (extensions.Contains(Path.GetExtension(filePath))) { fileContent = packageFile.GetStream().ReadToEnd(); } } catch (Exception ex) { // Log but swallow the exception Elmah.ErrorSignal.FromCurrentContext().Raise(ex); } package.Files.Add(new PackageFile { FilePath = filePath, FileContent = fileContent, }); } package.FlattenedAuthors = package.Authors.Flatten(); package.FlattenedDependencies = package.Dependencies.Flatten(); return package; }