Search in sources :

Example 1 with Dist

use of org.commonjava.indy.pkg.npm.model.Dist in project indy by Commonjava.

the class VersionMetadataDeserializer method deserialize.

@Override
public VersionMetadata deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    final ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
    final JsonNode vNode = mapper.readTree(jsonParser);
    VersionMetadata vm = new VersionMetadata(parseValue(vNode, "name"), parseValue(vNode, "version"));
    vm.setDescription(parseValue(vNode, "description"));
    JsonNode repoNode = vNode.get("repository");
    if (repoNode instanceof ArrayNode) {
        repoNode = repoNode.get(0);
    }
    vm.setRepository(new Repository(parseValue(repoNode, "type"), parseValue(repoNode, "url")));
    vm.setAuthor(parseObject(mapper, vNode.get("author"), UserInfo.class));
    vm.setBugs(parseObject(mapper, vNode.get("bugs"), Bugs.class));
    vm.setDist(parseObject(mapper, vNode.get("dist"), Dist.class));
    vm.setDirectories(parseObject(mapper, vNode.get("directories"), Directories.class));
    vm.setKeywords(parseList(mapper, vNode.get("keywords"), String.class));
    vm.setLicense(parseObject(mapper, vNode.get("license"), License.class));
    vm.setMain(parseValue(vNode, "main"));
    vm.setUrl(parseValue(vNode, "url"));
    vm.setContributors(parseList(mapper, vNode.get("contributors"), UserInfo.class));
    vm.setEngines(parseList(mapper, vNode.get("engines"), Engines.class));
    vm.setDependencies(parseObject(mapper, vNode.get("dependencies"), Map.class));
    vm.setDevDependencies(parseObject(mapper, vNode.get("devDependencies"), Map.class));
    vm.setMaintainers(parseList(mapper, vNode.get("maintainers"), UserInfo.class));
    vm.setLicenses(parseList(mapper, vNode.get("licenses"), License.class));
    vm.setScripts(parseObject(mapper, vNode.get("scripts"), Map.class));
    return vm;
}
Also used : Bugs(org.commonjava.indy.pkg.npm.model.Bugs) Dist(org.commonjava.indy.pkg.npm.model.Dist) License(org.commonjava.indy.pkg.npm.model.License) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserInfo(org.commonjava.indy.pkg.npm.model.UserInfo) Engines(org.commonjava.indy.pkg.npm.model.Engines) Directories(org.commonjava.indy.pkg.npm.model.Directories) Repository(org.commonjava.indy.pkg.npm.model.Repository) VersionMetadata(org.commonjava.indy.pkg.npm.model.VersionMetadata) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Dist

use of org.commonjava.indy.pkg.npm.model.Dist in project indy by Commonjava.

the class PackageMetadataGenerator method writePackageMetadata.

private boolean writePackageMetadata(List<StoreResource> firstLevelFiles, ArtifactStore store, String path, EventMetadata eventMetadata) throws IndyWorkflowException {
    logger.debug("writePackageMetadata, firstLevelFiles:{}, store:{}", firstLevelFiles, store.getKey());
    // Parse the path of the tar (e.g.: jquery/-/jquery-7.6.1.tgz or @types/jquery/-/jquery-2.2.3.tgz)
    // to get the version, then try to get the version metadata by the path (@scoped/)package/version
    List<PackagePath> packagePaths = firstLevelFiles.stream().map((res) -> new PackagePath(res.getPath())).sorted(Comparator.comparing(PackagePath::getVersion)).collect(Collectors.toList());
    if (packagePaths.size() == 0) {
        return false;
    }
    final Transfer metadataFile = fileManager.getTransfer(store, path);
    final PackageMetadata packageMetadata = new PackageMetadata();
    final IndyObjectMapper mapper = new IndyObjectMapper(true);
    List<String> keywords = new ArrayList<>();
    DistTag distTags = new DistTag();
    Map<String, VersionMetadata> versions = new LinkedHashMap<>();
    PackagePath latest = packagePaths.get(packagePaths.size() - 1);
    for (PackagePath packagePath : packagePaths) {
        String versionPath = packagePath.getVersionPath();
        logger.debug("Retrieving the version file {} from store {}", versionPath, store);
        Transfer metaFile = fileManager.retrieveRaw(store, versionPath, eventMetadata);
        if (metaFile == null) {
            // The metadata file (@scoped/)package/version for the specific version is missing, still need to extract it from tarball
            String tarPath = packagePath.getTarPath();
            Transfer tar = fileManager.retrieveRaw(store, tarPath, eventMetadata);
            if (tar == null) {
                logger.warn("Tarball file {} is missing in the store {}.", tarPath, store.getKey());
                continue;
            }
            logger.info("Extracting package metadata package.json from tarball {} and store it in {}/{}", tarPath, store.getKey(), versionPath);
            metaFile = extractMetaFileFromTarballAndStore(store, versionPath, tar);
            if (metaFile == null) {
                logger.warn("Package metadata is missing in tarball {}/{}.", store.getKey(), tarPath);
                continue;
            }
        }
        try (InputStream input = metaFile.openInputStream()) {
            VersionMetadata versionMetadata = mapper.readValue(input, VersionMetadata.class);
            if (versionMetadata == null) {
                continue;
            }
            versions.put(versionMetadata.getVersion(), versionMetadata);
            if (versionMetadata.getKeywords() != null) {
                for (String keyword : versionMetadata.getKeywords()) {
                    if (!keywords.contains(keyword)) {
                        keywords.add(keyword);
                    }
                }
            }
            // Set couple of attributes based on the latest version metadata
            if (packagePath.getVersion().equals(latest.getVersion())) {
                packageMetadata.setName(versionMetadata.getName());
                packageMetadata.setDescription(versionMetadata.getDescription());
                packageMetadata.setAuthor(versionMetadata.getAuthor());
                if (versionMetadata.getLicense() != null) {
                    packageMetadata.setLicense(versionMetadata.getLicense());
                }
                packageMetadata.setRepository(versionMetadata.getRepository());
                packageMetadata.setBugs(versionMetadata.getBugs());
                distTags.setLatest(versionMetadata.getVersion());
            }
            // Generate tarball url if missing
            if (versionMetadata.getDist() == null) {
                // here we use mock host. indy will amend it with the right hostname
                String tarball = "http://indy/" + packagePath.getTarPath();
                // logger.debug( "Generate dist tarball: {}", tarball );
                versionMetadata.setDist(new Dist(null, tarball));
            }
        } catch (IOException e) {
            logger.error("Get the version metadata error from path {}", versionPath, e);
            throw new IndyWorkflowException("Get the version metadata error from path {}", versionPath);
        }
    }
    if (!keywords.isEmpty()) {
        packageMetadata.setKeywords(keywords);
    }
    versions = sortVersions(versions);
    packageMetadata.setVersions(versions);
    packageMetadata.setDistTags(distTags);
    OutputStream stream = null;
    try {
        String output = mapper.writeValueAsString(packageMetadata);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(output.getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new IndyWorkflowException("Generating package metadata failure in store {}", store.getKey());
    } finally {
        closeQuietly(stream);
    }
    logger.debug("writePackageMetadata, DONE, store: {}", store.getKey());
    return true;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) Dist(org.commonjava.indy.pkg.npm.model.Dist) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) DistTag(org.commonjava.indy.pkg.npm.model.DistTag) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) VersionMetadata(org.commonjava.indy.pkg.npm.model.VersionMetadata) PackageMetadata(org.commonjava.indy.pkg.npm.model.PackageMetadata) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer)

Aggregations

Dist (org.commonjava.indy.pkg.npm.model.Dist)2 VersionMetadata (org.commonjava.indy.pkg.npm.model.VersionMetadata)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)1 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)1 IndyObjectMapper (org.commonjava.indy.model.core.io.IndyObjectMapper)1 Bugs (org.commonjava.indy.pkg.npm.model.Bugs)1 Directories (org.commonjava.indy.pkg.npm.model.Directories)1 DistTag (org.commonjava.indy.pkg.npm.model.DistTag)1 Engines (org.commonjava.indy.pkg.npm.model.Engines)1 License (org.commonjava.indy.pkg.npm.model.License)1