mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[Gradle|Exec] cleanup: replace the GradleProjectResolver#getProjectDataFunction function
The complex ProjectConnectionDataNodeFunction can be replaced by the two simple functions that prepare Gradle connection and adds error handling for the Gradle sync execution Issues IDEA-363359, IDEA-344664 GitOrigin-RevId: 1652df80f00e61d765db7c8a55f60931361d5318
This commit is contained in:
committed by
intellij-monorepo-bot
parent
cf02931f5b
commit
5ab82d7d23
+13
-22
@@ -7,10 +7,8 @@ import com.intellij.openapi.externalSystem.model.ProjectKeys;
|
||||
import com.intellij.openapi.externalSystem.model.project.*;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import org.gradle.tooling.ProjectConnection;
|
||||
import org.gradle.tooling.model.build.BuildEnvironment;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -19,7 +17,6 @@ import org.jetbrains.plugins.gradle.model.data.BuildParticipant;
|
||||
import org.jetbrains.plugins.gradle.model.data.BuildScriptClasspathData;
|
||||
import org.jetbrains.plugins.gradle.model.data.CompositeBuildData;
|
||||
import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData;
|
||||
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper;
|
||||
import org.jetbrains.plugins.gradle.settings.DistributionType;
|
||||
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings;
|
||||
import org.jetbrains.plugins.gradle.util.GradleConstants;
|
||||
@@ -91,12 +88,10 @@ public final class GradleBuildSrcProjectsResolver {
|
||||
String buildSrcGroup = getBuildSrcGroup(buildPath, buildName);
|
||||
|
||||
buildSrcResolverCtx.setBuildSrcGroup(buildSrcGroup);
|
||||
handleBuildSrcProject(mainBuildProjectDataNode,
|
||||
buildName,
|
||||
index.buildClasspathNodesMap().getModifiable(Paths.get(buildPath)),
|
||||
index.includedModulesPaths(),
|
||||
buildSrcResolverCtx,
|
||||
myProjectResolver.getProjectDataFunction(buildSrcResolverCtx, myResolverChain));
|
||||
|
||||
var buildClasspathNodes = index.buildClasspathNodesMap().getModifiable(Paths.get(buildPath));
|
||||
var includedModulesPaths = index.includedModulesPaths();
|
||||
handleBuildSrcProject(mainBuildProjectDataNode, buildName, buildClasspathNodes, includedModulesPaths, buildSrcResolverCtx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,12 +176,13 @@ public final class GradleBuildSrcProjectsResolver {
|
||||
return compositeBuildDataNode != null ? compositeBuildDataNode.getData() : null;
|
||||
}
|
||||
|
||||
private void handleBuildSrcProject(@NotNull DataNode<ProjectData> resultProjectDataNode,
|
||||
@Nullable String buildName,
|
||||
@NotNull Collection<DataNode<BuildScriptClasspathData>> buildClasspathNodes,
|
||||
@NotNull Map<String, DataNode<ModuleData>> includedModulesPaths,
|
||||
@NotNull DefaultProjectResolverContext buildSrcResolverCtx,
|
||||
@NotNull Function<ProjectConnection, DataNode<ProjectData>> projectConnectionDataNodeFunction) {
|
||||
private void handleBuildSrcProject(
|
||||
@NotNull DataNode<ProjectData> resultProjectDataNode,
|
||||
@Nullable String buildName,
|
||||
@NotNull Collection<DataNode<BuildScriptClasspathData>> buildClasspathNodes,
|
||||
@NotNull Map<String, DataNode<ModuleData>> includedModulesPaths,
|
||||
@NotNull DefaultProjectResolverContext buildSrcResolverCtx
|
||||
) {
|
||||
final String projectPath = buildSrcResolverCtx.getProjectPath();
|
||||
File projectPathFile = new File(projectPath);
|
||||
if (!projectPathFile.isDirectory()) {
|
||||
@@ -202,13 +198,8 @@ public final class GradleBuildSrcProjectsResolver {
|
||||
return;
|
||||
}
|
||||
|
||||
final DataNode<ProjectData> buildSrcProjectDataNode = GradleExecutionHelper.execute(
|
||||
buildSrcResolverCtx.getProjectPath(),
|
||||
buildSrcResolverCtx.getSettings(),
|
||||
buildSrcResolverCtx.getExternalSystemTaskId(),
|
||||
buildSrcResolverCtx.getListener(),
|
||||
buildSrcResolverCtx.getCancellationToken(),
|
||||
projectConnectionDataNodeFunction
|
||||
var buildSrcProjectDataNode = GradleProjectResolver.executeProjectResolverTask(buildSrcResolverCtx, myResolverChain, () ->
|
||||
myProjectResolver.doResolveProjectInfo(buildSrcResolverCtx, myResolverChain)
|
||||
);
|
||||
|
||||
if (buildSrcProjectDataNode == null) return;
|
||||
|
||||
+38
-52
@@ -32,7 +32,6 @@ import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.io.NioPathUtil;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.util.ExceptionUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.intellij.util.containers.CollectionFactory;
|
||||
@@ -178,8 +177,9 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
|
||||
|
||||
var projectResolverChain = createProjectResolverChain(resolverContext);
|
||||
|
||||
var projectDataFunction = getProjectDataFunction(resolverContext, projectResolverChain);
|
||||
var projectDataNode = GradleExecutionHelper.execute(projectPath, settings, id, listener, cancellationToken, projectDataFunction);
|
||||
var projectDataNode = executeProjectResolverTask(resolverContext, projectResolverChain, () ->
|
||||
doResolveProjectInfo(resolverContext, projectResolverChain)
|
||||
);
|
||||
|
||||
// auto-discover buildSrc projects of the main and included builds
|
||||
var gradleVersion = resolverContext.getProjectGradleVersion();
|
||||
@@ -196,14 +196,6 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected @NotNull Function<ProjectConnection, DataNode<ProjectData>> getProjectDataFunction(
|
||||
@NotNull DefaultProjectResolverContext resolverContext,
|
||||
@NotNull GradleProjectResolverExtension projectResolverChain
|
||||
) {
|
||||
return new ProjectConnectionDataNodeFunction(resolverContext, projectResolverChain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancelTask(@NotNull ExternalSystemTaskId id, @NotNull ExternalSystemTaskNotificationListener listener) {
|
||||
for (CancellationTokenSource cancellationTokenSource : myCancellationMap.get(id)) {
|
||||
@@ -224,6 +216,41 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
|
||||
}
|
||||
}
|
||||
|
||||
protected static <R> R executeProjectResolverTask(
|
||||
@NotNull DefaultProjectResolverContext resolverContext,
|
||||
@NotNull GradleProjectResolverExtension projectResolverChain,
|
||||
@NotNull Supplier<R> task
|
||||
) {
|
||||
var projectPath = resolverContext.getProjectPath();
|
||||
var id = resolverContext.getExternalSystemTaskId();
|
||||
var settings = resolverContext.getSettings();
|
||||
var listener = resolverContext.getListener();
|
||||
var cancellationToken = resolverContext.getCancellationToken();
|
||||
var buildEnvironment = resolverContext.getBuildEnvironment();
|
||||
|
||||
return GradleExecutionHelper.execute(projectPath, settings, id, listener, cancellationToken, connection -> {
|
||||
try {
|
||||
resolverContext.setConnection(connection);
|
||||
return task.get();
|
||||
}
|
||||
catch (ProcessCanceledException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
LOG.info("Gradle project resolve error", e);
|
||||
var esException = ExceptionUtil.findCause(e, ExternalSystemException.class);
|
||||
if (esException != null && esException != e) {
|
||||
LOG.info("\nCaused by: " + esException.getOriginalReason());
|
||||
}
|
||||
|
||||
ExternalSystemSyncActionsCollector.logError(id.findProject(), id.getId(), extractCause(e));
|
||||
ExternalSystemSyncActionsCollector.logSyncFinished(id.findProject(), id.getId(), false);
|
||||
|
||||
throw projectResolverChain.getUserFriendlyError(buildEnvironment, e, projectPath, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private DataNode<ProjectData> doResolveProjectInfo(
|
||||
@NotNull final DefaultProjectResolverContext resolverContext,
|
||||
@@ -896,47 +923,6 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
|
||||
}
|
||||
}
|
||||
|
||||
private final class ProjectConnectionDataNodeFunction implements Function<ProjectConnection, DataNode<ProjectData>> {
|
||||
private final @NotNull GradleProjectResolverExtension myProjectResolverChain;
|
||||
private final @NotNull DefaultProjectResolverContext myResolverContext;
|
||||
|
||||
private ProjectConnectionDataNodeFunction(
|
||||
@NotNull DefaultProjectResolverContext resolverContext,
|
||||
@NotNull GradleProjectResolverExtension projectResolverChain
|
||||
) {
|
||||
myResolverContext = resolverContext;
|
||||
myProjectResolverChain = projectResolverChain;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull DataNode<ProjectData> fun(@NotNull ProjectConnection connection) {
|
||||
ExternalSystemTaskId taskId = myResolverContext.getExternalSystemTaskId();
|
||||
final long activityId = taskId.getId();
|
||||
|
||||
return computeCancellable(myResolverContext, () -> {
|
||||
try {
|
||||
myResolverContext.setConnection(connection);
|
||||
return doResolveProjectInfo(myResolverContext, myProjectResolverChain);
|
||||
}
|
||||
catch (ProcessCanceledException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
LOG.info("Gradle project resolve error", e);
|
||||
ExternalSystemException esException = ExceptionUtil.findCause(e, ExternalSystemException.class);
|
||||
if (esException != null && esException != e) {
|
||||
LOG.info("\nCaused by: " + esException.getOriginalReason());
|
||||
}
|
||||
ExternalSystemSyncActionsCollector.logError(taskId.findProject(), activityId, extractCause(e));
|
||||
ExternalSystemSyncActionsCollector.logSyncFinished(taskId.findProject(), activityId, false);
|
||||
|
||||
throw myProjectResolverChain.getUserFriendlyError(
|
||||
myResolverContext.getBuildEnvironment(), e, myResolverContext.getProjectPath(), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static Throwable extractCause(Throwable e) {
|
||||
if (e instanceof BuildActionFailureException) {
|
||||
return extractCause(e.getCause());
|
||||
|
||||
Reference in New Issue
Block a user