From 57e336365b06116f4f1e7b996ff17abd8993e61a Mon Sep 17 00:00:00 2001 From: Ed Merks Date: Thu, 14 Dec 2023 16:22:55 +0100 Subject: [PATCH] Support RepositoryHelper.getWorkspaceBundlePools() Provide access to all the PDE bundle pools of all the known workspaces. https://github.com/eclipse-equinox/p2/issues/410 --- .../repository/helpers/RepositoryHelper.java | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/RepositoryHelper.java b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/RepositoryHelper.java index ec3456b8c4..14adbad8e8 100644 --- a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/RepositoryHelper.java +++ b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/RepositoryHelper.java @@ -20,6 +20,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.LongStream; import java.util.stream.Stream; import org.eclipse.core.runtime.*; @@ -32,11 +34,16 @@ import org.eclipse.osgi.util.NLS; public class RepositoryHelper { + + private static final Pattern WORKSPACE_KEY_PATTERN = Pattern.compile( + "\\s*"); //$NON-NLS-1$ + protected static final String FILE_SCHEME = "file"; //$NON-NLS-1$ /** - * If the provided URI can be interpreted as representing a local address (no schema, or one letter schema) - * but is missing the file schema, a new URI is created which represents the local file. + * If the provided URI can be interpreted as representing a local address (no + * schema, or one letter schema) but is missing the file schema, a new URI is + * created which represents the local file. * * @param location the URI to convert * @return the converted URI, or the original @@ -44,11 +51,11 @@ public class RepositoryHelper { public static URI localRepoURIHelper(URI location) { if (location == null) return null; - if (location.getScheme() == null) // Probably a local path: /home/user/repo + if (location.getScheme() == null) // Probably a local path: /home/user/repo location = (new File(location.getPath())).getAbsoluteFile().toURI(); else if (location.getScheme().length() == 1) - // Probably a windows path: C:\repo + // Probably a windows path: C:\repo location = (new File(URIUtil.toUnencodedString(location))).toURI(); else if (!FILE_SCHEME.equalsIgnoreCase(location.getScheme())) // This else must occur last! @@ -62,8 +69,10 @@ else if (!FILE_SCHEME.equalsIgnoreCase(location.getScheme())) } /** - * Determine if the repository could be used as a valid destination (eg, it is modifiable) - * @param repository the repository to test + * Determine if the repository could be used as a valid destination (eg, it is + * modifiable) + * + * @param repository the repository to test * @return the repository */ public static IRepository validDestinationRepository(IRepository repository) { @@ -173,6 +182,37 @@ public static List getSharedBundlePools() { return List.of(); } + /** + * Returns an unmodifiable list of workspace bundle pools, e.g., as used by PDE. + * + * @return an unmodifiable list of workspace bundle pools. + */ + @SuppressWarnings("nls") + public static List getWorkspaceBundlePools() { + Path locations = Path.of(System.getProperty("user.home"), + ".eclipse/org.eclipse.oomph.setup/setups/locations.setup"); + if (Files.isRegularFile(locations)) { + try (Stream allLocations = Files.lines(locations)) { + return allLocations.map(location -> { + Matcher matcher = WORKSPACE_KEY_PATTERN.matcher(location); + if (matcher.matches()) { + try { + return Path.of(URI.create(matcher.group(1))); + } catch (Exception ex) { + //$FALL-THROUGH$ + } + } + return null; + }).filter(Objects::nonNull).distinct() + .map(workspace -> workspace.resolve(".metadata/.plugins/org.eclipse.pde.core/.bundle_pool")) + .filter(pool -> Files.isRegularFile(pool.resolve("artifacts.xml"))).toList(); + } catch (Exception ex) { + LogHelper.log(Status.warning("The locations load failed: " + locations, ex)); + } + } + return List.of(); + } + @SuppressWarnings("nls") private static Path getAgentManagerLocation() { return Path.of(System.getProperty("user.home"), ".eclipse/org.eclipse.oomph.p2");