1 20 21 package net.sourceforge.jarbundler; 22 23 import net.sourceforge.jarbundler.AppBundleProperties; 25 import net.sourceforge.jarbundler.DocumentType; 26 import net.sourceforge.jarbundler.JavaProperty; 27 import net.sourceforge.jarbundler.PropertyListWriter; 28 29 import java.io.BufferedWriter ; 31 import java.io.BufferedReader ; 32 import java.io.File ; 33 import java.io.FileWriter ; 34 import java.io.FileReader ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.PrintWriter ; 38 39 import java.util.ArrayList ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Set ; 45 46 import org.apache.tools.ant.BuildException; 48 import org.apache.tools.ant.FileScanner; 49 import org.apache.tools.ant.Project; 50 import org.apache.tools.ant.Task; 51 52 import org.apache.tools.ant.types.FileList; 53 import org.apache.tools.ant.types.FileSet; 54 import org.apache.tools.ant.types.PatternSet; 55 56 import org.apache.tools.ant.taskdefs.MatchingTask; 57 import org.apache.tools.ant.taskdefs.Chmod; 58 import org.apache.tools.ant.taskdefs.Delete; 59 60 import org.apache.tools.ant.util.FileUtils; 61 62 63 import java.lang.Boolean ; 65 import java.lang.Process ; 66 import java.lang.Runtime ; 67 import java.lang.String ; 68 import java.lang.System ; 69 70 256 public class JarBundler extends MatchingTask { 257 258 private static final String DEFAULT_STUB = "/System/Library/Frameworks/JavaVM.framework/Versions/Current/Resources/MacOS/JavaApplicationStub"; 259 260 private static final String ABOUTMENU_KEY = "com.apple.mrj.application.apple.menu.about.name"; 261 private static final Set menuItems = new HashSet (); 262 private File mAppIcon; 263 264 private File mRootDir; 265 266 private final List mJavaFileLists = new ArrayList (); 267 private final List mJarFileSets = new ArrayList (); 268 269 private final List mExecFileLists = new ArrayList (); 270 private final List mExecFileSets = new ArrayList (); 271 272 private final List mResourceFileLists = new ArrayList (); 273 private final List mResourceFileSets = new ArrayList (); 274 275 private final List mJarFileLists = new ArrayList (); 276 private final List mJavaFileSets = new ArrayList (); 277 278 private final List mExtraClassPathFileLists = new ArrayList (); 279 private final List mExtraClassPathFileSets = new ArrayList (); 280 281 private final List mJarAttrs = new ArrayList (); 282 283 private final List mExecAttrs = new ArrayList (); 284 285 private final List mExtraClassPathAttrs = new ArrayList (); 286 287 private final List mHelpBooks = new ArrayList (); 288 289 private boolean mVerbose = false; 290 private boolean mShowPlist = false; 291 292 294 private File mStubFile = new File (DEFAULT_STUB); 295 296 private Boolean mAntiAliasedGraphics = null; 297 298 private Boolean mAntiAliasedText = null; 299 300 private Boolean mLiveResize = null; 301 302 private Boolean mScreenMenuBar = null; 303 304 private Boolean mGrowbox = null; 305 306 private Boolean mGrowboxIntrudes = null; 307 308 private File bundleDir; 310 311 private File mContentsDir; 313 314 private File mMacOsDir; 316 317 private File mResourcesDir; 319 320 private File mJavaDir; 322 323 327 328 private AppBundleProperties bundleProperties = new AppBundleProperties(); 329 330 332 private FileUtils mFileUtils = FileUtils.getFileUtils(); 333 334 337 338 344 public void setArguments(String s) { 345 bundleProperties.setArguments(s); 346 } 347 348 354 public void setStubFile(File file) { 355 mStubFile = (file.exists()) ? file : new File (DEFAULT_STUB); 356 bundleProperties.setCFBundleExecutable(file.getName()); 357 } 358 359 362 public void setDir(File f) { 363 mRootDir = f; 364 } 365 366 371 public void setName(String s) { 372 bundleProperties.setApplicationName(s); 373 } 374 375 381 public void setShortName(String s) { 382 bundleProperties.setCFBundleName(s); 383 } 384 385 388 public void setMainClass(String s) { 389 bundleProperties.setMainClass(s); 390 } 391 392 395 public void setWorkingDirectory(String s) { 396 bundleProperties.setWorkingDirectory(s); 397 } 398 399 402 403 public void setIcon(File f) { 404 mAppIcon = f; 405 bundleProperties.setCFBundleIconFile(f.getName()); 406 } 407 408 417 public void setBundleid(String s) { 418 bundleProperties.setCFBundleIdentifier(s); 419 } 420 421 424 public void setDevelopmentregion(String s) { 425 bundleProperties.setCFBundleDevelopmentRegion(s); 426 } 427 428 431 public void setAboutmenuname(String s) { 432 bundleProperties.setCFBundleName(s); 433 } 434 435 438 public void setSmallTabs(boolean b) { 439 bundleProperties.addJavaProperty("com.apple.smallTabs", new Boolean (b) 440 .toString()); 441 } 442 443 446 public void setVmoptions(String s) { 447 bundleProperties.setVMOptions(s); 448 } 449 450 453 public void setAntialiasedgraphics(boolean b) { 454 mAntiAliasedGraphics = new Boolean (b); 455 } 456 457 460 public void setAntialiasedtext(boolean b) { 461 mAntiAliasedText = new Boolean (b); 462 } 463 464 467 public void setScreenmenu(boolean b) { 468 mScreenMenuBar = new Boolean (b); 469 } 470 471 474 public void setGrowbox(boolean b) { 475 mGrowbox = new Boolean (b); 476 } 477 478 481 public void setGrowboxintrudes(boolean b) { 482 mGrowboxIntrudes = new Boolean (b); 483 } 484 485 488 public void setLiveresize(boolean b) { 489 mLiveResize = new Boolean (b); 490 } 491 492 495 public void setType(String s) { 496 bundleProperties.setCFBundlePackageType(s); 497 } 498 499 502 public void setSignature(String s) { 503 bundleProperties.setCFBundleSignature(s); 504 } 505 506 509 public void setJvmversion(String s) { 510 bundleProperties.setJVMVersion(s); 511 } 512 513 522 523 public void setInfoString(String s) { 524 bundleProperties.setCFBundleGetInfoString(s); 525 } 526 527 542 public void setShortInfoString(String s) { 543 setVersion(s); 544 } 545 546 549 public void setVerbose(boolean verbose) { 550 this.mVerbose = verbose; 551 } 552 public void setShowPlist(boolean showPlist) { 553 this.mShowPlist = showPlist; 554 } 555 556 557 558 559 578 public void setBuild(String s) { 579 bundleProperties.setCFBundleVersion(s); 580 } 581 582 588 public void setVersion(String s) { 589 bundleProperties.setCFBundleShortVersionString(s); 590 } 591 592 public void setHelpBookFolder(String s) { 593 bundleProperties.setCFBundleHelpBookFolder(s); 594 } 595 596 public void setHelpBookName(String s) { 597 bundleProperties.setCFBundleHelpBookName(s); 598 } 599 600 603 public void setJars(String s) { 604 PatternSet patset = new PatternSet(); 605 patset.setIncludes(s); 606 607 String [] jarNames = patset.getIncludePatterns(getProject()); 608 609 for (int i = 0; i < jarNames.length; i++) 610 mJarAttrs.add(getProject().resolveFile(jarNames[i])); 611 } 612 613 616 public void setJar(File s) { 617 mJarAttrs.add(s); 618 } 619 620 623 public void setExecs(String s) { 624 PatternSet patset = new PatternSet(); 625 patset.setIncludes(s); 626 627 String [] execNames = patset.getIncludePatterns(getProject()); 628 629 for (int i = 0; i < execNames.length; i++) { 630 File f = new File (execNames[i]); 631 mExecAttrs.add(f); 632 } 633 } 634 635 638 public void setExtraclasspath(String s) { 639 PatternSet patset = new PatternSet(); 640 patset.setIncludes(s); 641 642 String [] cpNames = patset.getIncludePatterns(getProject()); 643 644 for (int i = 0; i < cpNames.length; i++) { 645 File f = new File (cpNames[i]); 646 mExtraClassPathAttrs.add(f); 647 } 648 } 649 650 653 public void setChmod(String s) { 654 log("The \"chmod\" attribute has deprecaited, using the ANT Chmod task internally"); 655 } 656 657 660 661 public void addJarfileset(FileSet fs) { 662 mJarFileSets.add(fs); 663 } 664 665 public void addJarfilelist(FileList fl) { 666 mJarFileLists.add(fl); 667 } 668 669 public void addExecfileset(FileSet fs) { 670 mExecFileSets.add(fs); 671 } 672 673 public void addExecfilelist(FileList fl) { 674 mExecFileLists.add(fl); 675 } 676 677 public void addResourcefileset(FileSet fs) { 678 mResourceFileSets.add(fs); 679 } 680 681 public void addResourcefilelist(FileList fl) { 682 mResourceFileLists.add(fl); 683 } 684 685 public void addJavafileset(FileSet fs) { 686 mJavaFileSets.add(fs); 687 } 688 689 public void addJavafilelist(FileList fl) { 690 mJavaFileLists.add(fl); 691 } 692 693 public void addExtraclasspathfileset(FileSet fs) { 694 mExtraClassPathFileSets.add(fs); 695 } 696 697 public void addExtraclasspathfilelist(FileList fl) { 698 mExtraClassPathFileLists.add(fl); 699 } 700 701 702 705 706 707 public void addConfiguredJavaProperty(JavaProperty javaProperty) 708 throws BuildException { 709 710 String name = javaProperty.getName(); 711 String value = javaProperty.getValue(); 712 713 if ((name == null) || (value == null)) 714 throw new BuildException( 715 "'<javaproperty>' must have both 'name' and 'value' attibutes"); 716 717 bundleProperties.addJavaProperty(name, value); 718 } 719 720 public void addConfiguredDocumentType(DocumentType documentType) throws BuildException { 721 722 String name = documentType.getName(); 723 String role = documentType.getRole(); 724 List osTypes = documentType.getOSTypes(); 725 List extensions = documentType.getExtensions(); 726 List mimeTypes = documentType.getMimeTypes(); 727 728 if ((name == null) || (role == null)) 729 throw new BuildException( 730 "'<documenttype>' must have both a 'name' and a 'role' attibute"); 731 732 if ((osTypes.isEmpty()) && (extensions.isEmpty()) && (mimeTypes.isEmpty())) 733 throw new BuildException( 734 "'<documenttype>' of \"" 735 + name 736 + "\" must have 'osTypes' or 'extensions' or 'mimeTypes'"); 737 738 bundleProperties.addDocumentType(documentType); 739 } 740 741 public void addConfiguredService(Service service) { 742 743 746 if (service.getMessage() == null) 747 throw new BuildException("\"<service>\" must have a \"message\" attribute"); 748 749 String menuItem = service.getMenuItem(); 750 if (menuItem == null) 751 throw new BuildException("\"<service>\" must have a \"menuItem\" attribute"); 752 if (!menuItems.add(menuItem)) 753 throw new BuildException("\"<service>\" \"menuItem\" value must be unique"); 754 755 if (service.getSendTypes().isEmpty() && service.getReturnTypes().isEmpty()) 756 throw new BuildException("\"<service>\" must have either a \"sendTypes\" attribute, a \"returnTypes\" attribute or both"); 757 758 String keyEquivalent = service.getKeyEquivalent(); 759 if ((keyEquivalent != null) && (1 != keyEquivalent.length())) 760 throw new BuildException("\"<service>\" \"keyEquivalent\" must be one character if present"); 761 762 String timeoutString = service.getTimeout(); 763 if (timeoutString != null) { 764 long timeout = -1; 765 try { 766 timeout = Long.parseLong(timeoutString); 767 } catch (NumberFormatException nfe) { 768 throw new BuildException("\"<service>\" \"timeout\" must be a positive integral number"); 769 } 770 if (timeout < 0) 771 throw new BuildException("\"<service>\" \"timeout\" must not be negative"); 772 } 773 774 bundleProperties.addService(service); 775 } 776 777 public void addConfiguredHelpBook(HelpBook helpBook) { 778 779 if (helpBook.getFolderName() == null) { 781 if (bundleProperties.getCFBundleHelpBookFolder() == null) 782 throw new BuildException("Either the '<helpbook>' attribute 'foldername' or the '<jarbundler>' attribute 'helpbookfolder' must be defined"); 783 helpBook.setFolderName(bundleProperties.getCFBundleHelpBookFolder()); 784 } 785 786 if (helpBook.getName() == null) { 788 if (bundleProperties.getCFBundleHelpBookName() == null) 789 throw new BuildException("Either the '<helpbook>' attribute 'name' or the '<jarbundler>' attribute 'helpbookname' must be defined"); 790 helpBook.setName(bundleProperties.getCFBundleHelpBookName()); 791 } 792 793 List fileLists = helpBook.getFileLists(); 795 List fileSets = helpBook.getFileSets(); 796 797 if ( fileLists.isEmpty() && fileSets.isEmpty() ) 798 throw new BuildException("The '<helpbook>' task must have either " + 799 "'<fileset>' or '<filelist>' nested tags"); 800 801 802 mHelpBooks.add(helpBook); 803 } 804 805 806 807 810 811 814 815 public void execute() throws BuildException { 816 817 819 bundleDir = new File (mRootDir, bundleProperties.getApplicationName() + ".app"); 820 821 if (bundleDir.exists()) { 822 Delete deleteTask = new Delete(); 823 deleteTask.setProject(getProject()); 824 deleteTask.setDir(bundleDir); 825 deleteTask.execute(); 826 } 827 828 831 if (mRootDir == null) 832 throw new BuildException("Required attribute \"dir\" is not set."); 833 834 if (mJarAttrs.isEmpty() && mJarFileSets.isEmpty() 835 && mJarFileLists.isEmpty()) 836 throw new BuildException("Either the attribute \"jar\" must " 837 + "be set, or one or more jarfilelists or " 838 + "jarfilesets must be added."); 839 840 if (!mJarAttrs.isEmpty() 841 && (!mJarFileSets.isEmpty() || !mJarFileLists.isEmpty())) 842 throw new BuildException( 843 "Cannot set both the attribute " 844 + "\"jars\" and use jar filesets/filelists. Use only one or the other."); 845 846 if (bundleProperties.getApplicationName() == null) 847 throw new BuildException("Required attribute \"name\" is not set."); 848 849 if (bundleProperties.getMainClass() == null) 850 throw new BuildException( 851 "Required attribute \"mainclass\" is not set."); 852 853 855 857 if (useOldPropertyNames()) 859 bundleProperties.addJavaProperty(ABOUTMENU_KEY, bundleProperties 860 .getCFBundleName()); 861 862 String antiAliasedProperty = useOldPropertyNames() 864 ? "com.apple.macosx.AntiAliasedGraphicsOn" 865 : "apple.awt.antialiasing"; 866 867 if (mAntiAliasedGraphics != null) 868 bundleProperties.addJavaProperty(antiAliasedProperty, 869 mAntiAliasedGraphics.toString()); 870 871 String antiAliasedTextProperty = useOldPropertyNames() 873 ? "com.apple.macosx.AntiAliasedTextOn" 874 : "apple.awt.textantialiasing"; 875 876 if (mAntiAliasedText != null) 877 bundleProperties.addJavaProperty(antiAliasedTextProperty, 878 mAntiAliasedText.toString()); 879 880 if (useOldPropertyNames() && (mLiveResize != null)) 882 bundleProperties.addJavaProperty( 883 "com.apple.mrj.application.live-resize", mLiveResize 884 .toString()); 885 886 String screenMenuBarProperty = useOldPropertyNames() 888 ? "com.apple.macos.useScreenMenuBar" 889 : "apple.laf.useScreenMenuBar"; 890 891 if (mScreenMenuBar != null) 892 bundleProperties.addJavaProperty(screenMenuBarProperty, 893 mScreenMenuBar.toString()); 894 895 if ((useOldPropertyNames() == false) && (mGrowbox != null)) 897 bundleProperties.addJavaProperty("apple.awt.showGrowBox", mGrowbox 898 .toString()); 899 900 if (useOldPropertyNames() && (mGrowboxIntrudes != null)) 902 bundleProperties.addJavaProperty( 903 "com.apple.mrj.application.growbox.intrudes", 904 mGrowboxIntrudes.toString()); 905 906 if (!mRootDir.exists() 907 || (mRootDir.exists() && !mRootDir.isDirectory())) 908 throw new BuildException( 909 "Destination directory specified by \"dir\" " 910 + "attribute must already exist."); 911 912 if (bundleDir.exists()) 913 throw new BuildException("The directory/bundle \"" 914 + bundleDir.getName() 915 + "\" already exists, cannot continue."); 916 917 log("Creating application bundle: " + bundleDir); 919 920 if (!bundleDir.mkdir()) 921 throw new BuildException("Unable to create bundle: " + bundleDir); 922 923 mContentsDir = new File (bundleDir, "Contents"); 925 926 if (!mContentsDir.mkdir()) 927 throw new BuildException("Unable to create directory " 928 + mContentsDir); 929 930 mMacOsDir = new File (mContentsDir, "MacOS"); 932 933 if (!mMacOsDir.mkdir()) 934 throw new BuildException("Unable to create directory " + mMacOsDir); 935 936 mResourcesDir = new File (mContentsDir, "Resources"); 938 939 if (!mResourcesDir.mkdir()) 940 throw new BuildException("Unable to create directory " 941 + mResourcesDir); 942 943 mJavaDir = new File (mResourcesDir, "Java"); 945 946 if (!mJavaDir.mkdir()) 947 throw new BuildException("Unable to create directory " + mJavaDir); 948 949 952 if (mAppIcon != null) { 953 954 955 try { 956 File dest = new File (mResourcesDir, mAppIcon.getName()); 957 958 if(mVerbose) 959 log("Copying application icon file to \"" + bundlePath(dest) + "\""); 960 961 mFileUtils.copyFile(mAppIcon, dest); 962 } catch (IOException ex) { 963 throw new BuildException("Cannot copy icon file: " + ex); 964 } 965 } 966 967 try { 969 Iterator itor = bundleProperties.getDocumentTypes().iterator(); 970 971 while (itor.hasNext()) { 972 DocumentType documentType = (DocumentType) itor.next(); 973 File iconFile = documentType.getIconFile(); 974 if (iconFile != null) { 975 File dest = new File (mResourcesDir, iconFile.getName()); 976 if(mVerbose) 977 log("Copying document icon file to \"" + bundlePath(dest) + "\""); 978 mFileUtils.copyFile(iconFile, dest); 979 } 980 } 981 } catch (IOException ex) { 982 throw new BuildException("Cannot copy document icon file: " + ex); 983 } 984 985 processJarAttrs(); 987 988 processJarFileSets(); 990 991 processJarFileLists(); 993 994 processExecAttrs(); 996 997 processExecFileSets(); 999 1000 processExecFileLists(); 1002 1003 processResourceFileSets(); 1005 1006 processJavaFileSets(); 1008 1009 processResourceFileLists(); 1011 1012 processJavaFileLists(); 1014 1015 processExtraClassPathAttrs(); 1017 1018 processExtraClassPathFileSets(); 1021 1022 processExtraClassPathFileLists(); 1025 1026 copyHelpBooks(); 1028 1029 copyApplicationStub(); 1032 1033 writeInfoPlist(); 1035 1036 writePkgInfo(); 1038 1039 } 1041 1042 1045 1046 private void setExecutable(File f) { 1047 1048 Chmod chmodTask = new Chmod(); 1049 chmodTask.setProject(getProject()); 1050 chmodTask.setFile(f); 1051 chmodTask.setPerm("ugo+rx"); 1052 1053 if (mVerbose) 1054 log("Setting \"" + bundlePath(f) + "\" to executable"); 1055 1056 chmodTask.execute(); 1057 1058 } 1059 1060 1070 1071 private boolean useOldPropertyNames() { 1072 return (bundleProperties.getJVMVersion().startsWith("1.3")); 1073 } 1074 1075 private void processJarAttrs() throws BuildException { 1076 1077 try { 1078 1079 for (Iterator jarIter = mJarAttrs.iterator(); jarIter.hasNext();) { 1080 File src = (File ) jarIter.next(); 1081 File dest = new File (mJavaDir, src.getName()); 1082 1083 if (mVerbose) 1084 log("Copying JAR file to \"" + bundlePath(dest) + "\""); 1085 1086 1087 mFileUtils.copyFile(src, dest); 1088 bundleProperties.addToClassPath(dest.getName()); 1089 } 1090 } catch (IOException ex) { 1091 throw new BuildException("Cannot copy jar file: " + ex); 1092 } 1093 } 1094 1095 private void processJarFileSets() throws BuildException { 1096 1097 for (Iterator jarIter = mJarFileSets.iterator(); jarIter.hasNext();) { 1098 1099 FileSet fs = (FileSet) jarIter.next(); 1100 1101 Project p = fs.getProject(); 1102 File srcDir = fs.getDir(p); 1103 FileScanner ds = fs.getDirectoryScanner(p); 1104 fs.setupDirectoryScanner(ds, p); 1105 ds.scan(); 1106 1107 String [] files = ds.getIncludedFiles(); 1108 1109 try { 1110 1111 for (int i = 0; i < files.length; i++) { 1112 String fileName = files[i]; 1113 File src = new File (srcDir, fileName); 1114 File dest = new File (mJavaDir, fileName); 1115 1116 if (mVerbose) 1117 log("Copying JAR file to \"" + bundlePath(dest) + "\""); 1118 1119 mFileUtils.copyFile(src, dest); 1120 bundleProperties.addToClassPath(fileName); 1121 } 1122 1123 } catch (IOException ex) { 1124 throw new BuildException("Cannot copy jar file: " + ex); 1125 } 1126 } 1127 } 1128 1129 private void processJarFileLists() throws BuildException { 1130 1131 for (Iterator jarIter = mJarFileLists.iterator(); jarIter.hasNext();) { 1132 FileList fl = (FileList) jarIter.next(); 1133 Project p = fl.getProject(); 1134 File srcDir = fl.getDir(p); 1135 String [] files = fl.getFiles(p); 1136 1137 try { 1138 1139 for (int i = 0; i < files.length; i++) { 1140 String fileName = files[i]; 1141 File src = new File (srcDir, fileName); 1142 File dest = new File (mJavaDir, fileName); 1143 1144 if (mVerbose) 1145 log("Copying JAR file to \"" + bundlePath(dest) + "\""); 1146 1147 1148 mFileUtils.copyFile(src, dest); 1149 bundleProperties.addToClassPath(fileName); 1150 } 1151 } catch (IOException ex) { 1152 throw new BuildException("Cannot copy jar file: " + ex); 1153 } 1154 } 1155 } 1156 1157 private void processExtraClassPathAttrs() throws BuildException { 1158 1159 for (Iterator jarIter = mExtraClassPathAttrs.iterator(); jarIter 1160 .hasNext();) { 1161 File src = (File ) jarIter.next(); 1162 bundleProperties.addToExtraClassPath(src.getPath()); 1163 } 1164 } 1165 1166 private void processExtraClassPathFileSets() throws BuildException { 1167 1168 for (Iterator jarIter = mExtraClassPathFileSets.iterator(); jarIter 1169 .hasNext();) { 1170 FileSet fs = (FileSet) jarIter.next(); 1171 Project p = fs.getProject(); 1172 File srcDir = fs.getDir(p); 1173 FileScanner ds = fs.getDirectoryScanner(p); 1174 fs.setupDirectoryScanner(ds, p); 1175 ds.scan(); 1176 1177 String [] files = ds.getIncludedFiles(); 1178 1179 for (int i = 0; i < files.length; i++) { 1180 File f = new File (srcDir, files[i]); 1181 bundleProperties.addToExtraClassPath(f.getPath()); 1182 } 1183 } 1184 } 1185 1186 private void processExtraClassPathFileLists() throws BuildException { 1187 1188 for (Iterator jarIter = mExtraClassPathFileLists.iterator(); jarIter 1189 .hasNext();) { 1190 FileList fl = (FileList) jarIter.next(); 1191 Project p = fl.getProject(); 1192 File srcDir = fl.getDir(p); 1193 String [] files = fl.getFiles(p); 1194 1195 for (int i = 0; i < files.length; i++) { 1196 File f = new File (srcDir, files[i]); 1197 bundleProperties.addToExtraClassPath(f.getPath()); 1198 } 1199 } 1200 } 1201 1202 private void processExecAttrs() throws BuildException { 1203 1204 try { 1205 1206 for (Iterator execIter = mExecAttrs.iterator(); execIter.hasNext();) { 1207 File src = (File ) execIter.next(); 1208 File dest = new File (mMacOsDir, src.getName()); 1209 1210 if (mVerbose) 1211 log("Copying exec file to \"" + bundlePath(dest) + "\""); 1212 1213 1214 mFileUtils.copyFile(src, dest); 1215 setExecutable(dest); 1216 } 1217 } catch (IOException ex) { 1218 throw new BuildException("Cannot copy exec file: " + ex); 1219 } 1220 } 1221 1222 1224 private void processExecFileSets() { 1226 processCopyingFileSets(mExecFileSets, mMacOsDir, true); 1227 } 1228 1229 private void processResourceFileSets() { 1231 processCopyingFileSets(mResourceFileSets, mResourcesDir, false); 1232 } 1233 1234 private void processJavaFileSets() { 1236 processCopyingFileSets(mJavaFileSets, mJavaDir, false); 1237 } 1238 1239 private void processCopyingFileSets(List fileSets, File targetdir, boolean setExec) { 1240 1241 for (Iterator execIter = fileSets.iterator(); execIter.hasNext();) { 1242 FileSet fs = (FileSet) execIter.next(); 1243 Project p = fs.getProject(); 1244 File srcDir = fs.getDir(p); 1245 FileScanner ds = fs.getDirectoryScanner(p); 1246 fs.setupDirectoryScanner(ds, p); 1247 ds.scan(); 1248 1249 String [] files = ds.getIncludedFiles(); 1250 1251 if (files.length == 0) { 1252 System.err 1254 .println("WARNING: fileset for copying from directory " 1255 + srcDir + ": no files found"); 1256 } else { 1257 try { 1258 for (int i = 0; i < files.length; i++) { 1259 String fileName = files[i]; 1260 File src = new File (srcDir, fileName); 1261 File dest = new File (targetdir, fileName); 1262 1263 if (mVerbose) 1264 log("Copying " 1265 + (setExec ? "exec" : "resource") 1266 + " file to \"" + bundlePath(dest) +"\""); 1267 1268 mFileUtils.copyFile(src, dest); 1269 if (setExec) 1270 setExecutable(dest); 1271 } 1272 } catch (IOException ex) { 1273 throw new BuildException("Cannot copy file: " + ex); 1274 } 1275 } 1276 } 1277 } 1278 1279 1281 private void processExecFileLists() throws BuildException { 1283 processCopyingFileLists(mExecFileLists, mMacOsDir, true); 1284 } 1285 1286 private void processResourceFileLists() throws BuildException { 1288 processCopyingFileLists(mResourceFileLists, mResourcesDir, false); 1289 } 1290 1291 private void processJavaFileLists() throws BuildException { 1293 processCopyingFileLists(mJavaFileLists, mJavaDir, false); 1294 } 1295 1296 private void processCopyingFileLists(List fileLists, File targetDir, boolean setExec) throws BuildException { 1297 1298 for (Iterator execIter = fileLists.iterator(); execIter.hasNext();) { 1299 1300 FileList fl = (FileList) execIter.next(); 1301 Project p = fl.getProject(); 1302 File srcDir = fl.getDir(p); 1303 String [] files = fl.getFiles(p); 1304 1305 if (files.length == 0) { 1306 System.err.println("WARNING: filelist for copying from directory " 1308 + srcDir + ": no files found"); 1309 } else { 1310 try { 1311 for (int i = 0; i < files.length; i++) { 1312 String fileName = files[i]; 1313 File src = new File (srcDir, fileName); 1314 File dest = new File (targetDir, fileName); 1315 1316 if (mVerbose) 1317 log("Copying " 1318 + (setExec ? "exec" : "resource") 1319 + " file to \"" + bundlePath(dest) +"\""); 1320 1321 mFileUtils.copyFile(src, dest); 1322 if (setExec) 1323 setExecutable(dest); 1324 } 1325 } catch (IOException ex) { 1326 throw new BuildException("Cannot copy jar file: " + ex); 1327 } 1328 } 1329 } 1330 } 1331 1332 1333 1334 private void copyHelpBooks() { 1335 1336 for (Iterator itor = mHelpBooks.iterator(); itor.hasNext();) { 1337 1338 HelpBook helpBook = (HelpBook)itor.next(); 1339 1340 String folderName = helpBook.getFolderName(); 1341 String name = helpBook.getName(); 1342 String locale = helpBook.getLocale(); 1343 1344 List fileLists = helpBook.getFileLists(); 1345 List fileSets = helpBook.getFileSets(); 1346 1347 1348 File helpBookDir = null; 1349 1350 if (locale == null) { 1351 1352 if (folderName != null) 1354 bundleProperties.setCFBundleHelpBookFolder(folderName); 1355 1356 if (name != null) 1357 bundleProperties.setCFBundleHelpBookName(name); 1358 1359 helpBookDir = new File (mResourcesDir, folderName); 1361 helpBookDir.mkdir(); 1362 1363 if(mVerbose) 1364 log("Creating Help Book at \"" + 1365 bundlePath(helpBookDir) + "\""); 1366 1367 1368 } else { 1369 1370 1372 File lproj = new File (mResourcesDir, locale + ".lproj"); 1373 lproj.mkdir(); 1374 helpBookDir = new File (lproj, folderName); 1375 helpBookDir.mkdir(); 1376 1377 if(mVerbose) 1378 log("Creating Help Book for \"" + locale + 1379 "\" at \"" + bundlePath(helpBookDir) + "\""); 1380 1381 File infoPList = new File (lproj, "InfoPlist.strings"); 1383 PrintWriter writer = null; 1384 try { 1385 writer = new PrintWriter (new FileWriter (infoPList)); 1386 writer.println("CFBundleHelpBookFolder = \"" + folderName + "\";"); 1387 writer.println("CFBundleHelpBookName = \"" + name + "\";"); 1388 writer.println("CFBundleName = \"" + bundleProperties.getCFBundleName() + "\";"); 1389 } catch (IOException ioe) { 1390 throw new BuildException("IOException in writing Help Book locale: " + locale); 1391 } finally { 1392 mFileUtils.close(writer); 1393 } 1394 } 1395 1396 1398 processCopyingFileSets(fileSets, helpBookDir, false); 1399 processCopyingFileLists(fileLists, helpBookDir, false); 1400 1401 } 1402 } 1403 1404 1405 1406 1407 1410 private void copyApplicationStub() throws BuildException { 1411 1412 File newStubFile = new File (mMacOsDir, bundleProperties.getCFBundleExecutable()); 1413 1414 if (mVerbose) 1415 log("Copying Java application stub to \"" + bundlePath(newStubFile) + "\""); 1416 1417 try { 1418 mFileUtils.copyFile(mStubFile, newStubFile); 1419 } catch (IOException ex) { 1420 throw new BuildException("Cannot copy Java Application Stub: " + ex); 1421 } 1422 1423 1425 setExecutable(newStubFile); 1426 } 1427 1428 private void writeInfoPlist() throws BuildException { 1429 PropertyListWriter listWriter = new PropertyListWriter(bundleProperties); 1430 File infoPlist = new File (mContentsDir, "Info.plist"); 1431 1432 listWriter.writeFile(infoPlist); 1433 1434 if (mVerbose) 1435 log("Creating \"" + bundlePath(infoPlist) + "\" file"); 1436 1437 1438 if (mShowPlist) { 1439 try { 1440 BufferedReader in = new BufferedReader (new FileReader (infoPlist)); 1441 String str; 1442 while ((str = in.readLine()) != null) 1443 log(str); 1444 in.close(); 1445 } catch (IOException e) { 1446 throw new BuildException(e); 1447 } 1448 } 1449 } 1450 1451 1452 1456 private void writePkgInfo() throws BuildException { 1457 File pkgInfo = new File (mContentsDir, "PkgInfo"); 1458 PrintWriter writer = null; 1459 1460 try { 1461 writer = new PrintWriter (new BufferedWriter (new FileWriter (pkgInfo))); 1462 writer.print(bundleProperties.getCFBundlePackageType()); 1463 writer.println(bundleProperties.getCFBundleSignature()); 1464 writer.flush(); 1465 } catch (IOException ex) { 1466 throw new BuildException("Cannot create PkgInfo file: " + ex); 1467 } finally { 1468 mFileUtils.close(writer); 1469 } 1470 } 1471 1472 private String bundlePath(File bundleFile) { 1473 1474 String rootPath = bundleDir.getAbsolutePath(); 1475 String thisPath = bundleFile.getAbsolutePath(); 1476 1477 return thisPath.substring(rootPath.length()); 1478 1479 } 1480} 1481 | Popular Tags |