add support for file: in package version (per NPM spec)

This commit is contained in:
Grahame Grieve 2020-02-09 08:14:30 +11:00
parent 398dbb003c
commit 4708fcb49e
10 changed files with 180 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
NpmPackageTests.class,
SnomedExpressionsTests.class,
GraphQLParserTests.class,
TurtleTests.class,
@ -21,7 +22,7 @@ import org.junit.runners.Suite.SuiteClasses;
BaseDateTimeTypeTest.class,
OpenApiGeneratorTest.class,
MetadataResourceManagerTester.class,
NpmPackageTests.class,
UtilitiesTests.class,
SnapShotGenerationTests.class})
public class AllR5Tests {

View File

@ -102,7 +102,7 @@ public class FHIRPathTests {
@Parameters(name = "{index}: file {0}")
public static Iterable<Object[]> data() throws ParserConfigurationException, SAXException, IOException {
Document dom = XMLUtil.parseToDom(TestingUtilities.loadTestResource("r5", "fhirpath", "tests-fhir-r4.xml"));
Document dom = XMLUtil.parseToDom(TestingUtilities.loadTestResource("r5", "fhirpath", "tests-fhir-r5.xml"));
List<Element> list = new ArrayList<Element>();
List<Element> groups = new ArrayList<Element>();

View File

@ -0,0 +1,36 @@
package org.hl7.fhir.r5.test;
import java.io.File;
import java.io.IOException;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.cache.NpmPackage;
import org.hl7.fhir.utilities.cache.PackageCacheManager;
import org.hl7.fhir.utilities.cache.ToolsVersion;
import org.junit.Assert;
import org.junit.Test;
public class PackageCacheTests {
@Test
public void testPath() throws IOException {
PackageCacheManager cache = new PackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
cache.clear();
NpmPackage npm = cache.loadPackage("hl7.fhir.pubpack", "0.0.3");
npm.loadAllFiles();
Assert.assertNotNull(npm);
File dir = new File(Utilities.path("[tmp]", "cache"));
if (dir.exists()) {
Utilities.clearDirectory(dir.getAbsolutePath());
} else {
Utilities.createDirectory(dir.getAbsolutePath());
}
npm.save(dir);
NpmPackage npm2 = cache.loadPackage("hl7.fhir.pubpack", "file:"+dir.getAbsolutePath());
Assert.assertNotNull(npm2);
}
}

View File

@ -0,0 +1,18 @@
package org.hl7.fhir.r5.test;
import java.io.IOException;
import org.hl7.fhir.utilities.Utilities;
import org.junit.Test;
import junit.framework.Assert;
public class UtilitiesTests {
@Test
public void testPath() throws IOException {
Assert.assertEquals(Utilities.path("[tmp]", "test.txt"), "c:\\temp\\test.txt");
Assert.assertEquals(Utilities.path("[user]", "test.txt"), System.getProperty("user.home")+"\\test.txt");
Assert.assertEquals(Utilities.path("[JAVA_HOME]", "test.txt"), System.getenv("JAVA_HOME")+"\\test.txt");
}
}

View File

@ -63,6 +63,7 @@ import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
@ -590,11 +591,15 @@ public class Utilities {
a = a.substring(File.separator.length());
while (a.startsWith(".."+File.separator)) {
String p = s.toString().substring(0, s.length()-1);
if (!p.contains(File.separator)) {
s = new StringBuilder();
if (s.length() == 0) {
s = new StringBuilder(Paths.get(".").toAbsolutePath().normalize().toString());
} else {
s = new StringBuilder(p.substring(0, p.lastIndexOf(File.separator))+File.separator);
String p = s.toString().substring(0, s.length()-1);
if (!p.contains(File.separator)) {
s = new StringBuilder();
} else {
s = new StringBuilder(p.substring(0, p.lastIndexOf(File.separator))+File.separator);
}
}
a = a.substring(3);
}
@ -608,6 +613,9 @@ public class Utilities {
}
private static boolean hasCTempDir() {
if (!System.getProperty("os.name").toLowerCase().contains("win")) {
return false;
}
File tmp = new File("c:\\temp");
return tmp.exists() && tmp.isDirectory();
}

View File

@ -186,7 +186,8 @@ public class NpmPackage {
public static void loadFiles(NpmPackage res, String path, File source, String... exemptions) throws FileNotFoundException, IOException {
res.npm = (JsonObject) new com.google.gson.JsonParser().parse(TextFile.fileToString(Utilities.path(path, "package", "package.json")));
res.path = path;
File dir = new File(path);
for (File f : dir.listFiles()) {
if (!Utilities.existsInList(f.getName(), ".git", ".svn") && !Utilities.existsInList(f.getName(), exemptions)) {
@ -622,6 +623,37 @@ public class NpmPackage {
return folders;
}
public void save(File directory) throws IOException {
File dir = new File(Utilities.path(directory.getAbsolutePath(), name()));
if (!dir.exists()) {
Utilities.createDirectory(dir.getAbsolutePath());
} else {
Utilities.clearDirectory(dir.getAbsolutePath());
}
for (NpmPackageFolder folder : folders.values()) {
String n = folder.name;
File pd = new File(Utilities.path(dir.getAbsolutePath(), n));
if (!pd.exists()) {
Utilities.createDirectory(pd.getAbsolutePath());
}
NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
indexer.start();
for (String s : folder.content.keySet()) {
byte[] b = folder.content.get(s);
indexer.seeFile(s, b);
if (!s.equals(".index.json") && !s.equals("package.json")) {
TextFile.bytesToFile(b, Utilities.path(dir.getAbsolutePath(), n, s));
}
}
byte[] cnt = indexer.build().getBytes(Charset.forName("UTF-8"));
TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), n, ".index.json"));
}
byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false);
TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), "package", "package.json"));
}
public void save(OutputStream stream) throws IOException {
TarArchiveOutputStream tar;
ByteArrayOutputStream OutputStream;
@ -774,5 +806,17 @@ public class NpmPackage {
folder.types.put(type, new ArrayList<>());
folder.types.get(type).add(name);
}
public void loadAllFiles() throws IOException {
for (String folder : folders.keySet()) {
NpmPackageFolder pf = folders.get(folder);
String p = Utilities.path(path, folder);
for (File f : new File(p).listFiles()) {
if (!f.isDirectory()) {
pf.getContent().put(f.getName(), TextFile.fileToBytes(f));
}
}
}
}
}

View File

@ -268,8 +268,10 @@ public class PackageCacheManager {
private void clearCache() throws IOException {
for (File f : new File(cacheFolder).listFiles()) {
if (f.isDirectory())
if (f.isDirectory()) {
Utilities.clearDirectory(f.getAbsolutePath());
FileUtils.deleteDirectory(f);
}
else if (!f.getName().equals("packages.ini"))
FileUtils.forceDelete(f);
}
@ -552,6 +554,13 @@ public class PackageCacheManager {
* @throws IOException
*/
public NpmPackage loadPackageFromCacheOnly(String id, String version) throws IOException {
if (Utilities.noString(version)) {
throw new FHIRException("Invalid version - ''");
}
if (version.startsWith("file:")) {
return loadPackageFromFile(id, version.substring(5));
}
for (NpmPackage p : temporaryPackages) {
if (p.name().equals(id) && ("current".equals(version) || "dev".equals(version) || p.version().equals(version)))
return p;
@ -571,6 +580,7 @@ public class PackageCacheManager {
* Add an already fetched package to the cache
*/
public NpmPackage addPackageToCache(String id, String version, InputStream tgz, String sourceDesc) throws IOException {
checkValidVersionString(version, id);
if (progress ) {
System.out.println("Installing "+id+"#"+(version == null ? "?" : version)+" to the package cache");
System.out.print(" Fetching:");
@ -648,11 +658,31 @@ public class PackageCacheManager {
return pck;
}
private void checkValidVersionString(String version, String id) {
if (Utilities.noString(version)) {
throw new FHIRException("Cannot add package "+id+" to the package cache - a version must be provided");
}
if (version.startsWith("file:")) {
throw new FHIRException("Cannot add package "+id+" to the package cache - the version '"+version+"' is illegal in this context");
}
for (char ch : version.toCharArray()) {
if (!Character.isAlphabetic(ch) && !Character.isDigit(ch) && !Utilities.existsInList(ch, '.', '-')) {
throw new FHIRException("Cannot add package "+id+" to the package cache - the version '"+version+"' is illegal (ch '"+ch+"'");
}
}
}
public NpmPackage loadPackage(String id) throws FHIRException, IOException {
throw new Error("Not done yet");
}
public NpmPackage loadPackage(String id, String v) throws FHIRException, IOException {
if (Utilities.noString(v)) {
throw new FHIRException("Invalid version - ''");
}
if (v.startsWith("file:")) {
return loadPackageFromFile(id, v.substring(5));
}
NpmPackage p = loadPackageFromCacheOnly(id, v);
if (p != null) {
if ("current".equals(v)) {
@ -756,6 +786,16 @@ public class PackageCacheManager {
* @return
*/
public boolean hasPackage(String id, String version) {
if (Utilities.noString(version)) {
throw new FHIRException("Invalid version - ''");
}
if (version.startsWith("file:")) {
try {
return loadPackageFromFile(id, version.substring(5)) != null;
} catch (IOException e) {
return false;
}
}
for (NpmPackage p : temporaryPackages) {
if (p.name().equals(id) && ("current".equals(version) || "dev".equals(version) || p.version().equals(version)))
return true;
@ -772,6 +812,21 @@ public class PackageCacheManager {
}
private NpmPackage loadPackageFromFile(String id, String folder) throws IOException {
File f = new File(Utilities.path(folder, id));
if (!f.exists()) {
throw new FHIRException("Package '"+id+" not found in folder "+folder);
}
if (!f.isDirectory()) {
throw new FHIRException("File for '"+id+" found in folder "+folder+", not a folder");
}
File fp = new File(Utilities.path(folder, id, "package", "package.json"));
if (!fp.exists()) {
throw new FHIRException("Package '"+id+" found in folder "+folder+", but does not contain a package.json file in /package");
}
return NpmPackage.fromFolder(f.getAbsolutePath());
}
/**
* List which versions of a package are available
*

View File

@ -433,7 +433,7 @@ public class ValidationEngine implements IValidatorResourceFetcher {
return fetchFromUrl(src+(v == null ? "" : "|"+v), explore);
}
File f = new File(src);
File f = new File(Utilities.path(src));
if (f.exists()) {
if (f.isDirectory() && new File(Utilities.path(src, "package.tgz")).exists())
return loadPackage(new FileInputStream(Utilities.path(src, "package.tgz")), Utilities.path(src, "package.tgz"));

View File

@ -17,7 +17,7 @@
<properties>
<hapi_fhir_version>4.1.0</hapi_fhir_version>
<validator_test_case_version>1.0.40-SNAPSHOT</validator_test_case_version>
<validator_test_case_version>1.0.41-SNAPSHOT</validator_test_case_version>
</properties>
<artifactId>org.hl7.fhir.core</artifactId>

View File

@ -23,6 +23,9 @@ IF %ERRORLEVEL% NEQ 0 (
GOTO DONE
)
call "C:\tools\versionNotes.exe" -fileName C:\work\org.hl7.fhir\latest-ig-publisher\release-notes-validator.md -version %newver% -fileDest C:\temp\current-release-notes-validator.md -url https://fhir.github.io/latest-ig-publisher/org.hl7.fhir.validator.jar -maven https://oss.sonatype.org/service/local/artifact/maven/redirect?r=snapshots&g=ca.uhn.hapi.fhir&a=org.hl7.fhir.validation.cli&v=%newver%-SNAPSHOT&e=jar
copy org.hl7.fhir.validation.cli\target\org.hl7.fhir.validation.cli-%newver%-SNAPSHOT.jar ..\latest-ig-publisher\org.hl7.fhir.validator.jar
cd ..\latest-ig-publisher
call git commit -a -m "Release new version %newver%-SNAPSHOT"
@ -30,7 +33,11 @@ call git push origin master
cd ..\org.hl7.fhir.core
call python c:\tools\zulip-api\zulip\zulip\send.py --stream committers/notification --subject "java core" -m "New Java Core v%newver%-SNAPSHOT released. New Validator at https://oss.sonatype.org/service/local/artifact/maven/redirect?r=snapshots&g=ca.uhn.hapi.fhir&a=org.hl7.fhir.validation.cli&v=%newver%-SNAPSHOT&e=jar, and also deployed at https://fhir.github.io/latest-ig-publisher/org.hl7.fhir.validator.jar" --config-file zuliprc
call python c:\tools\zulip-api\zulip\zulip\send.py --stream tooling/releases --subject "Validator" -m "New Validator @ https://fhir.github.io/latest-ig-publisher/org.hl7.fhir.validator.jar (v%newver%)" --config-file zuliprc
call python c:\tools\zulip-api\zulip\zulip\send.py --stream tooling/releases --subject "Validator" --config-file zuliprc < C:\temp\current-release-notes-validator.md
del C:\temp\current-release-notes-validator.md
pause
:DONE
echo ===============================================================