예제 #1
0
        public async Task WriteReadLibraryIndexJson()
        {
            var id    = new LibraryId("nuget.org", "Newtonsoft.Json", "12.0.2");
            var model = new LibraryIndexJson
            {
                License =
                {
                    Code   = "MIT",
                    Status = "HasToBeApproved"
                }
            };

            await _storage.Object.WriteLibraryIndexJsonAsync(id, model, CancellationToken.None);

            var actual = await _storage.Object.ReadLibraryIndexJsonAsync <LibraryIndexJson>(id, CancellationToken.None);

            using (var stream = await _storage.Object.OpenLibraryFileReadAsync(id, StorageExtensions.IndexFileName, CancellationToken.None))
            {
                Console.WriteLine(new StreamReader(stream).ReadToEnd());
            }

            actual.ShouldNotBeNull();
            actual.License.Code.ShouldBe("MIT");
            actual.License.Status.ShouldBe("HasToBeApproved");
        }
예제 #2
0
            public LibraryIndexJsonSnapshot(LibraryIndexJson index)
            {
                _licenseCode      = index.License.Code;
                _licenseBySubject = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

                foreach (var license in index.Licenses)
                {
                    _licenseBySubject.Add(license.Subject, GetLicenseText(license));
                }
            }
예제 #3
0
        public async ValueTask <bool> DownloadAsync(LibraryId id, CancellationToken token)
        {
            var index = await Storage.ReadLibraryIndexJsonAsync <LibraryIndexJson>(id, token);

            var isNew = false;

            if (index == null)
            {
                isNew = true;
                index = new LibraryIndexJson();
                await CreateNewAsync(id, index, token);
            }
            else
            {
                var exists = await Storage.LibraryFileExistsAsync(id, RepositoryPackageFileName, token);

                if (!exists && DownloadPackageIntoRepository)
                {
                    await GetPackageContentAsync(id, token);
                }
            }

            _packageContentById.TryRemove(id, out _);

            var indexSnapshot = isNew ? default : new LibraryIndexJsonSnapshot(index);

                                if (index.License.Code.IsNullOrEmpty())
                                {
                                    foreach (var license in index.Licenses)
                                    {
                                        await RefreshLicenseAsync(id, license, token);

                                        if (license.Subject.EqualsIgnoreCase(PackageLicense.SubjectPackage))
                                        {
                                            await CheckPackageLicenseCodeMatchUrlAsync(id, license, token);

                                            if (license.Description.IsNullOrEmpty())
                                            {
                                                index.License.Code = license.Code;
                                            }
                                        }
                                    }
                                }

                                if (isNew || indexSnapshot.HasChanges(index))
                                {
                                    await Storage.WriteLibraryIndexJsonAsync(id, index, token);
                                }

                                return(isNew);
        }
예제 #4
0
            public bool HasChanges(LibraryIndexJson index)
            {
                if (!string.Equals(_licenseCode, index.License.Code, StringComparison.Ordinal))
                {
                    return(true);
                }

                foreach (var license in index.Licenses)
                {
                    if (!_licenseBySubject.TryGetValue(license.Subject, out var text) ||
                        !text.Equals(GetLicenseText(license), StringComparison.Ordinal))
                    {
                        return(true);
                    }

                    _licenseBySubject.Remove(license.Subject);
                }

                return(_licenseBySubject.Count == 0);
            }
예제 #5
0
        protected override async Task CreateNewAsync(LibraryId id, LibraryIndexJson index, CancellationToken token)
        {
            var package = await GetPackageContentAsync(id, token);

            var specContent = await Api.ExtractPackageJsonAsync(package, token);

            await Storage.WriteLibraryFileAsync(id, NpmConstants.RepositoryPackageJsonFileName, specContent, token);

            var spec = Api.ParsePackageJson(specContent);

            index.Licenses.Add(await ResolvePackageLicenseAsync(id, spec.License?.Type, spec.License?.Value, null, token));

            if (spec.Repository?.Url != null)
            {
                index.Licenses.Add(await ResolveUrlLicenseAsync(id, spec.Repository.Url, PackageLicense.SubjectRepository, token));
            }

            if (!spec.HomePage.IsNullOrEmpty())
            {
                index.Licenses.Add(await ResolveUrlLicenseAsync(id, spec.HomePage, PackageLicense.SubjectHomePage, token));
            }
        }
예제 #6
0
        protected override async Task CreateNewAsync(LibraryId id, LibraryIndexJson index, CancellationToken token)
        {
            var package = await GetPackageContentAsync(id, token);

            var specContent = await Api.ExtractSpecAsync(new NuGetPackageId(id.Name, id.Version), package, token);

            await Storage.WriteLibraryFileAsync(id, NuGetConstants.RepositorySpecFileName, specContent, token);

            var spec = Api.ParseSpec(specContent);

            var specLicenseUrl = NuGetConstants.IsDeprecateLicenseUrl(spec.LicenseUrl) ? null : spec.LicenseUrl;

            index.Licenses.Add(await ResolvePackageLicenseAsync(id, spec.License?.Type, spec.License?.Value, specLicenseUrl, token));

            if (!string.IsNullOrEmpty(spec.Repository?.Url))
            {
                index.Licenses.Add(await ResolveUrlLicenseAsync(id, spec.Repository.Url, PackageLicense.SubjectRepository, token));
            }

            if (!spec.ProjectUrl.IsNullOrEmpty())
            {
                index.Licenses.Add(await ResolveUrlLicenseAsync(id, spec.ProjectUrl, PackageLicense.SubjectProject, token));
            }
        }
예제 #7
0
 protected abstract Task CreateNewAsync(LibraryId id, LibraryIndexJson index, CancellationToken token);