From 6bb44cfcaca600d1af36fa8c4f8ede18e9eff61f Mon Sep 17 00:00:00 2001 From: Phillip Kruger Date: Mon, 16 Oct 2023 11:19:55 +1100 Subject: [PATCH] Small fixes Signed-off-by: Phillip Kruger --- .gitignore | 1 + pom.xml | 10 ++-- .../java/io/mvnpm/file/type/PomClient.java | 40 ++++++++++---- .../java/io/mvnpm/maven/UrlPathParser.java | 24 +++++++++ .../java/io/mvnpm/npm/NpmRegistryFacade.java | 10 +++- src/main/java/io/mvnpm/npm/model/License.java | 7 +++ .../mvnpm/npm/model/LicenseDeserializer.java | 52 +++++++++++++++++++ src/main/java/io/mvnpm/npm/model/Package.java | 4 +- .../npm/model/RepositoryDeserializer.java | 41 +++++++++++++++ .../io/mvnpm/version/VersionConverter.java | 9 ++-- 10 files changed, 175 insertions(+), 23 deletions(-) create mode 100644 src/main/java/io/mvnpm/npm/model/License.java create mode 100644 src/main/java/io/mvnpm/npm/model/LicenseDeserializer.java create mode 100644 src/main/java/io/mvnpm/npm/model/RepositoryDeserializer.java diff --git a/.gitignore b/.gitignore index f4f0673..5a72630 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ nbactions.xml # Local environment .env /dev.sh +/local.sh diff --git a/pom.xml b/pom.xml index 194b3ec..86ce7ae 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 io.mvnpm mvnpm - 2.0.20-SNAPSHOT + 2.0.24-SNAPSHOT mvnpm Maven on NPM @@ -56,7 +56,7 @@ UTF-8 quarkus-bom io.quarkus.platform - 3.4.2 + 3.4.3 true 3.0.0 2.23.0 @@ -65,7 +65,7 @@ 2.1.0.Final - 1.1.3 + 1.1.4 1.0.10 @@ -430,7 +430,7 @@ org.mvnpm.at.codemirror commands - 6.2.5 + 6.3.0 runtime @@ -460,7 +460,7 @@ org.mvnpm.at.codemirror view - 6.20.0 + 6.21.2 runtime diff --git a/src/main/java/io/mvnpm/file/type/PomClient.java b/src/main/java/io/mvnpm/file/type/PomClient.java index 4c225e3..7c6346a 100644 --- a/src/main/java/io/mvnpm/file/type/PomClient.java +++ b/src/main/java/io/mvnpm/file/type/PomClient.java @@ -9,7 +9,6 @@ import java.net.URL; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Properties; @@ -118,13 +117,22 @@ private String toUrl(Model model, URL homepage) { } } - private List toLicenses(String license) { - if (license != null && !license.isEmpty()) { - License l = new License(); - l.setName(license); - return List.of(l); + private List toLicenses(io.mvnpm.npm.model.License license) { + License l = new License(); + + if (license != null) { + if (license.type() != null) { + l.setName(license.type()); + } + if (license.url() != null) { + l.setUrl(license.url().toString()); + } } - return Collections.EMPTY_LIST; + + if (l.getName() == null && l.getUrl() == null) { + l.setName("none"); + } + return List.of(l); } private Organization toOrganization(io.mvnpm.npm.model.Package p) { @@ -168,22 +176,32 @@ private Scm toScm(Repository repository) { s.setConnection(conn); s.setDeveloperConnection(conn); return s; + } else { + Scm s = new Scm(); + s.setUrl("https://github.com/mvnpm/mvnpm.git"); + s.setConnection("https://github.com/mvnpm/mvnpm.git"); + s.setDeveloperConnection("https://github.com/mvnpm/mvnpm.git"); + return s; } - return null; + } private List toDevelopers(List maintainers) { + List ds = new ArrayList<>(); if (maintainers != null && !maintainers.isEmpty()) { - List ds = new ArrayList<>(); for (Maintainer m : maintainers) { Developer d = new Developer(); d.setEmail(m.email()); d.setName(m.name()); ds.add(d); } - return ds; } - return Collections.EMPTY_LIST; + if (ds.isEmpty()) { + Developer d = new Developer(); + d.setName("unknown"); + ds.add(d); + } + return ds; } private List toDependencies(Map dependencies) { diff --git a/src/main/java/io/mvnpm/maven/UrlPathParser.java b/src/main/java/io/mvnpm/maven/UrlPathParser.java index f356df7..1510d6f 100644 --- a/src/main/java/io/mvnpm/maven/UrlPathParser.java +++ b/src/main/java/io/mvnpm/maven/UrlPathParser.java @@ -1,6 +1,8 @@ package io.mvnpm.maven; import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import io.mvnpm.Constants; import io.mvnpm.npm.model.Name; @@ -26,6 +28,9 @@ public static Name parseMavenMetaDataXml(String fullName) { } public static NameVersionType parseMavenFile(String urlPath) { + if (urlPath.contains("/git:/") || urlPath.contains("/git+http:/") || urlPath.contains("/git+https:/")) { // We do not support git repos as version. Maybe something we can add later + urlPath = cleanUrlPath(urlPath); + } String[] parts = urlPath.split(Constants.SLASH); // We need at least 3 (name / version / filename) @@ -42,6 +47,25 @@ public static NameVersionType parseMavenFile(String urlPath) { fullName = fullName.replaceFirst(Constants.AT_SLASH, Constants.AT); } + // Make sure the version is in the correct format + version = fixVersion(version); return new NameVersionType(NameParser.fromNpmProject(fullName), version); } + + private static String cleanUrlPath(String urlPath) { + Matcher m = PATTERN.matcher(urlPath); + return m.replaceAll("latest"); + } + + private static String fixVersion(String version) { + String[] parts = version.split("\\."); + + for (int i = parts.length; i < 3; i++) { + version += ".0"; + } + + return version; + } + + private static final Pattern PATTERN = Pattern.compile("git\\+.*?\\.git"); } diff --git a/src/main/java/io/mvnpm/npm/NpmRegistryFacade.java b/src/main/java/io/mvnpm/npm/NpmRegistryFacade.java index a21600e..a830803 100644 --- a/src/main/java/io/mvnpm/npm/NpmRegistryFacade.java +++ b/src/main/java/io/mvnpm/npm/NpmRegistryFacade.java @@ -28,17 +28,23 @@ public Project getProject(String project) { if (response.getStatus() < 300) { return response.readEntity(Project.class); } else { - throw new WebApplicationException(response); + throw new WebApplicationException("Error while getting Project for [" + project + "]", response); } } @CacheResult(cacheName = "npm-package-cache") public io.mvnpm.npm.model.Package getPackage(String project, String version) { + if (null == version || version.startsWith("git:/") || version.startsWith("git+http")) { + // We do not support git repos as version. Maybe something we can add later + version = "*"; + } + Response response = npmRegistryClient.getPackage(project, version); if (response.getStatus() < 300) { return response.readEntity(io.mvnpm.npm.model.Package.class); } else { - throw new WebApplicationException(response); + throw new WebApplicationException("Error while getting Package for [" + project + "] version [" + version + "]", + response); } } diff --git a/src/main/java/io/mvnpm/npm/model/License.java b/src/main/java/io/mvnpm/npm/model/License.java new file mode 100644 index 0000000..cec7366 --- /dev/null +++ b/src/main/java/io/mvnpm/npm/model/License.java @@ -0,0 +1,7 @@ +package io.mvnpm.npm.model; + +import java.net.URL; + +public record License(String type, URL url) { + +} diff --git a/src/main/java/io/mvnpm/npm/model/LicenseDeserializer.java b/src/main/java/io/mvnpm/npm/model/LicenseDeserializer.java new file mode 100644 index 0000000..9a22b5d --- /dev/null +++ b/src/main/java/io/mvnpm/npm/model/LicenseDeserializer.java @@ -0,0 +1,52 @@ +package io.mvnpm.npm.model; + +import java.io.IOException; +import java.net.URL; + +import com.fasterxml.jackson.core.JacksonException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +/** + * To handle some License values that comes in with a String only + * + * @author Phillip Kruger (phillip.kruger@gmail.com) + */ +public class LicenseDeserializer extends StdDeserializer { + + public LicenseDeserializer() { + this(null); + } + + public LicenseDeserializer(Class vc) { + super(vc); + } + + @Override + public License deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException { + try { + return jp.readValueAs(License.class); + } catch (Throwable t) { + // Let try with String + try { + URL url = jp.readValueAs(URL.class); + return new License(null, url); + } catch (Throwable tt) { + try { + String type = jp.readValueAs(String.class); + return new License(type, null); + } catch (Throwable ttt) { + try { + String type = jp.getText(); + return new License(type, null); + } catch (Throwable tttt) { + ttt.printStackTrace(); + return null; + } + + } + } + } + } +} diff --git a/src/main/java/io/mvnpm/npm/model/Package.java b/src/main/java/io/mvnpm/npm/model/Package.java index ed105f4..abd8590 100644 --- a/src/main/java/io/mvnpm/npm/model/Package.java +++ b/src/main/java/io/mvnpm/npm/model/Package.java @@ -12,10 +12,10 @@ public record Package( Name name, String version, String description, - String license, + @JsonDeserialize(using = LicenseDeserializer.class) License license, @JsonDeserialize(using = AuthorDeserializer.class) Author author, URL homepage, - Repository repository, + @JsonDeserialize(using = RepositoryDeserializer.class) Repository repository, @JsonDeserialize(using = BugsDeserializer.class) Bugs bugs, String main, String module, diff --git a/src/main/java/io/mvnpm/npm/model/RepositoryDeserializer.java b/src/main/java/io/mvnpm/npm/model/RepositoryDeserializer.java new file mode 100644 index 0000000..a715d6a --- /dev/null +++ b/src/main/java/io/mvnpm/npm/model/RepositoryDeserializer.java @@ -0,0 +1,41 @@ +package io.mvnpm.npm.model; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JacksonException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +/** + * To handle some Repository values that comes in with a String only + * + * @author Phillip Kruger (phillip.kruger@gmail.com) + */ +public class RepositoryDeserializer extends StdDeserializer { + + public RepositoryDeserializer() { + this(null); + } + + public RepositoryDeserializer(Class vc) { + super(vc); + } + + @Override + public Repository deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException { + try { + return jp.readValueAs(Repository.class); + } catch (Throwable t) { + // Let try with String + try { + String url = jp.readValueAs(String.class); + return new Repository(null, url, null); + } catch (Throwable tt) { + tt.printStackTrace(); + return null; + } + } + } + +} diff --git a/src/main/java/io/mvnpm/version/VersionConverter.java b/src/main/java/io/mvnpm/version/VersionConverter.java index a2926bf..211b747 100644 --- a/src/main/java/io/mvnpm/version/VersionConverter.java +++ b/src/main/java/io/mvnpm/version/VersionConverter.java @@ -46,8 +46,9 @@ private VersionConverter() { public static String convert(String versionString) { try { - if (null == versionString) + if (null == versionString || versionString.startsWith("git:/") || versionString.startsWith("git+http")) { // We do not support git repos as version. Maybe something we can add later versionString = EMPTY; + } versionString = versionString.trim(); String[] orSet; @@ -238,7 +239,8 @@ private static String getLowerBoundary(String s) { } else if (s.startsWith(GREATER_THAN)) { return OPEN_ROUND + Version.fromString(s.substring(1)); } else { - throw new InvalidVersionException(s); + // Equal. If no operator is specified, then equality is assumed, so this operator is optional, but MAY be included. + return cleanVersion(s); } } @@ -248,7 +250,8 @@ private static String getUpperBoundary(String s) { } else if (s.startsWith(LESS_THAN)) { return Version.fromString(s.substring(1)) + CLOSE_ROUND; } else { - throw new InvalidVersionException(s); + // Equal. If no operator is specified, then equality is assumed, so this operator is optional, but MAY be included. + return cleanVersion(s); } }