From 49b9bf5be351f959554e5f6571da1d7c7142cf23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 2 Feb 2025 19:42:42 +0100 Subject: [PATCH] Add BundlesAction.createBundleIU(File) for easy creation of IU It is often useful to get some information about a file and BundlesAction is the P2 way to gather such data, but at the moment it is really cumbersome and requires a lot of calls and handling of cases to get to that point. This now adds a new method BundlesAction.createBundleIU(File) that ease this task. --- .../p2/publisher/eclipse/BundlesAction.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/p2/publisher/eclipse/BundlesAction.java b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/p2/publisher/eclipse/BundlesAction.java index 52663cda1..c55f89c62 100644 --- a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/p2/publisher/eclipse/BundlesAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/p2/publisher/eclipse/BundlesAction.java @@ -201,6 +201,30 @@ static IInstallableUnit createBundleConfigurationUnit(String hostId, Version cuV return MetadataFactory.createInstallableUnit(cu); } + /** + * Attempts to read the given file (or folder) as a bundle and creates an + * {@link IInstallableUnit} describing it. + * + * @param file the file to read as a bundle + * @return an {@link Optional} describing the result, if anything goes wrong an + * empty optional is returned + */ + public static Optional createBundleIU(File file) { + try { + BundleDescription bundleDescription = BundlesAction.createBundleDescription(file); + if (bundleDescription != null) { + IArtifactKey key = BundlesAction.createBundleArtifactKey(bundleDescription.getSymbolicName(), + bundleDescription.getVersion().toString()); + PublisherInfo publisherInfo = new PublisherInfo(); + publisherInfo.setArtifactOptions(IPublisherInfo.A_INDEX); + return Optional.ofNullable(BundlesAction.createBundleIU(bundleDescription, key, publisherInfo)); + } + } catch (BundleException | IOException | RuntimeException e) { + return Optional.empty(); + } + return Optional.empty(); + } + public static IInstallableUnit createBundleIU(BundleDescription bd, IArtifactKey key, IPublisherInfo info) { return new BundlesAction(new BundleDescription[] { bd }).doCreateBundleIU(bd, key, info); }