manage package currency

This commit is contained in:
Grahame Grieve 2019-09-06 12:11:16 +10:00
parent b32fb65327
commit 527e8d02b4
3 changed files with 53 additions and 8 deletions

View File

@ -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<String> fhirVersion = new ArrayList<>();
for (Enumeration<FHIRVersion> 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<String> fhirVersion) throws FHIRException, IOException {
public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig, Date date, List<String> 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<String> fhirVersion) throws FHIRException, IOException {
private void buildPackageJson(String canonical, PackageType kind, String web, Date date, ImplementationGuide ig, List<String> 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() {

View File

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

View File

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