This commit is contained in:
Grahame Grieve 2020-09-17 16:48:45 +10:00
commit 697b23e855
5 changed files with 80 additions and 2 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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
* <p>
* 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");

View File

@ -208,5 +208,19 @@ public class PackageClient {
}
}
public String getLatestVersion(String id, String majMinVersion) throws IOException {
List<PackageInfo> 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;
}
}
}

View File

@ -762,6 +762,18 @@ public class ValidationEngine implements IValidatorResourceFetcher, IPackageInst
context.getLoadedPackages().add(pi.name()+"#"+pi.version());
Map<String, byte[]> res = new HashMap<String, byte[]>();
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<context.getLoadedPackages().size() && !found; ++i) {
String loadedPackage = context.getLoadedPackages().get(i);
if (loadedPackage.startsWith(packageMajorMinor)) {
found = true;
}
}
if (found)
continue ;
}
if (! context.getLoadedPackages().contains(s)) {
if (!VersionUtilities.isCorePackage(s)) {
System.out.println("+ .. load IG from "+s);