[gradle] re-use init script for Gradle test debugging IDEA-241416
authorNikita Skvortsov <nikita.skvortsov@jetbrains.com>
Mon, 1 Jun 2020 11:39:32 +0000 (14:39 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Mon, 1 Jun 2020 11:46:52 +0000 (14:46 +0300)
GitOrigin-RevId: 5c683e99f276649c89e6345c575b587222836e5f

platform/external-system-rt/src/com/intellij/openapi/externalSystem/rt/execution/ForkedDebuggerHelper.java
plugins/gradle/java/src/service/debugger/GradleJvmDebuggerBackend.kt
plugins/gradle/src/org/jetbrains/plugins/gradle/service/task/GradleTaskManager.java

index 5c098ceca4f901eaf303ab28ce62c6d6c77b1450..7a041753685860b2e12f288f11eff5a2f86f27b7 100644 (file)
@@ -18,8 +18,13 @@ public class ForkedDebuggerHelper {
   public static final String PARAMETERS_SEPARATOR = ";";
 
   public static final String FINISH_PARAMS = "FINISH_PARAMS";
+  public static final String DISPATCH_PORT_SYS_PROP = "idea.debugger.dispatch.port";
 
   // returns port at which debugger is supposed to communicate with debuggee process
+  public static int setupDebugger(String debuggerId, String processName, String processParameters) {
+    return setupDebugger(debuggerId, processName, processParameters, getPortFromProperty());
+  }
+
   public static int setupDebugger(String debuggerId, String processName, String processParameters, int dispatchPort) {
     int port = 0;
     try {
@@ -35,6 +40,10 @@ public class ForkedDebuggerHelper {
     return port;
   }
 
+  public static void signalizeFinish(String debuggerId, String processName) {
+    signalizeFinish(debuggerId, processName, getPortFromProperty());
+  }
+
   public static void signalizeFinish(String debuggerId, String processName, int dispatchPort) {
     try {
       send(debuggerId, processName, FINISH_PARAMS, dispatchPort);
@@ -93,4 +102,16 @@ public class ForkedDebuggerHelper {
       serverSocket.close();
     }
   }
+
+  private static int getPortFromProperty() {
+    String property = System.getProperty(DISPATCH_PORT_SYS_PROP);
+    try {
+      if (property == null || property.trim().isEmpty()) {
+        throw new IllegalStateException("System property '" + DISPATCH_PORT_SYS_PROP + "' is not set");
+      }
+      return Integer.parseInt(property);
+    } catch (NumberFormatException e) {
+      throw new IllegalStateException("System property '" + DISPATCH_PORT_SYS_PROP + "' has invalid value: " + property, e);
+    }
+  }
 }
index 3203f540f9f341a13b7e7f04f16fbda1c2fad2e2..144e92e00e8c69c94c28006ba90db27a7f8fd5e7 100644 (file)
@@ -29,21 +29,23 @@ class GradleJvmDebuggerBackend : DebuggerBackendExtension {
 
   override fun initializationCode(dispatchPort: String, parameters: String) = listOf(
     "import com.intellij.openapi.externalSystem.rt.execution.ForkedDebuggerHelper",
-    "gradle.taskGraph.beforeTask { Task task ->",
-    "  if (task instanceof org.gradle.api.tasks.testing.Test) {",
-    "    task.maxParallelForks = 1",
-    "    task.forkEvery = 0",
-    "  }",
-    "  if (task instanceof JavaForkOptions) {",
-    "    def debugPort = ForkedDebuggerHelper.setupDebugger('${id()}', task.path, '$parameters', $dispatchPort)",
-    "    def jvmArgs = task.jvmArgs.findAll{!it?.startsWith('-agentlib:jdwp') && !it?.startsWith('-Xrunjdwp')}",
-    "    jvmArgs << ForkedDebuggerHelper.JVM_DEBUG_SETUP_PREFIX + debugPort",
-    "    task.jvmArgs = jvmArgs",
-    "  }",
-    "}",
-    "gradle.taskGraph.afterTask { Task task ->",
-    "  if (task instanceof JavaForkOptions) {",
-    "    ForkedDebuggerHelper.signalizeFinish('${id()}', task.path, $dispatchPort)",
-    "  }",
+    "gradle.taskGraph.whenReady { taskGraph ->",
+    "  taskGraph.allTasks.each { Task task ->",
+    "    if (task instanceof org.gradle.api.tasks.testing.Test) {",
+    "      task.maxParallelForks = 1",
+    "      task.forkEvery = 0",
+    "    }",
+    "    if (task instanceof JavaForkOptions) {",
+    "      task.doFirst {",
+    "        def debugPort = ForkedDebuggerHelper.setupDebugger('${id()}', task.path, '$parameters')",
+    "        def jvmArgs = task.jvmArgs.findAll{!it?.startsWith('-agentlib:jdwp') && !it?.startsWith('-Xrunjdwp')}",
+    "        jvmArgs << ForkedDebuggerHelper.JVM_DEBUG_SETUP_PREFIX + debugPort",
+    "        task.jvmArgs = jvmArgs",
+    "      }",
+    "      task.doLast {",
+    "        ForkedDebuggerHelper.signalizeFinish('${id()}', task.path)",
+    "      }",
+    "    }",
+    "  }" +
     "}")
 }
\ No newline at end of file
index 36e96e0359d99412c251bcb3724ffce3ca0f39eb..e536cd0c23eb66f9c80fa20562e09fe929e528f8 100644 (file)
@@ -52,6 +52,7 @@ import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
+import static com.intellij.openapi.externalSystem.rt.execution.ForkedDebuggerHelper.DISPATCH_PORT_SYS_PROP;
 import static com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunnableState.BUILD_PROCESS_DEBUGGER_PORT_KEY;
 import static com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunnableState.DEBUGGER_DISPATCH_PORT_KEY;
 import static com.intellij.util.containers.ContainerUtil.addAllNotNull;
@@ -100,6 +101,7 @@ public class GradleTaskManager implements ExternalSystemTaskManager<GradleExecut
     Function<ProjectConnection, Void> f = connection -> {
       try {
         setupGradleScriptDebugging(effectiveSettings);
+        setupDebuggerDispatchPort(effectiveSettings);
         appendInitScriptArgument(tasks, jvmParametersSetup, effectiveSettings);
         try {
           for (GradleBuildParticipant buildParticipant : effectiveSettings.getExecutionWorkspace().getBuildParticipants()) {
@@ -231,6 +233,13 @@ public class GradleTaskManager implements ExternalSystemTaskManager<GradleExecut
     }
   }
 
+  private static void setupDebuggerDispatchPort(@NotNull GradleExecutionSettings effectiveSettings) {
+    Integer dispatchPort = effectiveSettings.getUserData(DEBUGGER_DISPATCH_PORT_KEY);
+    if (dispatchPort != null) {
+      effectiveSettings.withVmOption(String.format("-D%s=%d", DISPATCH_PORT_SYS_PROP, dispatchPort));
+    }
+  }
+
   public static void runCustomTask(@NotNull Project project,
                                    @NotNull String executionName,
                                    @NotNull Class<? extends Task> taskClass,