A bit of package rework

This commit is contained in:
jamesagnew 2020-06-02 18:02:25 -04:00
parent 0d8cab0b6e
commit 56d933199c
2 changed files with 56 additions and 31 deletions

View File

@ -40,6 +40,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -204,6 +205,9 @@ public class NpmPackage {
super(); super();
} }
/**
* Factory method that parses a package from an extracted folder
*/
public static NpmPackage fromFolder(String path) throws IOException { public static NpmPackage fromFolder(String path) throws IOException {
NpmPackage res = new NpmPackage(); NpmPackage res = new NpmPackage();
loadFiles(res, path, new File(path)); loadFiles(res, path, new File(path));
@ -211,6 +215,15 @@ public class NpmPackage {
return res; return res;
} }
/**
* Factory method that starts a new empty package using the given PackageGenerator to create the manifest
*/
public static NpmPackage empty(PackageGenerator thePackageGenerator) {
NpmPackage retVal = new NpmPackage();
retVal.npm = thePackageGenerator.getRootJsonObject();
return retVal;
}
public Map<String, Object> getUserData() { public Map<String, Object> getUserData() {
return userData; return userData;
} }
@ -787,7 +800,7 @@ public class NpmPackage {
TextFile.bytesToFile(b, Utilities.path(dir.getAbsolutePath(), n, s)); TextFile.bytesToFile(b, Utilities.path(dir.getAbsolutePath(), n, s));
} }
} }
byte[] cnt = indexer.build().getBytes(Charset.forName("UTF-8")); byte[] cnt = indexer.build().getBytes(StandardCharsets.UTF_8);
TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), n, ".index.json")); TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), n, ".index.json"));
} }
byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false); byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false);
@ -825,7 +838,7 @@ public class NpmPackage {
tar.closeArchiveEntry(); tar.closeArchiveEntry();
} }
} }
byte[] cnt = indexer.build().getBytes(Charset.forName("UTF-8")); byte[] cnt = indexer.build().getBytes(StandardCharsets.UTF_8);
TarArchiveEntry entry = new TarArchiveEntry(n+"/.index.json"); TarArchiveEntry entry = new TarArchiveEntry(n+"/.index.json");
entry.setSize(cnt.length); entry.setSize(cnt.length);
tar.putArchiveEntry(entry); tar.putArchiveEntry(entry);
@ -942,6 +955,9 @@ public class NpmPackage {
} }
public void addFile(String folderName, String name, byte[] cnt, String type) { public void addFile(String folderName, String name, byte[] cnt, String type) {
if (!folders.containsKey(folderName)) {
folders.put(folderName, new NpmPackageFolder(folderName));
}
NpmPackageFolder folder = folders.get(folderName); NpmPackageFolder folder = folders.get(folderName);
folder.content.put(name, cnt); folder.content.put(name, cnt);
if (!folder.types.containsKey(type)) if (!folder.types.containsKey(type))

View File

@ -1,33 +1,33 @@
package org.hl7.fhir.utilities.cache; package org.hl7.fhir.utilities.cache;
/* /*
Copyright (c) 2011+, HL7, Inc. Copyright (c) 2011+, HL7, Inc.
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met: are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, * Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to * Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific endorse or promote products derived from this software without specific
prior written permission. prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
@ -63,9 +63,14 @@ public class PackageGenerator {
throw new Error("Unknown Type"); throw new Error("Unknown Type");
} }
} }
private OutputStream stream; private OutputStream stream;
private JsonObject object; private JsonObject object;
public PackageGenerator() {
object = new JsonObject();
}
public PackageGenerator(OutputStream stream) { public PackageGenerator(OutputStream stream) {
super(); super();
this.stream = stream; this.stream = stream;
@ -79,7 +84,11 @@ public class PackageGenerator {
object = parser.parse(TextFile.streamToString(template)).getAsJsonObject(); object = parser.parse(TextFile.streamToString(template)).getAsJsonObject();
} }
public JsonObject getRootJsonObject() {
return object;
}
public void commit() throws IOException { public void commit() throws IOException {
Gson gson = new GsonBuilder().setPrettyPrinting().create(); Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(object); String json = gson.toJson(object);