From 527e8d02b45502e086c5a6b2476601be662502ea Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Fri, 6 Sep 2019 12:11:16 +1000 Subject: [PATCH] manage package currency --- .../fhir/r5/utils/NPMPackageGenerator.java | 31 +++++++++++++++---- .../hl7/fhir/utilities/cache/NpmPackage.java | 4 +++ .../utilities/cache/PackageCacheManager.java | 26 ++++++++++++++-- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/NPMPackageGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/NPMPackageGenerator.java index 4825c137b..0ec09a46e 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/NPMPackageGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/NPMPackageGenerator.java @@ -26,11 +26,14 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; +import java.util.Date; import java.util.GregorianCalendar; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import java.util.TimeZone; @@ -84,8 +87,9 @@ public class NPMPackageGenerator { private BufferedOutputStream bufferedOutputStream; private GzipCompressorOutputStream gzipOutputStream; private JsonObject packageJ; + private JsonObject packageManifest; - public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig, String genDate) throws FHIRException, IOException { + public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig, Date date) throws FHIRException, IOException { super(); System.out.println("create package file at "+destFile); this.destFile = destFile; @@ -93,7 +97,7 @@ public class NPMPackageGenerator { List fhirVersion = new ArrayList<>(); for (Enumeration v : ig.getFhirVersion()) fhirVersion.add(v.asStringValue()); - buildPackageJson(canonical, kind, url, genDate, ig, fhirVersion); + buildPackageJson(canonical, kind, url, date, ig, fhirVersion); } public static NPMPackageGenerator subset(NPMPackageGenerator master, String destFile, String id, String name) throws FHIRException, IOException { @@ -107,12 +111,12 @@ public class NPMPackageGenerator { return new NPMPackageGenerator(destFile, p); } - public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig, String genDate, List fhirVersion) throws FHIRException, IOException { + public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig, Date date, List fhirVersion) throws FHIRException, IOException { super(); System.out.println("create package file at "+destFile); this.destFile = destFile; start(); - buildPackageJson(canonical, kind, url, genDate, ig, fhirVersion); + buildPackageJson(canonical, kind, url, date, ig, fhirVersion); } public NPMPackageGenerator(String destFile, JsonObject npm) throws FHIRException, IOException { @@ -129,7 +133,10 @@ public class NPMPackageGenerator { packageJ = npm; } - private void buildPackageJson(String canonical, PackageType kind, String web, String genDate, ImplementationGuide ig, List fhirVersion) throws FHIRException, IOException { + private void buildPackageJson(String canonical, PackageType kind, String web, Date date, ImplementationGuide ig, List fhirVersion) throws FHIRException, IOException { + String dtHuman = new SimpleDateFormat("EEE, MMM d, yyyy HH:mmZ", new Locale("en", "US")).format(date); + String dt = new SimpleDateFormat("yyyyMMddHHmmss").format(date); + CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); if (!ig.hasPackageId()) b.append("packageId"); @@ -150,6 +157,7 @@ public class NPMPackageGenerator { npm.addProperty("version", ig.getVersion()); npm.addProperty("tools-version", ToolsVersion.TOOLS_VERSION); npm.addProperty("type", kind.getCode()); + npm.addProperty("date", dt); if (ig.hasLicense()) npm.addProperty("license", ig.getLicense().toCode()); npm.addProperty("canonical", canonical); @@ -157,7 +165,7 @@ public class NPMPackageGenerator { if (ig.hasTitle()) npm.addProperty("title", ig.getTitle()); if (ig.hasDescription()) - npm.addProperty("description", ig.getDescription()+ " (built "+genDate+timezone()+")"); + npm.addProperty("description", ig.getDescription()+ " (built "+dtHuman+timezone()+")"); if (kind != PackageType.CORE) { JsonObject dep = new JsonObject(); npm.add("dependencies", dep); @@ -199,6 +207,13 @@ public class NPMPackageGenerator { } catch (UnsupportedEncodingException e) { } packageJ = npm; + + packageManifest = new JsonObject(); + packageManifest.addProperty("version", ig.getVersion()); + packageManifest.addProperty("fhirVersion", fhirVersion.toString()); + packageManifest.addProperty("date", dt); + packageManifest.addProperty("name", ig.getPackageId()); + } @@ -260,6 +275,10 @@ public class NPMPackageGenerator { bufferedOutputStream.close(); OutputStream.close(); TextFile.bytesToFile(OutputStream.toByteArray(), destFile); + // also, for cache management on current builds, generate a little manifest + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String json = gson.toJson(packageManifest); + TextFile.stringToFile(json, Utilities.changeFileExt(destFile, ".manifest.json")); } public String filename() { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/NpmPackage.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/NpmPackage.java index 58f7f249b..3b4f6e490 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/NpmPackage.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/NpmPackage.java @@ -269,6 +269,10 @@ import com.google.gson.JsonObject; public String name() { return npm.get("name").getAsString(); } + + public String date() { + return npm.has("date") ? npm.get("date").getAsString() : null; + } public String canonical() { return npm.has("canonical") ? npm.get("canonical").getAsString() : null; diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageCacheManager.java index da5950d74..8a4af5021 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageCacheManager.java @@ -581,11 +581,17 @@ public class PackageCacheManager { public NpmPackage loadPackage(String id, String v) throws FHIRException, IOException { NpmPackage p = loadPackageFromCacheOnly(id, v); - if (p != null) - return p; + if (p != null) { + if ("current".equals(v)) { + p = checkCurrency(id, p); + } + if (p != null) + return p; + } if ("dev".equals(v)) { p = loadPackageFromCacheOnly(id, "current"); + p = checkCurrency(id, p); if (p != null) return p; v = "current"; @@ -636,6 +642,22 @@ public class PackageCacheManager { } + private NpmPackage checkCurrency(String id, NpmPackage p) { + // special case: current versions roll over, and we have to check their currency + try { + String url = getPackageUrl(id); + JsonObject json = fetchJson(Utilities.pathURL(url, "package.version.json")); + String currDate = json.get("date").getAsString(); + String packDate = p.date(); + if (!currDate.equals(packDate)) + return null; // nup, we need a new copy + return p; + } catch (Exception e) { + return p; + } + } + + // !!! // if (packageId != null) { // return loadPackage();