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