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