/// <summary> /// Index a package from its metadata /// </summary> /// <param name="nuspecReader">The nuspec reader instance to be used to read metadata</param> /// <returns>The <see cref="RegistrationResult"/> instance which correspond to the package</returns> public override async Task <RegistrationResult> IndexPackage(INuspecCoreReader nuspecReader) { if (nuspecReader == null) { throw new ArgumentNullException(nameof(nuspecReader)); } var indexingStopwatch = Stopwatch.StartNew(); string version = nuspecReader.GetVersion().ToNormalizedString(); string packageRegistrationBaseUrl = $"{registrationServiceUrl + nuspecReader.GetId()}/index.json"; logger.LogDebug(LoggingEvents.PackageIndexIndexingPackage, "Indexing package {packageId}, version {version}", nuspecReader.GetId(), version); RegistrationResult registrationIndex = this.packages.FirstOrDefault(p => p.Id == packageRegistrationBaseUrl); registrationIndex = await this.IndexPackageCore(nuspecReader, registrationIndex).ConfigureAwait(false); await this.SavePackageIndex().ConfigureAwait(false); indexingStopwatch.Stop(); logger.LogInformation(LoggingEvents.PackageIndexPackageIndexed, "Package {packageId}, version {version} indexed in {elapsed}", nuspecReader.GetId(), version, indexingStopwatch.Elapsed); var metricEvent = new EventTelemetry("PackageIndexed"); metricEvent.Metrics.Add("packageIndexed", indexingStopwatch.ElapsedMilliseconds); telemetryClient.TrackEvent(metricEvent); return(registrationIndex); }
/// <summary> /// Installs the (unzipped) nuget package into <see cref="PackagesPath"/> directory. /// </summary> /// <param name="nugetContentPath">Content of the unzipped nuget.</param> /// <param name="nuspecreader">Outputs nuspec file.</param> /// <returns>Whether the installation was successful.</returns> public Scheme.InstalledPackage InstallPackage(string nugetContentPath, out INuspecCoreReader nuspecreader) { var nuspecs = Directory.GetFiles(nugetContentPath, "*.nuspec"); if (nuspecs.Length == 1) { var nuspec = new NuspecCoreReader(XDocument.Load(nuspecs[0])); // TODO: restore dependencies // copy lib to packages var dllsource = Path.Combine(nugetContentPath, "lib/netstandard2.0"); var dlltarget = Path.GetFullPath(Path.Combine(PackagesPath, nuspec.GetId(), nuspec.GetVersion().ToNormalizedString())); Directory.CreateDirectory(dlltarget); foreach (var fpath in Directory.GetFiles(dllsource)) { var filetarget = Path.Combine(dlltarget, Path.GetFileName(fpath)); if (!File.Exists(filetarget)) { File.Move(fpath, filetarget); } } // TODO: try to delete old versions of the package // var package = new Scheme.InstalledPackage { pluginId = nuspec.GetId(), version = nuspec.GetVersion().ToNormalizedString(), active = false }; // add packageId to packages.json UpdatePackagesJson(json => { json.Add(package); }); // nuspecreader = nuspec; return(package); } else { nuspecreader = null; return(null); } }
public override Task <RegistrationResult> IndexPackage(INuspecCoreReader nuspecReader) { throw new NotImplementedException(); }
/// <summary> /// Index a package from its metadata /// </summary> /// <param name="nuspecReader">The nuspec reader instance to be used to read metadata</param> /// <returns>The <see cref="RegistrationResult"/> instance which correspond to the package</returns> public abstract Task <RegistrationResult> IndexPackage(INuspecCoreReader nuspecReader);
/// <summary> /// Handle the logic to index a package from the <paramref name="nuspecReader"/> and store information in the <paramref name="registrationIndex"/> if provided or create a new entry /// </summary> /// <param name="nuspecReader">The nuspecReader to be used to read package information</param> /// <param name="registrationIndex">The <see cref="RegistrationResult"/> instance which should be used to create new version if package id already exists</param> /// <returns>The <paramref name="registrationIndex"/> instance of provided or a new one</returns> protected virtual async Task <RegistrationResult> IndexPackageCore(INuspecCoreReader nuspecReader, RegistrationResult registrationIndex) { bool isNewPackageId = false; var metadata = nuspecReader.GetMetadata(); string version = nuspecReader.GetVersion().ToNormalizedString(); string packageRegistrationBaseUrl = $"{registrationServiceUrl + nuspecReader.GetId()}/index.json"; if (registrationIndex == null) { registrationIndex = new RegistrationResult() { Id = packageRegistrationBaseUrl }; isNewPackageId = true; } RegistrationPage registrationPage = registrationIndex.Items.FirstOrDefault(); if (registrationPage == null) { registrationPage = new RegistrationPage(); registrationIndex.Items.Add(registrationPage); } bool versionExists = this.IsVersionAlreadyExisting(nuspecReader.GetId(), version); if (versionExists) { throw new PackageVersionAlreadyExistsException($"The version {version} already exists for package {nuspecReader.GetId()}"); } var packageSummary = new NuGetPackageSummary() { PackageMetadataUrl = registrationServiceUrl + nuspecReader.GetId() + "/index.json", Id = nuspecReader.GetId(), Version = nuspecReader.GetVersion().ToFullString() }; await this.AddNewVersion(packageSummary.Id, new NuGetPackageVersion() { PackageMetadataUrl = registrationServiceUrl + nuspecReader.GetId() + "/" + version, Version = version, Downloads = 0 }); foreach (var m in metadata) { if ("title".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase)) { packageSummary.Title = m.Value; } else if ("description".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase)) { packageSummary.Description = m.Value; } else if ("authors".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase) && !string.IsNullOrEmpty(m.Value)) { packageSummary.Authors = m.Value.Split(","); } else if ("owners".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase) && !string.IsNullOrEmpty(m.Value)) { packageSummary.Owners = m.Value.Split(","); } else if ("requireLicenseAcceptance".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase)) { packageSummary.RequireLicenseAcceptance = bool.Parse(m.Value); } else if ("licenseUrl".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase)) { packageSummary.LicenseUrl = m.Value; } else if ("projectUrl".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase)) { packageSummary.ProjectUrl = m.Value; } else if ("iconUrl".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase)) { packageSummary.IconUrl = m.Value; } else if ("copyright".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase)) { packageSummary.Copyright = m.Value; } else if ("tags".Equals(m.Key, StringComparison.InvariantCultureIgnoreCase)) { packageSummary.Tags = m.Value; } } RegistrationLeaf registrationLeaf = new RegistrationLeaf(); registrationLeaf.CatalogEntry = packageSummary; registrationLeaf.PackageContent = $"{this.packageContentServiceUrl}{nuspecReader.GetId()}/{version}/{nuspecReader.GetId()}.{version}.nupkg"; var versions = await this.GetAllVersions(packageSummary.Id).ConfigureAwait(false); string lowerVersion = versions.First(); string upperVersion = versions.Last(); registrationPage.Id = $"{registrationServiceUrl + nuspecReader.GetId()}/index.json/#page/{lowerVersion}/{upperVersion}"; registrationPage.Items.Add(registrationLeaf); registrationPage.Lower = lowerVersion; registrationPage.Upper = upperVersion; if (isNewPackageId) { await this.AddRegistrationResult(registrationIndex).ConfigureAwait(false); } return(registrationIndex); }