Skip to content

Commit 8d9d13a

Browse files
authored
Reduce usage of GradleExecutionHelper (#6355)
1 parent 3ac7a04 commit 8d9d13a

File tree

4 files changed

+27
-78
lines changed

4 files changed

+27
-78
lines changed

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/codegen/ApolloCodegenService.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.apollographql.ijplugin.codegen
22

33
import com.apollographql.ijplugin.gradle.CODEGEN_GRADLE_TASK_NAME
4-
import com.apollographql.ijplugin.gradle.GradleExecutionHelperCompat
54
import com.apollographql.ijplugin.gradle.GradleHasSyncedListener
65
import com.apollographql.ijplugin.gradle.SimpleProgressListener
76
import com.apollographql.ijplugin.gradle.getGradleRootPath
@@ -25,9 +24,6 @@ import com.intellij.openapi.editor.Document
2524
import com.intellij.openapi.editor.EditorFactory
2625
import com.intellij.openapi.editor.event.DocumentEvent
2726
import com.intellij.openapi.editor.event.DocumentListener
28-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
29-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener
30-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType
3127
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
3228
import com.intellij.openapi.fileEditor.FileDocumentManager
3329
import com.intellij.openapi.fileEditor.FileEditorManagerEvent
@@ -39,7 +35,7 @@ import com.intellij.openapi.util.CheckedDisposable
3935
import com.intellij.openapi.vfs.VfsUtil
4036
import org.gradle.tooling.CancellationTokenSource
4137
import org.gradle.tooling.GradleConnector
42-
import org.jetbrains.kotlin.idea.configuration.GRADLE_SYSTEM_ID
38+
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
4339
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
4440
import org.jetbrains.plugins.gradle.util.GradleConstants
4541
import java.util.concurrent.Executors
@@ -85,7 +81,8 @@ class ApolloCodegenService(
8581
})
8682
}
8783

88-
private fun shouldTriggerCodegenAutomatically() = project.apolloProjectService.apolloVersion.isAtLeastV3 && project.projectSettingsState.automaticCodegenTriggering
84+
private fun shouldTriggerCodegenAutomatically() =
85+
project.apolloProjectService.apolloVersion.isAtLeastV3 && project.projectSettingsState.automaticCodegenTriggering
8986

9087
private fun startOrStopCodegenObservers() {
9188
if (shouldTriggerCodegenAutomatically()) {
@@ -120,7 +117,8 @@ class ApolloCodegenService(
120117
EditorFactory.getInstance().eventMulticaster.addDocumentListener(object : DocumentListener {
121118
override fun documentChanged(event: DocumentEvent) {
122119
val vFile = FileDocumentManager.getInstance().getFile(event.document) ?: return
123-
val isGqlFileInProject = vFile.fileType is GraphQLFileType && ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(vFile) != null
120+
val isGqlFileInProject =
121+
vFile.fileType is GraphQLFileType && ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(vFile) != null
124122
if (!isGqlFileInProject) {
125123
// Not a GraphQL file or not from this project: ignore
126124
return
@@ -182,18 +180,19 @@ class ApolloCodegenService(
182180

183181
val modules = ModuleManager.getInstance(project).modules
184182
val rootProjectPath = project.getGradleRootPath() ?: return
185-
val executionSettings = ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
183+
val executionSettings =
184+
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
186185

187186
gradleExecutorService.submit {
188-
val gradleExecutionHelper = GradleExecutionHelperCompat()
187+
val gradleExecutionHelper = GradleExecutionHelper()
189188
gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
190189
gradleCodegenCancellation = GradleConnector.newCancellationTokenSource()
191190
logd("Start Gradle")
192191
try {
193-
val id = ExternalSystemTaskId.create(GRADLE_SYSTEM_ID, ExternalSystemTaskType.EXECUTE_TASK, project)
194-
gradleExecutionHelper.getBuildLauncher(connection, id, listOf(CODEGEN_GRADLE_TASK_NAME), executionSettings, ExternalSystemTaskNotificationListener.NULL_OBJECT)
192+
val cancellationToken = gradleCodegenCancellation!!.token()
193+
connection.newBuild()
195194
.forTasks(CODEGEN_GRADLE_TASK_NAME)
196-
.withCancellationToken(gradleCodegenCancellation!!.token())
195+
.withCancellationToken(cancellationToken)
197196
.addArguments("--continuous")
198197
.addProgressListener(object : SimpleProgressListener() {
199198
override fun onSuccess() {

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/gradle/DownloadSchemaAction.kt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ import com.intellij.notification.NotificationType
1313
import com.intellij.openapi.actionSystem.ActionUpdateThread
1414
import com.intellij.openapi.actionSystem.AnAction
1515
import com.intellij.openapi.actionSystem.AnActionEvent
16-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
17-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener
18-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType
1916
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
2017
import com.intellij.openapi.progress.ProgressIndicator
2118
import com.intellij.openapi.progress.Task
2219
import com.intellij.openapi.project.Project
2320
import org.gradle.tooling.Failure
2421
import org.gradle.tooling.model.GradleProject
25-
import org.jetbrains.kotlin.idea.configuration.GRADLE_SYSTEM_ID
22+
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
2623
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
2724
import org.jetbrains.plugins.gradle.util.GradleConstants
2825

@@ -42,7 +39,8 @@ class DownloadSchemaAction : AnAction() {
4239
}
4340

4441
private val DOWNLOAD_SCHEMA_TASK_REGEX = Regex("download.+ApolloSchemaFrom(Introspection|Registry)")
45-
private const val SCHEMA_CONFIGURATION_DOC_URL = "https://www.apollographql.com/docs/kotlin/advanced/plugin-configuration#downloading-a-schema"
42+
private const val SCHEMA_CONFIGURATION_DOC_URL =
43+
"https://www.apollographql.com/docs/kotlin/advanced/plugin-configuration#downloading-a-schema"
4644

4745
private class DownloadSchemaTask(project: Project) : Task.Backgroundable(
4846
project,
@@ -51,14 +49,13 @@ private class DownloadSchemaTask(project: Project) : Task.Backgroundable(
5149
) {
5250
override fun run(indicator: ProgressIndicator) {
5351
val rootProjectPath = project.getGradleRootPath() ?: return
54-
val gradleExecutionHelper = GradleExecutionHelperCompat()
55-
val executionSettings = ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
52+
val gradleExecutionHelper = GradleExecutionHelper()
53+
val executionSettings =
54+
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
5655
val rootGradleProject = gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
5756
logd("Fetch Gradle project model")
5857
return@execute try {
59-
val id = ExternalSystemTaskId.create(GRADLE_SYSTEM_ID, ExternalSystemTaskType.RESOLVE_PROJECT, project)
60-
gradleExecutionHelper.getModelBuilder(GradleProject::class.java, connection, id, executionSettings, ExternalSystemTaskNotificationListener.NULL_OBJECT)
61-
.get()
58+
connection.model<GradleProject>(GradleProject::class.java).get()
6259
} catch (t: Throwable) {
6360
logw(t, "Couldn't fetch Gradle project model")
6461
null
@@ -85,8 +82,7 @@ private class DownloadSchemaTask(project: Project) : Task.Backgroundable(
8582

8683
gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
8784
try {
88-
val id = ExternalSystemTaskId.create(GRADLE_SYSTEM_ID, ExternalSystemTaskType.EXECUTE_TASK, project)
89-
gradleExecutionHelper.getBuildLauncher(connection, id, allDownloadSchemaTasks, executionSettings, ExternalSystemTaskNotificationListener.NULL_OBJECT)
85+
connection.newBuild()
9086
.forTasks(*allDownloadSchemaTasks.toTypedArray())
9187
.addProgressListener(object : SimpleProgressListener() {
9288
override fun onFailure(failures: List<Failure>) {
@@ -95,14 +91,16 @@ private class DownloadSchemaTask(project: Project) : Task.Backgroundable(
9591
project = project,
9692
title = ApolloBundle.message("action.DownloadSchemaAction.buildFail.title"),
9793
content = ApolloBundle.message("action.DownloadSchemaAction.buildFail.content", failures.firstOrNull()?.message
98-
?: "(no message)", allDownloadSchemaTasks.joinToString(" ")),
94+
?: "(no message)", allDownloadSchemaTasks.joinToString(" ")
95+
),
9996
type = NotificationType.WARNING
10097
)
10198
}
10299

103100
override fun onSuccess() {
104101
super.onSuccess()
105-
val schemas = if (allDownloadSchemaTasks.size > 1) ApolloBundle.message("action.DownloadSchemaAction.schema.plural") else ApolloBundle.message("action.DownloadSchemaAction.schema.singular")
102+
val schemas =
103+
if (allDownloadSchemaTasks.size > 1) ApolloBundle.message("action.DownloadSchemaAction.schema.plural") else ApolloBundle.message("action.DownloadSchemaAction.schema.singular")
106104
showNotification(
107105
project = project,
108106
content = ApolloBundle.message("action.DownloadSchemaAction.buildSuccess.content", schemas),

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/gradle/GradleExecutionHelperCompat.kt

Lines changed: 0 additions & 43 deletions
This file was deleted.

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/gradle/GradleToolingModelService.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ import com.apollographql.ijplugin.util.newDisposable
1717
import com.intellij.openapi.Disposable
1818
import com.intellij.openapi.components.Service
1919
import com.intellij.openapi.components.service
20-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
21-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener
22-
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType
2320
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
2421
import com.intellij.openapi.progress.ProcessCanceledException
2522
import com.intellij.openapi.progress.ProgressManager
@@ -31,7 +28,7 @@ import com.intellij.openapi.vfs.VirtualFileManager
3128
import org.gradle.tooling.CancellationTokenSource
3229
import org.gradle.tooling.GradleConnector
3330
import org.gradle.tooling.model.GradleProject
34-
import org.jetbrains.kotlin.idea.configuration.GRADLE_SYSTEM_ID
31+
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
3532
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
3633
import org.jetbrains.plugins.gradle.util.GradleConstants
3734
import java.io.File
@@ -162,15 +159,14 @@ class GradleToolingModelService(
162159
private fun doRun() {
163160
logd()
164161
val rootProjectPath = project.getGradleRootPath() ?: return
165-
val gradleExecutionHelper = GradleExecutionHelperCompat()
162+
val gradleExecutionHelper = GradleExecutionHelper()
166163
val executionSettings =
167164
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
168165
val rootGradleProject = gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
169166
gradleCancellation = GradleConnector.newCancellationTokenSource()
170167
logd("Fetch Gradle project model")
171168
return@execute try {
172-
val id = ExternalSystemTaskId.create(GRADLE_SYSTEM_ID, ExternalSystemTaskType.RESOLVE_PROJECT, project)
173-
gradleExecutionHelper.getModelBuilder(GradleProject::class.java, connection, id, executionSettings, ExternalSystemTaskNotificationListener.NULL_OBJECT)
169+
connection.model<GradleProject>(GradleProject::class.java)
174170
.withCancellationToken(gradleCancellation!!.token())
175171
.get()
176172
} catch (t: Throwable) {
@@ -194,8 +190,7 @@ class GradleToolingModelService(
194190
gradleCancellation = GradleConnector.newCancellationTokenSource()
195191
logd("Fetch tooling model for ${gradleProject.path}")
196192
return@execute try {
197-
val id = ExternalSystemTaskId.create(GRADLE_SYSTEM_ID, ExternalSystemTaskType.RESOLVE_PROJECT, project)
198-
gradleExecutionHelper.getModelBuilder(ApolloGradleToolingModel::class.java, connection, id, executionSettings, ExternalSystemTaskNotificationListener.NULL_OBJECT)
193+
connection.model<ApolloGradleToolingModel>(ApolloGradleToolingModel::class.java)
199194
.withCancellationToken(gradleCancellation!!.token())
200195
.get()
201196
.takeIf {

0 commit comments

Comments
 (0)