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;
}
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;
}
Aggregations