001package org.hl7.fhir.utilities.npm;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.util.ArrayList;
009import java.util.List;
010import java.util.Map;
011
012import org.hl7.fhir.utilities.TextFile;
013import org.hl7.fhir.utilities.Utilities;
014
015import com.google.gson.GsonBuilder;
016import com.google.gson.JsonArray;
017import com.google.gson.JsonObject;
018
019/**
020 * intenral use only - set the file name to edit in main(), and fill out the edit routine
021 * 
022 * @author grahame
023 *
024 */
025public class PackageHacker {
026
027  private static boolean useSecureReferences = false;
028  
029  public static void main(String[] args) throws FileNotFoundException, IOException {
030    new PackageHacker().edit("M:\\web\\hl7.org\\fhir\\5.0.0-snapshot1\\hl7.fhir.r5.examples.tgz");
031  }
032
033  private void edit(String name) throws FileNotFoundException, IOException {
034    File f = new File(name);
035    if (!f.exists())
036      throw new Error("Unable to find "+f.getAbsolutePath());
037    
038    NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f));
039    System.out.println("Altering Package "+f.getAbsolutePath());
040    System.out.println(nice(pck.getNpm()));
041    
042    change(pck.getNpm(), pck.getFolders().get("package").getContent());
043    
044    System.out.println("Revised Package");
045    System.out.println("=======================");
046    System.out.println(nice(pck.getNpm()));
047    System.out.println("=======================");
048    System.out.print("save? y/n: ");
049    int r = System.in.read();
050    if (r == 'y') {
051      f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak")));
052      pck.save(new FileOutputStream(f));
053    }   
054  }
055
056  private String nice(JsonObject json) {
057    return new GsonBuilder().setPrettyPrinting().create().toJson(json);
058  }
059
060  private void change(JsonObject npm, Map<String, byte[]> content) throws FileNotFoundException, IOException {
061    fixVersions(npm);
062    npm.remove("notForPublication");
063//    npm.addProperty("url", "http://hl7.org/fhir/us/carin-rtpbc/STU1");
064//    npm.remove("name");
065//    npm.addProperty("name", "hl7.fhir.uv.smart-app-launch");
066//    npm.remove("canonical");
067//    npm.addProperty("canonical", "http://hl7.org/fhir/us/davinci-drug-formulary");
068////    npm.remove("description");
069////    npm.addProperty("description", "Group Wrapper that includes all the R4 packages");
070//    npm.remove("url");
071//    npm.addProperty("url", "https://terminology.hl7.org/1.0.0/");
072//    npm.remove("dependencies");
073//    JsonObject dep = new JsonObject();
074//    npm.add("dependencies", dep);
075//    dep.addProperty("hl7.fhir.r3.core", "3.0.1");
076//    dep.addProperty("hl7.fhir.r4.examples", "4.0.1");
077//    dep.addProperty("hl7.fhir.r4.expansions", "4.0.1");
078//    dep.addProperty("hl7.fhir.r4.elements", "4.0.1");
079  }
080
081  private void fixVersions(JsonObject npm) {
082    npm.remove("fhirVersions");
083    JsonArray a = new JsonArray();
084    npm.add("fhirVersions", a);
085    a.add("3.0.1");
086  }
087
088  private void setProperty(JsonObject npm, String name, String value) {
089    npm.remove("homepage");
090    npm.addProperty("homepage", "http://hl7.org/fhir");    
091  }
092
093  private void fixNames(Map<String, byte[]> content) {
094    List<String> names = new ArrayList<>();
095    names.addAll(content.keySet());
096    for (String s : names) {
097      if (s.endsWith("json") && !s.endsWith(".json")) {
098        String n = s.substring(0, s.length()-4)+".json";
099        content.put(n, content.get(s));
100        content.remove(s);
101      }
102    }
103  }
104
105  private void addContentFrom(String folder, Map<String, byte[]> content) throws FileNotFoundException, IOException {
106    for (File f : new File(folder).listFiles()) {
107      if (f.getName().endsWith(".json") && !f.getName().endsWith(".canonical.json")) {
108         String cnt = TextFile.fileToString(f);
109         if (cnt.contains("\"resourceType\"")) {
110           content.put("package/"+f.getName(), TextFile.fileToBytes(f));
111         }
112      }
113    }
114  }
115
116  public static String fixPackageUrl(String webref) {
117    if (webref == null) {
118      return null;
119    }
120    // workaround for past publishing problems
121    switch (webref) {
122    case "file://C:\\GitHub\\hl7.fhir.us.breast-radiology#0.2.0\\output":   return "http://hl7.org/fhir/us/breast-radiology/2020May"; 
123    case "file://C:\\GitHub\\hl7.fhir.us.bser#1.0.0\\output":                return "http://hl7.org/fhir/us/bser/STU1"; 
124    case "file://C:\\GitHub\\hl7.fhir.us.carin-bb#0.1.0\\output":            return "http://hl7.org/fhir/us/carin-bb/2020Feb"; 
125    case "file://C:\\GitHub\\hl7.fhir.us.carin-rtpbc#0.1.0\\output":         return "http://hl7.org/fhir/us/carin-rtpbc/2020Feb"; 
126    case "file://C:\\GitHub\\hl7.fhir.us.cqfmeasures#1.1.0\\output":         return "http://hl7.org/fhir/us/cqfmeasures/2020Feb"; 
127    case "file://C:\\GitHub\\hl7.fhir.us.cqfmeasures#2.0.0\\output":         return "http://hl7.org/fhir/us/cqfmeasures/STU2"; 
128    case "file://C:\\GitHub\\hl7.fhir.us.davinci-alerts#0.2.0\\output":      return "http://hl7.org/fhir/us/davinci-alerts/2020Feb"; 
129    case "file://C:\\GitHub\\hl7.fhir.us.davinci-atr#0.1.0\\output":         return "http://hl7.org/fhir/us/davinci-atr/2020Feb";  
130    case "file://C:\\GitHub\\hl7.fhir.us.davinci-deqm#1.1.0\\output":        return "http://hl7.org/fhir/us/davinci-deqm/2020Feb"; 
131    case "file://C:\\GitHub\\hl7.fhir.us.davinci-deqm#1.0.0\\output":        return "http://hl7.org/fhir/us/davinci-deqm/STU1"; 
132    case "file://C:\\GitHub\\hl7.fhir.us.dme-orders#0.1.1\\output":          return "http://hl7.org/fhir/us/dme-orders/2020May"; 
133    case "file://C:\\GitHub\\hl7.fhir.us.ecr#1.0.0\\output":                 return "http://hl7.org/fhir/us/ecr/STU1"; 
134    case "file://C:\\GitHub\\hl7.fhir.us.mcode#1.0.0\\output":               return "http://hl7.org/fhir/us/mcode/STU1"; 
135    case "file://C:\\GitHub\\hl7.fhir.us.odh#1.0.0\\output":                 return "http://hl7.org/fhir/us/odh/STU1"; 
136    case "file://C:\\GitHub\\hl7.fhir.us.qicore#4.0.0\\output":              return "http://hl7.org/fhir/us/qicore/STU4"; 
137    case "file://C:\\GitHub\\hl7.fhir.uv.ips#1.0.0\\output":                 return "http://hl7.org/fhir/uv/ips/STU1"; 
138    case "file://C:\\GitHub\\hl7.fhir.uv.mhealth-framework#0.1.0\\output":   return "http://hl7.org/fhir/uv/mhealth-framework/2020May"; 
139    case "file://C:\\GitHub\\hl7.fhir.uv.security-label-ds4p#0.1.0\\output": return "http://hl7.org/fhir/uv/security-label-ds4p/2020May"; 
140    case "file://C:\\GitHub\\hl7.fhir.uv.shorthand#0.12.0\\output":          return "http://hl7.org/fhir/uv/shorthand/2020May"; 
141    case "http://build.fhir.org/branches/R4B//":                             return "http://hl7.org/fhir/2021Mar"; 
142    }
143
144    // https://github.com/HL7/fhir-ig-publisher/issues/295
145    if (webref.contains("hl7.org/fhir/us/core/STU4.0.0")) {
146      return webref.replace("hl7.org/fhir/us/core/STU4.0.0", "hl7.org/fhir/us/core/STU4");
147    }
148
149    if (isUseSecureReferences()) {
150      return webref.replace("http://hl7.org/fhir", "https://hl7.org/fhir").replace("http://build.fhir.org", "https://build.fhir.org");
151    } else {
152      return webref;
153    }
154  }
155
156  public static boolean isUseSecureReferences() {
157    return useSecureReferences;
158  }
159
160  public static void setUseSecureReferences(boolean useSecureReferences) {
161    PackageHacker.useSecureReferences = useSecureReferences;
162  }
163
164}