Merge pull request #109 from lmckenzi/Enable_appending_in_packages

Enable packages containing instructions to append files
This commit is contained in:
Grahame Grieve 2019-11-21 20:17:19 +13:00 committed by GitHub
commit f08e903fbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -61,6 +61,9 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
@ -194,8 +197,13 @@ public class TextFile {
OutputStream sw = new FileOutputStream(file);
sw.write(bytes);
sw.flush();
sw.close();
sw.close();
}
public static void appendBytesToFile(byte[] bytes, String path) throws IOException {
byte[] linebreak = new byte[] {13, 10};
Files.write(Paths.get(path), linebreak, StandardOpenOption.APPEND);
Files.write(Paths.get(path), bytes, StandardOpenOption.APPEND);
}
public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException {

View File

@ -498,10 +498,26 @@ public class NpmPackage {
}
public void unPack(String dir) throws IOException {
unPack (dir, false);
}
public void unPackWithAppend(String dir) throws IOException {
unPack (dir, true);
}
public void unPack(String dir, boolean withAppend) throws IOException {
for (String s : content.keySet()) {
String fn = Utilities.path(dir, s);
String dn = Utilities.getDirectoryForFile(fn);
Utilities.createDirectory(dn);
File f = new File(s);
if (withAppend && f.getName().startsWith("_append.")) {
String appendFn = Utilities.path(dir, Utilities.getDirectoryForFile(s), f.getName().substring(8));
if (new File(appendFn).exists())
TextFile.appendBytesToFile(content.get(s), appendFn);
else
TextFile.bytesToFile(content.get(s), appendFn);
} else
TextFile.bytesToFile(content.get(s), fn);
}
if (path != null)