From 6b1ae88e242cc94631b01b47ae4f3cc3262f3f5a Mon Sep 17 00:00:00 2001 From: Oliver Egger Date: Tue, 8 Sep 2020 09:27:32 +0200 Subject: [PATCH] add patch wildcard for ig depencies --- .../hl7/fhir/utilities/VersionUtilities.java | 38 ++++++++++++++++++- .../cache/BasePackageCacheManager.java | 3 ++ .../cache/FilesystemPackageCacheManager.java | 15 +++++++- .../fhir/utilities/cache/PackageClient.java | 14 +++++++ .../hl7/fhir/validation/ValidationEngine.java | 12 ++++++ 5 files changed, 80 insertions(+), 2 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java index 3b303a035..91e8d46b2 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java @@ -193,6 +193,16 @@ public class VersionUtilities { } } + public static String getPatch(String version) { + if (version == null) + return null; + if (Utilities.charCount(version, '.') == 2) { + String[] p = version.split("\\."); + return p[2]; + } + return null; + } + public static boolean isSemVer(String version) { if (Utilities.charCount(version, '.') != 2) { return false; @@ -202,7 +212,7 @@ public class VersionUtilities { } /** - * return true if the current vresion equals test, or later + * return true if the current version equals test, or later * * so if a feature is defined in 4.0, if (VersionUtilities.isThisOrLater("4.0", version))... * @@ -213,10 +223,36 @@ public class VersionUtilities { public static boolean isThisOrLater(String test, String current) { String t = getMajMin(test); String c = getMajMin(current); + if (c.compareTo(t) == 0) { + return isMajMinOrLaterPatch(test, current); + } boolean ok = c.compareTo(t) >= 0; return ok; } + /** + * return true if the current version equals test for major and min, or later patch + * + * @param test + * @param current + * @return + */ + public static boolean isMajMinOrLaterPatch(String test, String current) { + String t = getMajMin(test); + String c = getMajMin(current); + if (c.compareTo(t) == 0) { + String pt = getPatch(test); + String pc = getPatch(current); + if (pt==null || "x".equals(pt)) { + return true; + } + if (pc!=null) { + return pc.compareTo(pt) >= 0; + } + } + return false; + } + public static String incMajorVersion(String v) { assert isSemVer(v); int[] parts = splitParts(v); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/BasePackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/BasePackageCacheManager.java index 82e295e89..5074396bd 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/BasePackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/BasePackageCacheManager.java @@ -69,6 +69,9 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { if (Utilities.noString(version)) { version = packageClient.getLatestVersion(id); } + if (version.endsWith(".x")) { + version = packageClient.getLatestVersion(id, version); + } InputStream stream = packageClient.fetch(id, version); String url = packageClient.url(id, version); return new InputStreamWithSrc(stream, url, version); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/FilesystemPackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/FilesystemPackageCacheManager.java index bf9bdac65..4415901af 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/FilesystemPackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/FilesystemPackageCacheManager.java @@ -39,6 +39,7 @@ import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.utilities.IniFile; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.VersionUtilities; import org.hl7.fhir.utilities.cache.NpmPackage.NpmPackageFolder; import org.hl7.fhir.utilities.json.JSONUtil; import org.slf4j.Logger; @@ -283,7 +284,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } /** - * Load the identified package from the cache - it it exists + * Load the identified package from the cache - if it exists *

* This is for special purpose only (testing, control over speed of loading). * Generally, use the loadPackage method @@ -307,10 +308,22 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple return p; } } + String foundPackage = null; + String foundVersion = null; for (String f : sorted(new File(cacheFolder).list())) { if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) { return loadPackageInfo(Utilities.path(cacheFolder, f)); } + if (version!=null && version.endsWith(".x") && f.contains("#")) { + String[] parts = f.split("#"); + if (parts[0].equals(id) && VersionUtilities.isMajMinOrLaterPatch((foundVersion!=null ? foundVersion : version),parts[1])) { + foundVersion = parts[1]; + foundPackage = f; + } + } + } + if (foundPackage!=null) { + return loadPackageInfo(Utilities.path(cacheFolder, foundPackage)); } if ("dev".equals(version)) return loadPackageFromCacheOnly(id, "current"); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageClient.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageClient.java index f384d2f2e..7171bf548 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageClient.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageClient.java @@ -208,5 +208,19 @@ public class PackageClient { } } + public String getLatestVersion(String id, String majMinVersion) throws IOException { + List list = getVersions(id); + if (list.isEmpty()) { + throw new IOException("Package not found: "+id); + } else { + String v = majMinVersion; + for (PackageInfo p : list) { + if (VersionUtilities.isMajMinOrLaterPatch(v, p.version)) { + v = p.version; + } + } + return v; + } + } } \ No newline at end of file diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java index b6ca6b9f2..d01ed9700 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java @@ -762,6 +762,18 @@ public class ValidationEngine implements IValidatorResourceFetcher, IPackageInst context.getLoadedPackages().add(pi.name()+"#"+pi.version()); Map res = new HashMap(); for (String s : pi.dependencies()) { + if (s.endsWith(".x") && s.length()>2) { + String packageMajorMinor = s.substring(0, s.length()-2); + boolean found = false; + for (int i=0; i