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.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -204,6 +205,9 @@ public class NpmPackage {
super();
}
/**
* Factory method that parses a package from an extracted folder
*/
public static NpmPackage fromFolder(String path) throws IOException {
NpmPackage res = new NpmPackage();
loadFiles(res, path, new File(path));
@ -211,6 +215,15 @@ public class NpmPackage {
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() {
return userData;
}
@ -787,7 +800,7 @@ public class NpmPackage {
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"));
}
byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false);
@ -825,7 +838,7 @@ public class NpmPackage {
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");
entry.setSize(cnt.length);
tar.putArchiveEntry(entry);
@ -942,6 +955,9 @@ public class NpmPackage {
}
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);
folder.content.put(name, cnt);
if (!folder.types.containsKey(type))

View File

@ -63,9 +63,14 @@ public class PackageGenerator {
throw new Error("Unknown Type");
}
}
private OutputStream stream;
private JsonObject object;
public PackageGenerator() {
object = new JsonObject();
}
public PackageGenerator(OutputStream stream) {
super();
this.stream = stream;
@ -80,6 +85,10 @@ public class PackageGenerator {
}
public JsonObject getRootJsonObject() {
return object;
}
public void commit() throws IOException {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(object);