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.nio.charset.StandardCharsets; 009import java.util.ArrayList; 010import java.util.List; 011import java.util.Map; 012 013import org.hl7.fhir.utilities.TextFile; 014import org.hl7.fhir.utilities.Utilities; 015import org.hl7.fhir.utilities.VersionUtilities; 016import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 017import org.hl7.fhir.utilities.json.model.JsonArray; 018import org.hl7.fhir.utilities.json.model.JsonObject; 019import org.hl7.fhir.utilities.json.parser.JsonParser; 020 021 022 023/** 024 * intenral use only - set the file name to edit in main(), and fill out the edit routine 025 * 026 * @author grahame 027 * 028 */ 029public class PackageHacker { 030 031 private static boolean useSecureReferences = false; 032 033 public static void main(String[] args) throws FileNotFoundException, IOException { 034// new PackageHacker().massEdit(new File("/Users/grahamegrieve/web/hl7.org/fhir")); 035// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.core.tgz"); 036// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.corexml.tgz"); 037// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.examples.tgz"); 038// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.expansions.tgz"); 039// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.search.tgz"); 040// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.core.tgz"); 041// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.corexml.tgz"); 042// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.examples.tgz"); 043// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.expansions.tgz"); 044// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.search.tgz"); 045 046// new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/us/core/v311/package.tgz", "http://hl7.org/fhir/us/core/STU3.1.1"); 047 // new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/us/core/v700/package.tgz", "http://hl7.org/fhir/us/core/STU7"); 048 049 PackageHacker self = new PackageHacker(); 050// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/hl7.terminology.r4b.tgz"); 051// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/1.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/1.0.0/hl7.terminology.r4b.tgz"); 052// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/2.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/2.0.0/hl7.terminology.r4b.tgz"); 053 self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/2.1.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/2.1.0/hl7.terminology.r4b.tgz"); 054// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/3.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/3.0.0/hl7.terminology.r4b.tgz"); 055// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/3.1.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/3.1.0/hl7.terminology.r4b.tgz"); 056// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/4.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/4.0.0/hl7.terminology.r4b.tgz"); 057// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.0.0/hl7.terminology.r4b.tgz"); 058// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.1.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.1.0/hl7.terminology.r4b.tgz"); 059// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.2.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.2.0/hl7.terminology.r4b.tgz"); 060// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.3.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.3.0/hl7.terminology.r4b.tgz"); 061// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.4.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.4.0/hl7.terminology.r4b.tgz"); 062// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.5.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.5.0/hl7.terminology.r4b.tgz"); 063// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/6.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/6.0.0/hl7.terminology.r4b.tgz"); 064// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/6.0.1/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/6.0.1/hl7.terminology.r4b.tgz"); 065// self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/6.0.2/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/6.0.2/hl7.terminology.r4b.tgz"); 066 } 067 068 private void cloneToR4B(String src, String dst) throws IOException { 069 FileInputStream fs = ManagedFileAccess.inStream(src); 070 NpmPackage pck = NpmPackage.fromPackage(fs); 071 System.out.println(nice(pck.getNpm())); 072 JsonObject json = pck.getNpm(); 073 String name = json.asString("name"); 074 json.remove("name"); 075 json.add("name", name.replace(".r4", ".r4b")); 076 json.remove("fhirVersions"); 077 json.remove("dependencies"); 078 JsonArray fv = new JsonArray(); 079 json.add("fhirVersions", fv); 080 fv.add("4.3.0"); 081 JsonObject dep = new JsonObject(); 082 json.add("dependencies", dep); 083 dep.add(VersionUtilities.packageForVersion("4.3.0"), "4.3.0"); 084 pck.save(new FileOutputStream(dst)); 085 } 086 087// private void massEdit(File dir) throws IOException { 088// System.out.println("process "+dir.getAbsolutePath()); 089// for (File f : dir.listFiles()) { 090// if (f.isDirectory()) { 091// massEdit(f); 092// } else if (f.getName().equals("package.tgz")) { 093// try { 094// FileInputStream fs = ManagedFileAccess.inStream(f); 095// NpmPackage pck = NpmPackage.fromPackage(fs); 096// if ("fhir.core".equals(pck.getNpm().str("type"))) { 097// System.out.println("!!change "+f.getAbsolutePath()); 098// pck.getNpm().remove("type"); 099// pck.getNpm().set("type", "Core"); 100// FileOutputStream fso = ManagedFileAccess.outStream(f); 101// try { 102// pck.save(fso); 103// } finally { 104// fso.close(); 105// } 106// } 107// } catch (Exception e) { 108// System.out.println("!!Error: "+e.getMessage()); 109// } 110// } else if (f.getName().startsWith("hl7.fhir.r") && f.getName().endsWith(".examples.tgz")) { 111// try { 112// FileInputStream fs = ManagedFileAccess.inStream(f); 113// NpmPackage pck = NpmPackage.fromPackage(fs); 114// if ("fhir.examples".equals(pck.getNpm().str("type"))) { 115// System.out.println("!!change "+f.getAbsolutePath()); 116// pck.getNpm().remove("type"); 117// pck.getNpm().set("type", "Examples"); 118// FileOutputStream fso = ManagedFileAccess.outStream(f); 119// try { 120// pck.save(fso); 121// } finally { 122// fso.close(); 123// } 124// } 125// } catch (Exception e) { 126// System.out.println("!!Error: "+e.getMessage()); 127// } 128// 129// } 130// } 131// } 132 133 private void edit(String name, String path) throws FileNotFoundException, IOException { 134 File f = ManagedFileAccess.file(name); 135 if (!f.exists()) 136 throw new Error("Unable to find "+f.getAbsolutePath()); 137 138 System.out.println("Loading Package "+f.getAbsolutePath()); 139 NpmPackage pck = null; 140 FileInputStream fs = ManagedFileAccess.inStream(f); 141 try { 142 pck = NpmPackage.fromPackage(fs); 143 } finally { 144 fs.close(); 145 } 146 System.out.println("Altering Package "+f.getAbsolutePath()); 147 System.out.println(nice(pck.getNpm())); 148 149 if (change(pck.getNpm(), path)) { 150 151 System.out.println("Revised Package"); 152 System.out.println("======================="); 153 System.out.println(nice(pck.getNpm())); 154 System.out.println("======================="); 155 System.out.print("save? y/n: "); 156 int r = System.in.read(); 157 if (r == 'y') { 158 f.renameTo(ManagedFileAccess.file(Utilities.changeFileExt(name, ".tgz.bak"))); 159 FileOutputStream fso = ManagedFileAccess.outStream(f); 160 try { 161 pck.save(fso); 162 } finally { 163 fso.close(); 164 } 165 } 166 } 167 } 168 169 private void fixExampleContent(Map<String, byte[]> content) { 170 // byte[] cnt = content.get("ServiceRequest-SDOHCC-ServiceRequestCompletedFoodPantryApplicationAssistExample.json"); 171 // content.put("ServiceRequest-SDOHCC-ServiceRequestCompletedFoodPantryApplicationAssist.json", cnt); 172 // content.remove("ServiceRequest-SDOHCC-ServiceRequestCompletedFoodPantryApplicationAssistExample.json"); 173 } 174 175 private void fixContent(Map<String, byte[]> content) { 176 // fixVersionInContent(content); 177 178 } 179 180 private String nice(JsonObject json) { 181 return JsonParser.compose(json, true); 182 } 183 184 private boolean change(JsonObject npm, String path) throws FileNotFoundException, IOException { 185 npm.remove("url"); 186 npm.add("url", path); 187 return true; 188// if (npm.has("notForPublication")) { 189// npm.remove("notForPublication"); 190// return true; 191// } 192// return false; 193 } 194 195 private void fixVersionInContent(Map<String, byte[]> content) { 196 for (String n : content.keySet()) { 197 if (n.endsWith(".json") || n.endsWith(".xml") || n.endsWith(".xsd")) { 198 String json = new String(content.get(n)); 199 if (json.contains("4.3.0-cibuild") && !json.contains("4.3.0-snapshot1")) { 200 json = json.replace("4.3.0-cibuild", "4.3.0"); 201 content.put(n, json.getBytes(StandardCharsets.UTF_8)); 202 } 203 } 204 } 205 206 } 207 208 private void fixVersions(JsonObject npm) { 209 npm.remove("fhirVersions"); 210 JsonArray a = new JsonArray(); 211 npm.add("fhirVersions", a); 212 a.add("3.0.1"); 213 } 214 215 private void setProperty(JsonObject npm, String name, String value) { 216 npm.remove("homepage"); 217 npm.add("homepage", "http://hl7.org/fhir"); 218 } 219 220 private void fixNames(Map<String, byte[]> content) { 221 List<String> names = new ArrayList<>(); 222 names.addAll(content.keySet()); 223 for (String s : names) { 224 if (s.endsWith("json") && !s.endsWith(".json")) { 225 String n = s.substring(0, s.length()-4)+".json"; 226 content.put(n, content.get(s)); 227 content.remove(s); 228 } 229 } 230 } 231 232 private void addContentFrom(String folder, Map<String, byte[]> content) throws FileNotFoundException, IOException { 233 for (File f : ManagedFileAccess.file(folder).listFiles()) { 234 if (f.getName().endsWith(".json") && !f.getName().endsWith(".canonical.json")) { 235 String cnt = TextFile.fileToString(f); 236 if (cnt.contains("\"resourceType\"")) { 237 content.put("package/"+f.getName(), TextFile.fileToBytes(f)); 238 } 239 } 240 } 241 } 242 243 public static String fixPackageUrl(String webref) { 244 if (webref == null) { 245 return null; 246 } 247 // workaround for past publishing problems 248 switch (webref) { 249 case "file://C:\\GitHub\\hl7.fhir.us.breast-radiology#0.2.0\\output": return "http://hl7.org/fhir/us/breast-radiology/2020May"; 250 case "file://C:\\GitHub\\hl7.fhir.us.bser#1.0.0\\output": return "http://hl7.org/fhir/us/bser/STU1"; 251 case "file://C:\\GitHub\\hl7.fhir.us.carin-bb#0.1.0\\output": return "http://hl7.org/fhir/us/carin-bb/2020Feb"; 252 case "file://C:\\GitHub\\hl7.fhir.us.carin-rtpbc#0.1.0\\output": return "http://hl7.org/fhir/us/carin-rtpbc/2020Feb"; 253 case "file://C:\\GitHub\\hl7.fhir.us.cqfmeasures#1.1.0\\output": return "http://hl7.org/fhir/us/cqfmeasures/2020Feb"; 254 case "file://C:\\GitHub\\hl7.fhir.us.cqfmeasures#2.0.0\\output": return "http://hl7.org/fhir/us/cqfmeasures/STU2"; 255 case "file://C:\\GitHub\\hl7.fhir.us.davinci-alerts#0.2.0\\output": return "http://hl7.org/fhir/us/davinci-alerts/2020Feb"; 256 case "file://C:\\GitHub\\hl7.fhir.us.davinci-atr#0.1.0\\output": return "http://hl7.org/fhir/us/davinci-atr/2020Feb"; 257 case "file://C:\\GitHub\\hl7.fhir.us.davinci-deqm#1.1.0\\output": return "http://hl7.org/fhir/us/davinci-deqm/2020Feb"; 258 case "file://C:\\GitHub\\hl7.fhir.us.davinci-deqm#1.0.0\\output": return "http://hl7.org/fhir/us/davinci-deqm/STU1"; 259 case "file://C:\\GitHub\\hl7.fhir.us.dme-orders#0.1.1\\output": return "http://hl7.org/fhir/us/dme-orders/2020May"; 260 case "file://C:\\GitHub\\hl7.fhir.us.ecr#1.0.0\\output": return "http://hl7.org/fhir/us/ecr/STU1"; 261 case "file://C:\\GitHub\\hl7.fhir.us.mcode#1.0.0\\output": return "http://hl7.org/fhir/us/mcode/STU1"; 262 case "file://C:\\GitHub\\hl7.fhir.us.odh#1.0.0\\output": return "http://hl7.org/fhir/us/odh/STU1"; 263 case "file://C:\\GitHub\\hl7.fhir.us.qicore#4.0.0\\output": return "http://hl7.org/fhir/us/qicore/STU4"; 264 case "file://C:\\GitHub\\hl7.fhir.uv.ips#1.0.0\\output": return "http://hl7.org/fhir/uv/ips/STU1"; 265 case "file://C:\\GitHub\\hl7.fhir.uv.mhealth-framework#0.1.0\\output": return "http://hl7.org/fhir/uv/mhealth-framework/2020May"; 266 case "file://C:\\GitHub\\hl7.fhir.uv.security-label-ds4p#0.1.0\\output": return "http://hl7.org/fhir/uv/security-label-ds4p/2020May"; 267 case "file://C:\\GitHub\\hl7.fhir.uv.shorthand#0.12.0\\output": return "http://hl7.org/fhir/uv/shorthand/2020May"; 268 case "http://build.fhir.org/branches/R4B//": return "http://hl7.org/fhir/2021Mar"; 269 } 270 271 // https://github.com/HL7/fhir-ig-publisher/issues/295 272 if (webref.contains("hl7.org/fhir/us/core/STU4.0.0")) { 273 return webref.replace("hl7.org/fhir/us/core/STU4.0.0", "hl7.org/fhir/us/core/STU4"); 274 } 275 276 if (isUseSecureReferences()) { 277 return webref.replace("http://hl7.org/fhir", "https://hl7.org/fhir").replace("http://build.fhir.org", "https://build.fhir.org"); 278 } else { 279 return webref; 280 } 281 } 282 283 public static boolean isUseSecureReferences() { 284 return useSecureReferences; 285 } 286 287 public static void setUseSecureReferences(boolean useSecureReferences) { 288 PackageHacker.useSecureReferences = useSecureReferences; 289 } 290 291}