add -install for packages

This commit is contained in:
Grahame Grieve 2023-04-21 18:53:20 +08:00
parent 1498823d5f
commit fbaff6cdb0
5 changed files with 56 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package org.hl7.fhir.validation;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@ -1114,4 +1115,9 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP
}
public Resource loadResource(byte[] content, String fn) throws FHIRException, IOException {
return igLoader.loadResourceByVersion(version, content, fn);
}
}

View File

@ -2,6 +2,7 @@ package org.hl7.fhir.validation.cli.services;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
@ -16,6 +17,7 @@ import java.util.Locale;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.conformance.R5ExtensionsLoader;
import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
import org.hl7.fhir.r5.context.ContextUtilities;
import org.hl7.fhir.r5.context.SimpleWorkerContext;
import org.hl7.fhir.r5.context.SystemOutLoggingService;
@ -51,6 +53,7 @@ import org.hl7.fhir.utilities.i18n.LanguageFileProducer.LanguageProducerSession;
import org.hl7.fhir.utilities.i18n.PoGetTextProducer;
import org.hl7.fhir.utilities.i18n.XLIFFProducer;
import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.hl7.fhir.utilities.settings.FhirSettings;
import org.hl7.fhir.utilities.validation.ValidationMessage;
import org.hl7.fhir.validation.Content;
@ -480,7 +483,7 @@ public class ValidationService {
}
public String determineVersion(CliContext cliContext, String sessionId) throws Exception {
if (cliContext.getMode() != EngineMode.VALIDATION) {
if (cliContext.getMode() != EngineMode.VALIDATION && cliContext.getMode() != EngineMode.INSTALL) {
return "5.0";
}
System.out.println("Scanning for versions (no -version parameter):");
@ -637,4 +640,42 @@ public class ValidationService {
// System.exit(ec > 0 ? 1 : 0);
//
// }
private int cp;
private int cs;
public void install(CliContext cliContext, ValidationEngine validator) throws FHIRException, IOException {
cp = 0;
cs = 0;
System.out.println("Generating Snapshots");
for (String ig : cliContext.getIgs()) {
processIG(validator, ig);
}
System.out.println("Installed/Processed "+cp+" packages, generated "+cs+" snapshots");
}
private void processIG(ValidationEngine validator, String ig) throws FHIRException, IOException {
validator.loadPackage(ig, null);
NpmPackage npm = validator.getPcm().loadPackage(ig);
if (!npm.isCore()) {
for (String d : npm.dependencies()) {
processIG(validator, d);
}
System.out.println("Processing "+ig);
cp++;
for (String d : npm.listResources("StructureDefinition")) {
String filename = npm.getFilePath(d);
Resource res = validator.loadResource(TextFile.fileToBytes(filename), filename);
if (!(res instanceof StructureDefinition))
throw new FHIRException("Require a StructureDefinition for generating a snapshot");
StructureDefinition sd = (StructureDefinition) res;
if (!sd.hasSnapshot()) {
StructureDefinition base = validator.getContext().fetchResource(StructureDefinition.class, sd.getBaseDefinition());
cs++;
new ProfileUtilities(validator.getContext(), null, null).setAutoFixSliceNames(true).generateSnapshot(base, sd, sd.getUrl(), null, sd.getName());
validator.handleOutput(sd, filename, validator.getVersion());
}
}
}
}
}

View File

@ -12,5 +12,6 @@ public enum EngineMode {
SPREADSHEET,
FHIRPATH,
VERSION,
RUN_TESTS
RUN_TESTS,
INSTALL
}

View File

@ -44,6 +44,7 @@ public class Params {
public static final String LANG_TRANSFORM = "-lang-transform";
public static final String NARRATIVE = "-narrative";
public static final String SNAPSHOT = "-snapshot";
public static final String INSTALL = "-install";
public static final String SCAN = "-scan";
public static final String TERMINOLOGY = "-tx";
public static final String TERMINOLOGY_LOG = "-txLog";
@ -261,6 +262,8 @@ public class Params {
cliContext.setMode(EngineMode.SPREADSHEET);
} else if (args[i].equals(SNAPSHOT)) {
cliContext.setMode(EngineMode.SNAPSHOT);
} else if (args[i].equals(INSTALL)) {
cliContext.setMode(EngineMode.INSTALL);
} else if (args[i].equals(RUN_TESTS)) {
// TODO setBaseTestingUtils test directory
cliContext.setMode(EngineMode.RUN_TESTS);

View File

@ -2,8 +2,9 @@ package org.hl7.fhir.validation.tests;
import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.utilities.Servers;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.settings.FhirSettings;
import org.hl7.fhir.validation.NativeHostServices;
import org.junit.jupiter.api.Test;
@ -20,7 +21,7 @@ public class NativeHostServiceTester {
NativeHostServices svc = new NativeHostServices();
svc.init("hl7.fhir.r4.core#4.0.1");
svc.connectToTxSvc(Utilities.path(Servers.TX_SERVER_DEV, "r4"), null, getTerminologyCacheDirectory("nativeHost").toString());
svc.connectToTxSvc(Utilities.path(FhirSettings.getTxFhirDevelopment(), "r4"), null, getTerminologyCacheDirectory("nativeHost").toString());
msg("base: "+svc.status());
svc.seeResource(TestingUtilities.loadTestResourceBytes("validator", "misc", "ValueSet-dicm-2-AnatomicModifier.json"), FhirFormat.JSON);