001package org.hl7.fhir.utilities.npm; 002 003import java.io.File; 004import java.io.IOException; 005 006import org.hl7.fhir.exceptions.FHIRException; 007import org.hl7.fhir.utilities.TextFile; 008import org.hl7.fhir.utilities.Utilities; 009import org.hl7.fhir.utilities.json.model.JsonArray; 010import org.hl7.fhir.utilities.json.model.JsonObject; 011import org.hl7.fhir.utilities.json.parser.JsonParser; 012 013/** 014 * This class builds the .index.json for a package 015 * 016 * @author grahame 017 * 018 */ 019public class NpmPackageIndexBuilder { 020 021 public static final Integer CURRENT_INDEX_VERSION = 2; 022 private JsonObject index; 023 private JsonArray files; 024 025 public void start() { 026 index = new JsonObject(); 027 index.add("index-version", CURRENT_INDEX_VERSION); 028 files = new JsonArray(); 029 index.add("files", files); 030 } 031 032 public boolean seeFile(String name, byte[] content) { 033 if (name.endsWith(".json")) { 034 try { 035 JsonObject json = JsonParser.parseObject(content); 036 if (json.has("resourceType")) { 037 // ok we treat it as a resource 038 JsonObject fi = new JsonObject(); 039 files.add(fi); 040 fi.add("filename", name); 041 fi.add("resourceType", json.asString("resourceType")); 042 if (json.hasPrimitive("id")) { 043 fi.add("id", json.asString("id")); 044 } 045 if (json.hasPrimitive("url")) { 046 fi.add("url", json.asString("url")); 047 } 048 if (json.hasPrimitive("version")) { 049 fi.add("version", json.asString("version")); 050 } 051 if (json.hasPrimitive("kind")) { 052 fi.add("kind", json.asString("kind")); 053 } 054 if (json.hasPrimitive("type")) { 055 fi.add("type", json.asString("type")); 056 } 057 if (json.hasPrimitive("supplements")) { 058 fi.add("supplements", json.asString("supplements")); 059 } 060 if (json.hasPrimitive("content")) { 061 fi.add("content", json.asString("content")); 062 } 063 } 064 } catch (Exception e) { 065 System.out.println("Error parsing "+name+": "+e.getMessage()); 066 if (name.contains("openapi")) { 067 return false; 068 } 069 } 070 } 071 return true; 072 } 073 074 public String build() { 075 String res = JsonParser.compose(index, true); 076 index = null; 077 files = null; 078 return res; 079 } 080 081// private Map<String, List<String>> types = new HashMap<>(); 082// private Map<String, String> canonicalMap = new HashMap<>(); 083 084 085 public void executeWithStatus(String folder) throws IOException { 086 System.out.print("Index Package "+folder+" ... "); 087 execute(folder); 088 System.out.println("done"); 089 } 090 091 public void execute(String folder) throws IOException { 092 if (existsFolder(folder, "package")) { 093 folder = Utilities.path(folder, "package"); 094 } 095 if (!existsFile(folder, "package.json")) { 096 throw new FHIRException("Not a proper package? (can't find package.json)"); 097 } 098 start(); 099 File dir = new File(folder); 100 for (File f : dir.listFiles()) { 101 seeFile(f.getName(), TextFile.fileToBytes(f)); 102 } 103 TextFile.stringToFile(build(), Utilities.path(folder, ".index.json")); 104 } 105 106 107 private boolean existsFolder(String... args) throws IOException { 108 File f = new File(Utilities.path(args)); 109 return f.exists() && f.isDirectory(); 110 } 111 112 private boolean existsFile(String... args) throws IOException { 113 File f = new File(Utilities.path(args)); 114 return f.exists() && !f.isDirectory(); 115 } 116 117 118 public static void main(String[] args) throws IOException { 119 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.core"); 120 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.examples"); 121 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.expansions"); 122 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.elements"); 123 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.core"); 124 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.examples"); 125 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.expansions"); 126 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.elements"); 127 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.core"); 128 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.examples"); 129 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.expansions"); 130 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.core"); 131 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.examples"); 132 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.expansions"); 133 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.elements"); 134 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r2"); 135 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r3"); 136 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r4"); 137 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r2"); 138 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r3"); 139 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r4"); 140 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#1.0.2"); 141 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#1.4.0"); 142 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#3.0.2"); 143 new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#4.0.1"); 144 } 145 146}