1 11 package org.eclipse.pde.internal.build; 12 13 import java.io.*; 14 import org.eclipse.pde.internal.swt.tools.IconExe; 15 16 19 public class BrandingIron implements IXMLConstants { 20 private static final String MARKER_NAME = "%EXECUTABLE_NAME%"; private static final String BUNDLE_NAME = "%BUNDLE_NAME%"; private static final String ICON_NAME = "%ICON_NAME%"; private static final String MARKER_KEY = "<key>CFBundleExecutable</key>"; private static final String BUNDLE_KEY = "<key>CFBundleName</key>"; private static final String ICON_KEY = "<key>CFBundleIconFile</key>"; private static final String STRING_START = "<string>"; private static final String STRING_END = "</string>"; private static final String XDOC_ICON = "-Xdock:icon=../Resources/Eclipse.icns"; 30 private String [] icons = null; 31 private String root; 32 private String name; 33 private String os = "win32"; private boolean brandIcons = true; 35 36 public void setName(String value) { 37 name = value; 38 } 39 40 public void setIcons(String value) { 41 icons = value.split(",\\s*"); if (icons[0].startsWith("${")) { if (icons.length > 1) { 44 String [] temp = new String [icons.length - 1]; 45 System.arraycopy(icons, 1, temp, 0, temp.length); 46 icons = temp; 47 } else { 48 icons = null; 49 } 50 } 51 } 52 53 public void setRoot(String value) { 54 root = value; 55 } 56 57 public void brand() throws Exception { 58 if (name.startsWith("${")) return; 61 62 if (icons == null || icons[0].startsWith("${")) brandIcons = false; 64 65 String testName = os.equals("win32") ? name + ".exe" : name; if (!new File(root).exists() || (!brandIcons && new File(root, testName).exists())) 69 return; 70 71 if ("win32".equals(os)) brandWindows(); 73 if ("linux".equals(os)) brandLinux(); 75 if ("solaris".equals(os)) brandSolaris(); 77 if ("macosx".equals(os)) brandMac(); 79 if ("aix".equals(os)) brandAIX(); 81 if ("hpux".equals(os)) brandHPUX(); 83 } 84 85 private void brandAIX() { 86 renameLauncher(); 87 } 88 89 private void brandHPUX() { 90 renameLauncher(); 91 } 92 93 private void brandLinux() throws Exception { 94 renameLauncher(); 95 if (brandIcons) 96 copy(new File(icons[0]), new File(root, "icon.xpm")); 97 } 98 99 private void brandSolaris() throws Exception { 100 renameLauncher(); 101 if (brandIcons == false) 102 return; 103 104 for (int i = 0; i < icons.length; i++) { 105 String icon = icons[i]; 106 if (icon.endsWith(".l.pm")) copy(new File(icon), new File(root, name + ".l.pm")); if (icon.endsWith(".m.pm")) copy(new File(icon), new File(root, name + ".m.pm")); 110 if (icon.endsWith(".s.pm")) 111 copy(new File(icon), new File(root, name + ".s.pm")); 112 if (icon.endsWith(".t.pm")) 113 copy(new File(icon), new File(root, name + ".t.pm")); 114 } 115 } 116 117 private void brandMac() throws Exception { 118 122 String target = root + '/' + name + ".app/Contents"; new File(target).mkdirs(); 125 new File(target + "/MacOS").mkdirs(); 126 new File(target + "/Resources").mkdirs(); 127 128 String initialRoot = root + "/Launcher.app/Contents"; if (!new File(initialRoot).exists()) 130 initialRoot = root + "/Eclipse.app/Contents"; copyMacLauncher(initialRoot, target); 132 String iconName = ""; 133 File splashApp = new File(initialRoot, "Resources/Splash.app"); if (brandIcons) { 135 File icon = new File(icons[0]); 136 iconName = icon.getName(); 137 copy(icon, new File(target + "/Resources/" + icon.getName())); new File(initialRoot + "/Resources/Eclipse.icns").delete(); 139 if (!splashApp.exists()) 140 new File(initialRoot + "/Resources/").delete(); } 142 copyMacIni(initialRoot, target, iconName); 143 modifyInfoPListFile(initialRoot, target, iconName); 144 if (splashApp.exists()) { 145 brandMacSplash(initialRoot, target, iconName); 146 } 147 148 File rootFolder = new File(initialRoot); 149 rootFolder.delete(); 150 if (rootFolder.exists()) { 151 moveContents(rootFolder, new File(target)); 153 } 154 rootFolder.getParentFile().delete(); 155 } 156 157 158 165 private void brandMacSplash(String initialRoot, String target, String iconName) { 166 String splashContents = "/Resources/Splash.app/Contents"; modifyInfoPListFile(initialRoot + splashContents, target + splashContents, iconName); 168 169 int result = -1; 171 String osName = System.getProperty("os.name"); if (osName != null && !osName.startsWith("Windows")) { try { 174 String [] command = new String [] { "ln", "-sf", "../../../MacOS/" + name, "MacOS/" + name}; File linkDir = new File(target, splashContents); 176 Process proc = Runtime.getRuntime().exec( command, null, linkDir); 177 result = proc.waitFor(); 178 } catch (IOException e) { 179 } catch (InterruptedException e) { 181 } 183 } 184 185 if (result != 0) { 186 File macOSDir = new File(target, "MacOS"); File splashMacOSDir = new File(target, splashContents + "/MacOS"); splashMacOSDir.mkdirs(); 190 try { 191 File targetFile = new File(splashMacOSDir, name); 192 copy(new File(macOSDir, name), targetFile); 193 try { 194 Runtime.getRuntime().exec(new String [] {"chmod", "755", targetFile.getAbsolutePath()}); } catch (IOException e) { 196 } 198 } catch (IOException e) { 199 System.out.println("Could not copy macosx splash launcher"); } 201 } 202 } 203 204 private void moveContents(File source, File target) { 205 if (!source.exists()) 206 return; 207 208 try { 209 if (source.getCanonicalFile().equals(target.getCanonicalFile())) 210 return; 211 } catch (IOException e) { 212 System.out.println("Could not copy macosx resources."); return; 214 } 215 216 target.getParentFile().mkdirs(); 217 if (source.isDirectory()) { 218 target.mkdirs(); 219 File[] contents = source.listFiles(); 220 for (int i = 0; i < contents.length; i++) { 221 File dest = new File(target, contents[i].getName()); 222 if (contents[i].isFile()) 223 contents[i].renameTo(dest); 224 else 225 moveContents(contents[i], dest); 226 } 227 source.delete(); 228 } else { 229 source.renameTo(target); 230 } 231 } 232 233 private void brandWindows() throws Exception { 234 File templateLauncher = new File(root, "launcher.exe"); 235 if (!templateLauncher.exists()) 236 templateLauncher = new File(root, "eclipse.exe"); 237 if (brandIcons) { 238 String [] args = new String [icons.length + 1]; 239 args[0] = templateLauncher.getAbsolutePath(); 240 System.arraycopy(icons, 0, args, 1, icons.length); 241 IconExe.main(args); 242 } 243 templateLauncher.renameTo(new File(root, name + ".exe")); 244 } 245 246 private void renameLauncher() { 247 if (!new File(root, "launcher").renameTo(new File(root, name))) 248 new File(root, "eclipse").renameTo(new File(root, name)); 249 } 250 251 private void copyMacLauncher(String initialRoot, String target) { 252 String targetLauncher = target + "/MacOS/"; 253 File launcher = new File(initialRoot + "/MacOS/launcher"); 254 File eclipseLauncher = new File(initialRoot + "/MacOS/eclipse"); if (!launcher.exists()) { 256 launcher = eclipseLauncher; 257 } else if (eclipseLauncher.exists()) { 258 eclipseLauncher.delete(); 260 } 261 File targetFile = new File(targetLauncher, name); 262 try { 263 if (targetFile.getCanonicalFile().equals(launcher.getCanonicalFile())) { 264 try { 265 Runtime.getRuntime().exec(new String [] {"chmod", "755", targetFile.getAbsolutePath()}); } catch (IOException e) { 268 } 270 return; 271 } 272 copy(launcher, targetFile); 273 } catch (IOException e) { 274 System.out.println("Could not copy macosx launcher"); 275 return; 276 } 277 try { 278 Runtime.getRuntime().exec(new String [] {"chmod", "755", targetFile.getAbsolutePath()}); } catch (IOException e) { 281 } 283 launcher.delete(); 284 launcher.getParentFile().delete(); 285 } 286 287 private void copyMacIni(String initialRoot, String target, String iconName) { 288 File brandedIni = new File(initialRoot, "/MacOS/" + name + ".ini"); 290 File ini = new File(initialRoot, "/MacOS/eclipse.ini"); if (!ini.exists() && !brandedIni.exists()) 292 return; 293 294 if (brandedIni.exists() && ini.exists()) { 295 ini.delete(); 297 ini = brandedIni; 298 } 299 300 StringBuffer buffer; 301 try { 302 buffer = readFile(ini); 303 ini.delete(); 304 } catch (IOException e) { 305 System.out.println("Impossible to brand ini file"); return; 307 } 308 309 if(iconName.length() > 0){ 310 int xdoc = scan(buffer, 0, XDOC_ICON); 311 if (xdoc != -1) { 312 String icns = XDOC_ICON.replaceFirst("Eclipse.icns", iconName); buffer.replace(xdoc, xdoc + XDOC_ICON.length(), icns); 314 } 315 } 316 317 try { 318 File targetFile = new File(target, "/MacOS/" + name + ".ini"); transferStreams(new ByteArrayInputStream(buffer.toString().getBytes()), new FileOutputStream(targetFile)); 320 } catch (FileNotFoundException e) { 321 System.out.println("Impossible to brand ini file"); return; 323 } catch (IOException e) { 324 System.out.println("Impossible to brand ini file"); return; 326 } 327 } 328 329 private void modifyInfoPListFile(String initialRoot, String targetRoot, String iconName) { 330 File infoPList = new File(initialRoot, "Info.plist"); StringBuffer buffer; 332 try { 333 buffer = readFile(infoPList); 334 } catch (IOException e) { 335 System.out.println("Impossible to brand info.plist file"); return; 337 } 338 int exePos = scan(buffer, 0, MARKER_NAME); 339 if (exePos != -1) 340 buffer.replace(exePos, exePos + MARKER_NAME.length(), name); 341 else { 342 exePos = scan(buffer, 0, MARKER_KEY); 343 if (exePos != -1) { 344 int start = scan(buffer, exePos + MARKER_KEY.length(), STRING_START); 345 int end = scan(buffer, start + STRING_START.length(), STRING_END); 346 if (start > -1 && end > start) { 347 buffer.replace(start + STRING_START.length(), end, name); 348 } 349 } 350 } 351 352 int bundlePos = scan(buffer, 0, BUNDLE_NAME); 353 if (bundlePos != -1) 354 buffer.replace(bundlePos, bundlePos + BUNDLE_NAME.length(), name); 355 else { 356 exePos = scan(buffer, 0, BUNDLE_KEY); 357 if (exePos != -1) { 358 int start = scan(buffer, exePos + BUNDLE_KEY.length(), STRING_START); 359 int end = scan(buffer, start + STRING_START.length(), STRING_END); 360 if (start > -1 && end > start) { 361 buffer.replace(start + STRING_START.length(), end, name); 362 } 363 } 364 } 365 366 int iconPos = scan(buffer, 0, ICON_NAME); 367 if (iconPos != -1) 368 buffer.replace(iconPos, iconPos + ICON_NAME.length(), iconName); 369 else { 370 exePos = scan(buffer, 0, ICON_KEY); 371 if (exePos != -1) { 372 int start = scan(buffer, exePos + ICON_KEY.length(), STRING_START); 373 int end = scan(buffer, start + STRING_START.length(), STRING_END); 374 if (start > -1 && end > start) { 375 buffer.replace(start + STRING_START.length(), end, iconName); 376 } 377 } 378 } 379 380 File target = null; 381 try { 382 target = new File(targetRoot, "Info.plist"); 383 target.getParentFile().mkdirs(); 384 transferStreams(new ByteArrayInputStream(buffer.toString().getBytes()), new FileOutputStream(target)); 385 } catch (FileNotFoundException e) { 386 System.out.println("Impossible to brand info.plist file"); return; 388 } catch (IOException e) { 389 System.out.println("Impossible to brand info.plist file"); return; 391 } 392 try { 393 if (!infoPList.getCanonicalFile().equals(target.getCanonicalFile())) 394 infoPList.delete(); 395 } catch (IOException e) { 396 } 398 } 399 400 405 public void copy(File source, File destination) throws IOException { 406 InputStream in = null; 407 OutputStream out = null; 408 try { 409 in = new BufferedInputStream(new FileInputStream(source)); 410 out = new BufferedOutputStream(new FileOutputStream(destination)); 411 final byte[] buffer = new byte[8192]; 412 while (true) { 413 int bytesRead = -1; 414 bytesRead = in.read(buffer); 415 if (bytesRead == -1) 416 break; 417 out.write(buffer, 0, bytesRead); 418 } 419 } finally { 420 try { 421 if (in != null) 422 in.close(); 423 } finally { 424 if (out != null) 425 out.close(); 426 } 427 } 428 } 429 430 private int scan(StringBuffer buf, int start, String targetName) { 431 return scan(buf, start, new String [] {targetName}); 432 } 433 434 private int scan(StringBuffer buf, int start, String [] targets) { 435 for (int i = start; i < buf.length(); i++) { 436 for (int j = 0; j < targets.length; j++) { 437 if (i < buf.length() - targets[j].length()) { 438 String match = buf.substring(i, i + targets[j].length()); 439 if (targets[j].equalsIgnoreCase(match)) 440 return i; 441 } 442 } 443 } 444 return -1; 445 } 446 447 private StringBuffer readFile(File targetName) throws IOException { 448 InputStreamReader reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(targetName))); 449 StringBuffer result = new StringBuffer (); 450 char[] buf = new char[4096]; 451 int count; 452 try { 453 count = reader.read(buf, 0, buf.length); 454 while (count != -1) { 455 result.append(buf, 0, count); 456 count = reader.read(buf, 0, buf.length); 457 } 458 } finally { 459 try { 460 reader.close(); 461 } catch (IOException e) { 462 } 464 } 465 return result; 466 } 467 468 private void transferStreams(InputStream source, OutputStream destination) throws IOException { 469 source = new BufferedInputStream(source); 470 destination = new BufferedOutputStream(destination); 471 try { 472 byte[] buffer = new byte[8192]; 473 while (true) { 474 int bytesRead = -1; 475 if ((bytesRead = source.read(buffer)) == -1) 476 break; 477 destination.write(buffer, 0, bytesRead); 478 } 479 } finally { 480 try { 481 source.close(); 482 } catch (IOException e) { 483 } 485 try { 486 destination.close(); 487 } catch (IOException e) { 488 } 490 } 491 } 492 493 public void setOS(String value) { 494 os = value; 495 } 496 } 497 | Popular Tags |