Skip to content

Commit

Permalink
[IJ plugin] Support IJ platform 232 (#5095)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD authored Jul 13, 2023
1 parent 9dc4276 commit c6095ce
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
2 changes: 1 addition & 1 deletion intellij-plugin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pluginRepositoryUrl=https://github.com/apollographql/apollo-kotlin
# for insight into build numbers and IntelliJ Platform versions.
pluginSinceBuild=222
# This can be kept empty to mean no upper bound, but it's recommended to set it to whatever was tested
pluginUntilBuild=231.*
pluginUntilBuild=232.*
# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType=IC
# Corresponds to AS Flamingo 2022.2.1 Canary 1 -> https://plugins.jetbrains.com/docs/intellij/android-studio-releases-list.html
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.apollographql.ijplugin.codegen

import com.apollographql.ijplugin.gradle.CODEGEN_GRADLE_TASK_NAME
import com.apollographql.ijplugin.gradle.GradleExecutionHelperCompat
import com.apollographql.ijplugin.gradle.GradleHasSyncedListener
import com.apollographql.ijplugin.gradle.getGradleRootPath
import com.apollographql.ijplugin.project.ApolloProjectListener
Expand Down Expand Up @@ -42,7 +43,6 @@ import org.gradle.tooling.events.ProgressListener
import org.gradle.tooling.events.StartEvent
import org.gradle.tooling.events.SuccessResult
import org.jetbrains.kotlin.idea.configuration.GRADLE_SYSTEM_ID
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
import org.jetbrains.plugins.gradle.util.GradleConstants
import java.util.concurrent.Executors
Expand Down Expand Up @@ -187,13 +187,13 @@ class ApolloCodegenService(
val executionSettings = ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)

gradleExecutorService.submit {
val gradleExecutionHelper = GradleExecutionHelper()
val gradleExecutionHelper = GradleExecutionHelperCompat()
gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
gradleCodegenCancellation = GradleConnector.newCancellationTokenSource()
logd("Start Gradle")
try {
val id = ExternalSystemTaskId.create(GRADLE_SYSTEM_ID, ExternalSystemTaskType.EXECUTE_TASK, project)
gradleExecutionHelper.getBuildLauncher(id, connection, executionSettings, ExternalSystemTaskNotificationListenerAdapter.NULL_OBJECT)
gradleExecutionHelper.getBuildLauncher(connection, id, listOf(CODEGEN_GRADLE_TASK_NAME), executionSettings, ExternalSystemTaskNotificationListenerAdapter.NULL_OBJECT)
.forTasks(CODEGEN_GRADLE_TASK_NAME)
.withCancellationToken(gradleCodegenCancellation!!.token())
.addArguments("--continuous")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.apollographql.ijplugin.gradle

import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener
import org.gradle.tooling.BuildLauncher
import org.gradle.tooling.ModelBuilder
import org.gradle.tooling.ProjectConnection
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings

class GradleExecutionHelperCompat {
private val gradleExecutionHelper = GradleExecutionHelper()

/**
* < 232:
* ```
* public BuildLauncher getBuildLauncher(
* @NotNull final ExternalSystemTaskId id,
* @NotNull ProjectConnection connection,
* @Nullable GradleExecutionSettings settings,
* @NotNull ExternalSystemTaskNotificationListener listener
* )
* ```
*/
private val getBuildLauncherMethodPre232 = GradleExecutionHelper::class.java.methods.firstOrNull {
it.name == "getBuildLauncher" && it.parameterCount == 4
}

/**
* ≥ 232:
* ```
* public @NotNull BuildLauncher getBuildLauncher(
* @NotNull ProjectConnection connection,
* @NotNull ExternalSystemTaskId id,
* @NotNull List<String> tasksAndArguments,
* @NotNull GradleExecutionSettings settings,
* @NotNull ExternalSystemTaskNotificationListener listener
* )
* ```
*/
private val getBuildLauncherMethodPost232 = GradleExecutionHelper::class.java.methods.firstOrNull {
it.name == "getBuildLauncher" && it.parameterCount == 5
}

/**
* < 232:
* ```
* public <T> ModelBuilder<T> getModelBuilder(
* @NotNull Class<T> modelType,
* @NotNull final ExternalSystemTaskId id,
* @Nullable GradleExecutionSettings settings,
* @NotNull ProjectConnection connection,
* @NotNull ExternalSystemTaskNotificationListener listener
* )
*/
private val getModelBuilderMethodPre232 = GradleExecutionHelper::class.java.methods.firstOrNull {
it.name == "getModelBuilder" && it.parameterTypes[1] == ExternalSystemTaskId::class.java
}

/**
* ≥ 232:
* ```
* public <T> @NotNull ModelBuilder<T> getModelBuilder(
* @NotNull Class<T> modelType,
* @NotNull ProjectConnection connection,
* @NotNull ExternalSystemTaskId id,
* @NotNull GradleExecutionSettings settings,
* @NotNull ExternalSystemTaskNotificationListener listener
* )
*/
private val getModelBuilderMethodPost232 = GradleExecutionHelper::class.java.methods.firstOrNull {
it.name == "getModelBuilder" && it.parameterTypes[1] == ProjectConnection::class.java
}

fun <T> execute(
projectPath: String,
settings: GradleExecutionSettings?,
f: (ProjectConnection) -> T,
): T {
return gradleExecutionHelper.execute(projectPath, settings, f)
}

fun getBuildLauncher(
connection: ProjectConnection,
id: ExternalSystemTaskId,
tasksAndArguments: List<String>,
settings: GradleExecutionSettings,
listener: ExternalSystemTaskNotificationListener,
): BuildLauncher {
return if (getBuildLauncherMethodPre232 != null) {
getBuildLauncherMethodPre232.invoke(gradleExecutionHelper, id, connection, settings, listener) as BuildLauncher
} else {
if (getBuildLauncherMethodPost232 == null) {
error("Could not find GradleExecutionHelper.getBuildLauncher method for either < 232 or ≥ 232")
}
getBuildLauncherMethodPost232.invoke(gradleExecutionHelper, connection, id, tasksAndArguments, settings, listener) as BuildLauncher
}
}

fun <T> getModelBuilder(
modelType: Class<T>,
connection: ProjectConnection,
id: ExternalSystemTaskId,
settings: GradleExecutionSettings,
listener: ExternalSystemTaskNotificationListener,
): ModelBuilder<T> {
@Suppress("UNCHECKED_CAST")
return if (getModelBuilderMethodPre232 != null) {
getModelBuilderMethodPre232.invoke(gradleExecutionHelper, modelType, id, settings, connection, listener) as ModelBuilder<T>
} else {
if (getModelBuilderMethodPost232 == null) {
error("Could not find GradleExecutionHelper.getModelBuilder method for either < 232 or ≥ 232")
}
getModelBuilderMethodPost232.invoke(gradleExecutionHelper, modelType, connection, id, settings, listener) as ModelBuilder<T>
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import org.gradle.tooling.CancellationTokenSource
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.model.GradleProject
import org.jetbrains.kotlin.idea.configuration.GRADLE_SYSTEM_ID
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
import org.jetbrains.plugins.gradle.util.GradleConstants
import java.io.File
Expand Down Expand Up @@ -145,14 +144,14 @@ class GradleToolingModelService(

override fun run(indicator: ProgressIndicator) {
val rootProjectPath = project.getGradleRootPath() ?: return
val gradleExecutionHelper = GradleExecutionHelper()
val gradleExecutionHelper = GradleExecutionHelperCompat()
val executionSettings = ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
val rootGradleProject = gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
gradleCancellation = GradleConnector.newCancellationTokenSource()
logd("Fetch Gradle project model")
return@execute try {
val id = ExternalSystemTaskId.create(GRADLE_SYSTEM_ID, ExternalSystemTaskType.RESOLVE_PROJECT, project)
gradleExecutionHelper.getModelBuilder(GradleProject::class.java, id, executionSettings, connection, ExternalSystemTaskNotificationListenerAdapter.NULL_OBJECT)
gradleExecutionHelper.getModelBuilder(GradleProject::class.java, connection, id, executionSettings, ExternalSystemTaskNotificationListenerAdapter.NULL_OBJECT)
.withCancellationToken(gradleCancellation!!.token())
.get()
} catch (t: Throwable) {
Expand All @@ -176,7 +175,7 @@ class GradleToolingModelService(
logd("Fetch tooling model for :${gradleProject.name}")
return@execute try {
val id = ExternalSystemTaskId.create(GRADLE_SYSTEM_ID, ExternalSystemTaskType.RESOLVE_PROJECT, project)
gradleExecutionHelper.getModelBuilder(ApolloGradleToolingModel::class.java, id, executionSettings, connection, ExternalSystemTaskNotificationListenerAdapter.NULL_OBJECT)
gradleExecutionHelper.getModelBuilder(ApolloGradleToolingModel::class.java, connection,id, executionSettings, ExternalSystemTaskNotificationListenerAdapter.NULL_OBJECT)
.withCancellationToken(gradleCancellation!!.token())
.get()
.takeIf {
Expand Down

0 comments on commit c6095ce

Please sign in to comment.