From e6334daea09b2e97c11a987a26b2b7c6e8a9e957 Mon Sep 17 00:00:00 2001 From: Eldar Abusalimov Date: Thu, 29 Oct 2020 05:02:20 +0300 Subject: [PATCH] PathManager: Add getArtifactPath(), use it to find Java debugger-agent GitOrigin-RevId: 0669b18b87bdf629119b28b328318dc13c136e0f --- .../impl/RemoteConnectionBuilder.java | 34 +++++--------- .../openapi/application/PathManager.java | 44 +++++++++++++++++++ 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/java/debugger/impl/src/com/intellij/debugger/impl/RemoteConnectionBuilder.java b/java/debugger/impl/src/com/intellij/debugger/impl/RemoteConnectionBuilder.java index a7a63398f390..215adea1e042 100644 --- a/java/debugger/impl/src/com/intellij/debugger/impl/RemoteConnectionBuilder.java +++ b/java/debugger/impl/src/com/intellij/debugger/impl/RemoteConnectionBuilder.java @@ -15,6 +15,7 @@ import com.intellij.execution.configurations.ParametersList; import com.intellij.execution.configurations.RemoteConnection; import com.intellij.ide.plugins.PluginManagerCore; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.PathManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.JavaSdk; @@ -34,6 +35,8 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Properties; import java.util.jar.Attributes; @@ -194,44 +197,31 @@ public class RemoteConnectionBuilder { } } - private static final String AGENT_FILE_NAME = "debugger-agent.jar"; + private static final String AGENT_ARTIFACT_NAME = "debugger-agent"; @NonNls private static final String DEBUG_KEY_NAME = "idea.xdebug.key"; private static void addDebuggerAgent(JavaParameters parameters, @Nullable Project project) { if (AsyncStacksUtils.isAgentEnabled()) { String prefix = "-javaagent:"; ParametersList parametersList = parameters.getVMParametersList(); - if (parametersList.getParameters().stream().noneMatch(p -> p.startsWith(prefix) && p.contains(AGENT_FILE_NAME))) { + if (parametersList.getParameters().stream().noneMatch(p -> p.startsWith(prefix) && p.contains(AGENT_ARTIFACT_NAME + ".jar"))) { Sdk jdk = parameters.getJdk(); String version = jdk != null ? JdkUtil.getJdkMainAttribute(jdk, Attributes.Name.IMPLEMENTATION_VERSION) : null; if (version != null) { JavaSdkVersion sdkVersion = JavaSdkVersion.fromVersionString(version); if (sdkVersion != null && sdkVersion.isAtLeast(JavaSdkVersion.JDK_1_6)) { - File classesRoot = new File(PathUtil.getJarPathForClass(DebuggerManagerImpl.class)); - File agentFile; - if (classesRoot.isFile()) { - agentFile = new File(classesRoot.getParentFile(), "rt/" + AGENT_FILE_NAME); - } - else { - File artifactsInBuildScripts = new File(classesRoot.getParentFile().getParentFile().getParentFile(), "project-artifacts"); - if (artifactsInBuildScripts.exists()) { - //running tests via build scripts - agentFile = new File(artifactsInBuildScripts, "debugger_agent/" + AGENT_FILE_NAME); - } - else { - //running IDE or tests in IDE - agentFile = new File(classesRoot.getParentFile().getParentFile(), "/artifacts/debugger_agent/" + AGENT_FILE_NAME); - } - } - if (agentFile.exists()) { - String agentPath = JavaExecutionUtil.handleSpacesInAgentPath( - agentFile.getAbsolutePath(), "captureAgent", null, f -> f.getName().startsWith("debugger-agent")); + String classesRoot = PathUtil.getJarPathForClass(DebuggerManagerImpl.class); + Path agentArtifactPath = PathManager.getJarArtifactPath(classesRoot, AGENT_ARTIFACT_NAME); + if (Files.exists(agentArtifactPath)) { + String agentPath = JavaExecutionUtil.handleSpacesInAgentPath(agentArtifactPath.toAbsolutePath().toString(), + "captureAgent", null, + f -> f.getName().startsWith("debugger-agent")); if (agentPath != null) { parametersList.add(prefix + agentPath + generateAgentSettings(project)); } } else { - LOG.warn("Capture agent not found: " + agentFile); + LOG.warn("Capture agent not found: " + agentArtifactPath); } } else { diff --git a/platform/util/src/com/intellij/openapi/application/PathManager.java b/platform/util/src/com/intellij/openapi/application/PathManager.java index 5fd0e28ab53d..6591b5238edd 100644 --- a/platform/util/src/com/intellij/openapi/application/PathManager.java +++ b/platform/util/src/com/intellij/openapi/application/PathManager.java @@ -626,6 +626,50 @@ public final class PathManager { return resourceRoot != null ? Paths.get(resourceRoot).toAbsolutePath().toString() : null; } + /** + * Resolves the path to the jar file of an artifact. + * + * @param classesRoot the resource root containing any class of the plugin, see {@link #getJarPathForClass}. + * @param artifactName the artifact name, from which the necessary relative paths and the jar name are derived. + * For an artifact named "foo-bar:jar", we assume that: + *
  • the artifact jar file name is "foo-bar.jar", + *
  • the output directory specified in the project structure is named "foo_bar_jar", + *
  • the "relativeOutputPath" argument of "withArtifact()" in the plugin layout script is just "rt". + */ + public static @NotNull Path getJarArtifactPath(@NotNull String classesRoot, + @NotNull String artifactName) { + String artifactJarName = Strings.trimEnd(artifactName, ":jar") + ".jar"; + String artifactDirNameInBuildLayout = artifactName.replaceAll("\\W", "_"); + return getArtifactPath(classesRoot, artifactJarName, "rt", artifactDirNameInBuildLayout); + } + + /** + * Resolves the path to an artifact. + * + * @param classesRoot the resource root containing any class of the plugin, see {@link #getJarPathForClass}. + * @param artifactFileName the name of the target artifact file + * @param artifactDirNameInPluginLayout the value of "relativeOutputPath" used in the plugin layout for "withArtifact()", usually "rt" + * @param artifactDirNameInBuildLayout the name specified in the output directory property in the project structure + */ + public static @NotNull Path getArtifactPath(@NotNull String classesRoot, + @NotNull String artifactFileName, + @NotNull String artifactDirNameInPluginLayout, + @NotNull String artifactDirNameInBuildLayout) { + Path rootPath = Paths.get(classesRoot); + if (Files.isRegularFile(rootPath)) { + // running regular installation + return rootPath.resolveSibling(artifactDirNameInPluginLayout).resolve(artifactFileName); + } + + Path outClassesDir = rootPath.getParent().getParent(); + Path artifactsDir = outClassesDir.resolveSibling("project-artifacts"); + if (!Files.exists(artifactsDir)) { + // running IDE or tests in IDE + artifactsDir = outClassesDir.resolve("artifacts"); + } // otherwise running tests via build scripts + return artifactsDir.resolve(artifactDirNameInBuildLayout).resolve(artifactFileName); + } + public static @NotNull Collection getUtilClassPath() { Set classPath = new HashSet<>();