public async Task <(IList <RootReadMeLicenseContext> Licenses, string MarkdownExpression)> GetLicensesAsync(string licenseExpression, CancellationToken token) { if (licenseExpression.IsNullOrEmpty()) { return(Array.Empty <RootReadMeLicenseContext>(), null); } var codes = LicenseExpression.GetCodes(licenseExpression); var licenses = new RootReadMeLicenseContext[codes.Count]; for (var i = 0; i < licenses.Length; i++) { var code = codes[i]; if (!_licenseByCode.TryGetValue(code, out var license)) { var repositoryLicense = await Repository.LoadOrCreateLicenseAsync(code, token); license = new RootReadMeLicenseContext { Code = repositoryLicense.Code, RequiresApproval = repositoryLicense.RequiresApproval, RequiresThirdPartyNotices = repositoryLicense.RequiresThirdPartyNotices, LocalHRef = Repository.Storage.GetLicenseLocalHRef(repositoryLicense.Code) }; _licenseByCode.Add(code, license); } licenses[i] = license; } var markdownExpression = LicenseExpression.ReplaceCodes(licenseExpression, code => "[{0}]({1})".FormatWith(code, _licenseByCode[code].LocalHRef)); return(licenses, markdownExpression); }
public async Task UpdatePackageReadMeAsync(Package package, CancellationToken token) { package.AssertNotNull(nameof(package)); var id = new LibraryId(package.SourceCode, package.Name, package.Version); await Storage.CreateDefaultRemarksFile(id, token); await Storage.CreateDefaultThirdPartyNoticesFile(id, token); var index = await Storage.ReadLibraryIndexJsonAsync <LibraryIndexJson>(id, token); var context = new LibraryReadMeContext { Name = package.Name, Version = package.Version, HRef = package.HRef, Description = package.Description, LicenseCode = package.LicenseCode, UsedBy = PackageRepositoryTools.BuildUsedBy(package.UsedBy), TargetFrameworks = string.Join(", ", index.UsedBy.SelectMany(i => i.TargetFrameworks).Distinct(StringComparer.OrdinalIgnoreCase).OrderBy(i => i)), Remarks = package.Remarks, ThirdPartyNotices = package.ThirdPartyNotices }; if (context.LicenseCode.IsNullOrEmpty()) { context.LicenseCode = "Unknown"; } else { var codes = LicenseExpression.GetCodes(context.LicenseCode); context.LicenseLocalHRef = Storage.GetLicenseLocalHRef(codes.First(), id); context.LicenseMarkdownExpression = LicenseExpression.ReplaceCodes( context.LicenseCode, i => "[{0}]({1})".FormatWith(i, Storage.GetLicenseLocalHRef(i, id))); if (package.ApprovalStatus == PackageApprovalStatus.HasToBeApproved) { context.LicenseDescription = "has to be approved"; } } foreach (var license in index.Licenses) { if (license.Code.IsNullOrEmpty()) { license.Code = "Unknown"; } context.Licenses.Add(license); } var dependencies = index .UsedBy .SelectMany(i => i.Dependencies) .Select(i => new LibraryId(id.SourceCode, i.Name, i.Version)) .Distinct(); foreach (var dependency in dependencies) { context.Dependencies.Add(new LibraryReadMeDependencyContext { Name = dependency.Name, Version = dependency.Version, LocalHRef = Storage.GetPackageLocalHRef(dependency, id) }); } await Storage.WriteLibraryReadMeAsync(id, context, token); }