PathManager: Add getArtifactPath(), use it to find Java debugger-agent

GitOrigin-RevId: 0669b18b87bdf629119b28b328318dc13c136e0f
This commit is contained in:
Eldar Abusalimov
2020-10-31 01:47:20 +00:00
committed by intellij-monorepo-bot
parent 72b315f12a
commit e6334daea0
2 changed files with 56 additions and 22 deletions
@@ -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 {
@@ -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:
* <li> the artifact jar file name is "foo-bar.jar",
* <li> the output directory specified in the project structure is named "foo_bar_jar",
* <li> 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<String> getUtilClassPath() {
Set<String> classPath = new HashSet<>();