Exemple #1
0
        public async Task <ThirdPartyNoticesLicenseContext> GetLicensesAsync(string licenseExpression, CancellationToken token)
        {
            if (_licenseByCode.TryGetValue(licenseExpression, out var result))
            {
                return(result);
            }

            var codes = LicenseExpression.GetCodes(licenseExpression);

            result = new ThirdPartyNoticesLicenseContext {
                FullName = licenseExpression
            };

            foreach (var code in codes)
            {
                var index = await LoadLicenseIndexAsync(code, token);

                if (index != null)
                {
                    result.HRefs.Add(index.HRef);
                    if (!index.FileName.IsNullOrEmpty())
                    {
                        result.FileNames.Add(index.FileName);
                    }

                    if (codes.Count == 1)
                    {
                        result.FullName = index.FullName;
                    }
                }
            }

            _licenseByCode.Add(licenseExpression, result);
            return(result);
        }
Exemple #2
0
        public async ValueTask <bool> LicenseRequiresApprovalAsync(string licenseExpression, CancellationToken token)
        {
            var codes = LicenseExpression.GetCodes(licenseExpression);

            var result = false;

            foreach (var code in codes)
            {
                var license = await LoadLicenseAsync(code, token);

                if (license.RequiresApproval)
                {
                    result = true;
                }
            }

            return(result);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }