|
41 | 41 | import java.nio.file.Path;
|
42 | 42 | import java.nio.file.StandardCopyOption;
|
43 | 43 | import java.util.ArrayList;
|
| 44 | +import java.util.List; |
44 | 45 | import java.util.zip.ZipEntry;
|
45 | 46 | import java.util.zip.ZipInputStream;
|
46 | 47 |
|
@@ -79,6 +80,7 @@ public class AppBundlerTask extends Task {
|
79 | 80 | private ArrayList<FileSet> libraryPath = new ArrayList<>();
|
80 | 81 | private ArrayList<String> options = new ArrayList<>();
|
81 | 82 | private ArrayList<String> arguments = new ArrayList<>();
|
| 83 | + private List<DocumentType> documentTypes = new ArrayList<>(); |
82 | 84 |
|
83 | 85 | private static final String EXECUTABLE_NAME = "JavaAppLauncher";
|
84 | 86 | private static final String DEFAULT_ICON_NAME = "GenericApp.icns";
|
@@ -186,6 +188,10 @@ public void addConfiguredArgument(Argument argument) throws BuildException {
|
186 | 188 | arguments.add(value);
|
187 | 189 | }
|
188 | 190 |
|
| 191 | + public void addDocumentType(DocumentType documentType) { |
| 192 | + documentTypes.add(documentType); |
| 193 | + } |
| 194 | + |
189 | 195 | @Override
|
190 | 196 | public void execute() throws BuildException {
|
191 | 197 | // Validate required properties
|
@@ -243,6 +249,15 @@ public void execute() throws BuildException {
|
243 | 249 | throw new IllegalStateException("Main class name is required.");
|
244 | 250 | }
|
245 | 251 |
|
| 252 | + for (DocumentType documentType : documentTypes) { |
| 253 | + if(documentType.getName() == null) { |
| 254 | + throw new IllegalStateException("Name is required for document type."); |
| 255 | + } |
| 256 | + if (!documentType.getIcon().exists()) { |
| 257 | + throw new IllegalStateException("Document icon does not exist."); |
| 258 | + } |
| 259 | + } |
| 260 | + |
246 | 261 | // Create the app bundle
|
247 | 262 | try {
|
248 | 263 | System.out.println("Creating app bundle: " + name);
|
@@ -297,6 +312,12 @@ public void execute() throws BuildException {
|
297 | 312 |
|
298 | 313 | // Copy icon to Resources folder
|
299 | 314 | copyIcon(resourcesDirectory);
|
| 315 | + |
| 316 | + for(DocumentType documentType : documentTypes) { |
| 317 | + if (documentType.getIcon() != null) { |
| 318 | + copy(documentType.getIcon(), new File(resourcesDirectory, documentType.getIcon().getName())); |
| 319 | + } |
| 320 | + } |
300 | 321 | } catch (IOException exception) {
|
301 | 322 | throw new BuildException(exception);
|
302 | 323 | }
|
@@ -451,6 +472,25 @@ private void writeInfoPlist(File file) throws IOException {
|
451 | 472 | writeProperty(xout, "LSApplicationCategoryType", applicationCategory);
|
452 | 473 | }
|
453 | 474 |
|
| 475 | + if ( ! documentTypes.isEmpty()) { |
| 476 | + writeKey(xout, "CFBundleDocumentTypes"); |
| 477 | + xout.writeStartElement(ARRAY_TAG); |
| 478 | + xout.writeCharacters("\n"); |
| 479 | + for (DocumentType documentType : documentTypes) { |
| 480 | + writeProperty(xout, "CFBundleTypeName", documentType.getName()); |
| 481 | + writeProperty(xout, "CFBundleTypeRole", documentType.getRole().name()); |
| 482 | + if (documentType.getIcon() != null) { |
| 483 | + writeProperty(xout, "CFBundleTypeIconFile", documentType.getIcon().getName()); |
| 484 | + } |
| 485 | + writeStringList(xout, "CFBundleTypeExtensions", documentType.getExtensions()); |
| 486 | + writeStringList(xout, "CFBundleTypeMIMETypes", documentType.getMimeTypes()); |
| 487 | + writeStringList(xout, "CFBundleTypeOSTypes", documentType.getOsTypes()); |
| 488 | + |
| 489 | + } |
| 490 | + xout.writeEndElement(); |
| 491 | + xout.writeCharacters("\n"); |
| 492 | + } |
| 493 | + |
454 | 494 | // Write runtime
|
455 | 495 | if (runtime != null) {
|
456 | 496 | writeProperty(xout, "JVMRuntime", runtime.getDir().getParentFile().getParentFile().getName());
|
@@ -524,6 +564,19 @@ private void writeProperty(XMLStreamWriter xout, String key, String value) throw
|
524 | 564 | writeString(xout, value);
|
525 | 565 | }
|
526 | 566 |
|
| 567 | + private void writeStringList(XMLStreamWriter xout, String key, List<? extends AbstractKeyValue> list) throws XMLStreamException { |
| 568 | + if ( ! list.isEmpty()) { |
| 569 | + writeKey(xout, key); |
| 570 | + xout.writeStartElement(ARRAY_TAG); |
| 571 | + xout.writeCharacters("\n"); |
| 572 | + for (AbstractKeyValue extension : list) { |
| 573 | + writeString(xout, extension.getValue()); |
| 574 | + } |
| 575 | + xout.writeEndElement(); |
| 576 | + xout.writeCharacters("\n"); |
| 577 | + } |
| 578 | + } |
| 579 | + |
527 | 580 | private void writePkgInfo(File file) throws IOException {
|
528 | 581 | Writer out = new BufferedWriter(new FileWriter(file));
|
529 | 582 |
|
|
0 commit comments