use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class NpmPackage method loadFiles.
public void loadFiles(String path, File source, String... exemptions) throws FileNotFoundException, IOException {
this.npm = (JsonObject) new com.google.gson.JsonParser().parse(TextFile.fileToString(Utilities.path(path, "package", "package.json")));
this.path = path;
File dir = new File(path);
for (File f : dir.listFiles()) {
if (!isInternalExemptFile(f) && !Utilities.existsInList(f.getName(), exemptions)) {
if (f.isDirectory()) {
String d = f.getName();
if (!d.equals("package")) {
d = Utilities.path("package", d);
}
NpmPackageFolder folder = this.new NpmPackageFolder(d);
folder.folder = f;
this.folders.put(d, folder);
File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json"));
if (ij.exists()) {
try {
if (!folder.readIndex(JsonTrackingParser.parseJson(ij))) {
indexFolder(folder.getName(), folder);
}
} catch (Exception e) {
throw new IOException("Error parsing " + ij.getAbsolutePath() + ": " + e.getMessage(), e);
}
}
loadSubFolders(dir.getAbsolutePath(), f);
} else {
NpmPackageFolder folder = this.new NpmPackageFolder(Utilities.path("package", "$root"));
folder.folder = dir;
this.folders.put(Utilities.path("package", "$root"), folder);
}
}
}
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class NpmPackage method save.
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(StandardCharsets.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"));
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class NpmPackage method fromZip.
public static NpmPackage fromZip(InputStream stream, boolean dropRootFolder, String desc) throws IOException {
NpmPackage res = new NpmPackage();
ZipInputStream zip = new ZipInputStream(stream);
ZipEntry ze;
while ((ze = zip.getNextEntry()) != null) {
int size;
byte[] buffer = new byte[2048];
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(bytes, buffer.length);
while ((size = zip.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
if (bytes.size() > 0) {
if (dropRootFolder) {
res.loadFile(ze.getName().substring(ze.getName().indexOf("/") + 1), bytes.toByteArray());
} else {
res.loadFile(ze.getName(), bytes.toByteArray());
}
}
zip.closeEntry();
}
zip.close();
try {
res.npm = JsonTrackingParser.parseJson(res.folders.get("package").fetchFile("package.json"));
} catch (Exception e) {
throw new IOException("Error parsing " + (desc == null ? "" : desc + "#") + "package/package.json: " + e.getMessage(), e);
}
res.checkIndexed(desc);
return res;
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class PackageHacker method edit.
private void edit(String name) throws FileNotFoundException, IOException {
File f = new File(name);
if (!f.exists())
throw new Error("Unable to find " + f.getAbsolutePath());
NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f));
System.out.println("Altering Package " + f.getAbsolutePath());
System.out.println(nice(pck.getNpm()));
change(pck.getNpm(), pck.getFolders().get("package").getContent());
System.out.println("Revised Package");
System.out.println("=======================");
System.out.println(nice(pck.getNpm()));
System.out.println("=======================");
System.out.print("save? y/n: ");
int r = System.in.read();
if (r == 'y') {
f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak")));
pck.save(new FileOutputStream(f));
}
}
use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.
the class BaseTestingUtilities method loadTestResource.
public static String loadTestResource(String... paths) throws IOException {
/**
* This 'if' condition checks to see if the fhir-test-cases project (https://github.com/FHIR/fhir-test-cases) is
* installed locally at the same directory level as the core library project is. If so, the test case data is read
* directly from that project, instead of the imported maven dependency jar. It is important, that if you want to
* test against the dependency imported from sonatype nexus, instead of your local copy, you need to either change
* the name of the project directory to something other than 'fhir-test-cases', or move it to another location, not
* at the same directory level as the core project.
*/
String dir = System.getenv("FHIR-TEST-CASES");
if (dir == null && ToolGlobalSettings.hasTestsPath()) {
dir = ToolGlobalSettings.getTestsPath();
}
if (dir != null && new CSFile(dir).exists()) {
String n = Utilities.path(dir, Utilities.path(paths));
// ok, we'll resolve this locally
return TextFile.fileToString(new CSFile(n));
} else {
// resolve from the package
String contents;
String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths));
try (InputStream inputStream = BaseTestingUtilities.class.getResourceAsStream(classpath)) {
if (inputStream == null) {
throw new IOException("Can't find file on classpath: " + classpath);
}
contents = IOUtils.toString(inputStream, java.nio.charset.StandardCharsets.UTF_8);
}
return contents;
}
}
Aggregations