Skip to content

Commit a5dc9e6

Browse files
authored
Backport changes IJ plugin from main (#6559)
1 parent 75d9a8e commit a5dc9e6

File tree

9 files changed

+161
-116
lines changed

9 files changed

+161
-116
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.apollographql.ijplugin.gradle.CODEGEN_GRADLE_TASK_NAME
44
import com.apollographql.ijplugin.gradle.GradleHasSyncedListener
55
import com.apollographql.ijplugin.gradle.SimpleProgressListener
66
import com.apollographql.ijplugin.gradle.getGradleRootPath
7+
import com.apollographql.ijplugin.gradle.runGradleBuild
78
import com.apollographql.ijplugin.project.ApolloProjectListener
89
import com.apollographql.ijplugin.project.ApolloProjectService
910
import com.apollographql.ijplugin.project.apolloProjectService
@@ -24,7 +25,6 @@ import com.intellij.openapi.editor.Document
2425
import com.intellij.openapi.editor.EditorFactory
2526
import com.intellij.openapi.editor.event.DocumentEvent
2627
import com.intellij.openapi.editor.event.DocumentListener
27-
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
2828
import com.intellij.openapi.fileEditor.FileDocumentManager
2929
import com.intellij.openapi.fileEditor.FileEditorManagerEvent
3030
import com.intellij.openapi.fileEditor.FileEditorManagerListener
@@ -37,10 +37,6 @@ import kotlinx.coroutines.CoroutineScope
3737
import kotlinx.coroutines.launch
3838
import org.gradle.tooling.CancellationTokenSource
3939
import org.gradle.tooling.GradleConnector
40-
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
41-
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
42-
import org.jetbrains.plugins.gradle.util.GradleConstants
43-
import java.io.File
4440

4541
@Service(Service.Level.PROJECT)
4642
class ApolloCodegenService(
@@ -180,19 +176,18 @@ class ApolloCodegenService(
180176
}
181177

182178
val modules = ModuleManager.getInstance(project).modules
183-
val rootProjectPath = project.getGradleRootPath() ?: return
184179
coroutineScope.launch {
185-
val executionSettings =
186-
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
187-
val gradleExecutionHelper = GradleExecutionHelper()
188-
gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
189-
gradleCodegenCancellation = GradleConnector.newCancellationTokenSource()
190-
logd("Start Gradle")
191-
try {
192-
val cancellationToken = gradleCodegenCancellation!!.token()
193-
connection.newBuild()
194-
.setJavaHome(executionSettings.javaHome?.let { File(it) })
195-
.forTasks(CODEGEN_GRADLE_TASK_NAME)
180+
gradleCodegenCancellation = GradleConnector.newCancellationTokenSource()
181+
logd("Start Gradle")
182+
try {
183+
val cancellationToken = gradleCodegenCancellation!!.token()
184+
val gradleProjectPath = project.getGradleRootPath()
185+
if (gradleProjectPath == null) {
186+
logw("Could not get Gradle root project path")
187+
return@launch
188+
}
189+
runGradleBuild(project, gradleProjectPath) {
190+
it.forTasks(CODEGEN_GRADLE_TASK_NAME)
196191
.withCancellationToken(cancellationToken)
197192
.addArguments("--continuous")
198193
.let {
@@ -211,13 +206,12 @@ class ApolloCodegenService(
211206
VfsUtil.markDirtyAndRefresh(true, true, true, *generatedSourceRoots.toTypedArray())
212207
}
213208
})
214-
.run()
215-
logd("Gradle execution finished")
216-
} catch (t: Throwable) {
217-
logd(t, "Gradle execution failed")
218-
} finally {
219-
gradleCodegenCancellation = null
220209
}
210+
logd("Gradle execution finished")
211+
} catch (t: Throwable) {
212+
logd(t, "Gradle execution failed")
213+
} finally {
214+
gradleCodegenCancellation = null
221215
}
222216
}
223217
}

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

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@ 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.util.ExternalSystemApiUtil
1716
import com.intellij.openapi.progress.ProgressIndicator
1817
import com.intellij.openapi.progress.Task
1918
import com.intellij.openapi.project.Project
2019
import org.gradle.tooling.Failure
2120
import org.gradle.tooling.model.GradleProject
22-
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
23-
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
24-
import org.jetbrains.plugins.gradle.util.GradleConstants
25-
import java.io.File
2621

2722
class DownloadSchemaAction : AnAction() {
2823
override fun actionPerformed(e: AnActionEvent) {
@@ -49,20 +44,13 @@ private class DownloadSchemaTask(project: Project) : Task.Backgroundable(
4944
false,
5045
) {
5146
override fun run(indicator: ProgressIndicator) {
52-
val rootProjectPath = project.getGradleRootPath() ?: return
53-
val gradleExecutionHelper = GradleExecutionHelper()
54-
val executionSettings =
55-
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
56-
val rootGradleProject = gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
57-
logd("Fetch Gradle project model")
58-
return@execute try {
59-
connection.model<GradleProject>(GradleProject::class.java)
60-
.setJavaHome(executionSettings.javaHome?.let { File(it) })
61-
.get()
62-
} catch (t: Throwable) {
63-
logw(t, "Couldn't fetch Gradle project model")
64-
null
65-
}
47+
val gradleProjectPath = project.getGradleRootPath() ?: return
48+
49+
val rootGradleProject: GradleProject = try {
50+
getGradleModel(project, gradleProjectPath) { it }
51+
} catch (t: Throwable) {
52+
logw(t, "Couldn't fetch Gradle project model")
53+
null
6654
} ?: return
6755

6856
val allDownloadSchemaTasks: List<String> = rootGradleProject.allChildrenRecursively()
@@ -83,11 +71,9 @@ private class DownloadSchemaTask(project: Project) : Task.Backgroundable(
8371
return
8472
}
8573

86-
gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
87-
try {
88-
connection.newBuild()
89-
.setJavaHome(executionSettings.javaHome?.let { File(it) })
90-
.forTasks(*allDownloadSchemaTasks.toTypedArray())
74+
try {
75+
runGradleBuild(project, gradleProjectPath) {
76+
it.forTasks(*allDownloadSchemaTasks.toTypedArray())
9177
.addProgressListener(object : SimpleProgressListener() {
9278
override fun onFailure(failures: List<Failure>) {
9379
super.onFailure(failures)
@@ -112,11 +98,10 @@ private class DownloadSchemaTask(project: Project) : Task.Backgroundable(
11298
)
11399
}
114100
})
115-
.run()
116-
logd("Gradle execution finished")
117-
} catch (t: Throwable) {
118-
logd(t, "Gradle execution failed")
119101
}
102+
logd("Gradle execution finished")
103+
} catch (t: Throwable) {
104+
logd(t, "Gradle execution failed")
120105
}
121106
}
122107
}

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

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.apollographql.ijplugin.util.newDisposable
1616
import com.intellij.openapi.Disposable
1717
import com.intellij.openapi.components.Service
1818
import com.intellij.openapi.components.service
19-
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
2019
import com.intellij.openapi.progress.ProcessCanceledException
2120
import com.intellij.openapi.progress.ProgressManager
2221
import com.intellij.openapi.project.Project
@@ -29,9 +28,6 @@ import kotlinx.coroutines.launch
2928
import org.gradle.tooling.CancellationTokenSource
3029
import org.gradle.tooling.GradleConnector
3130
import org.gradle.tooling.model.GradleProject
32-
import org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper
33-
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
34-
import org.jetbrains.plugins.gradle.util.GradleConstants
3531
import java.io.File
3632

3733
@Service(Service.Level.PROJECT)
@@ -160,24 +156,22 @@ class GradleToolingModelService(
160156

161157
private fun doRun() {
162158
logd()
163-
val rootProjectPath = project.getGradleRootPath() ?: return
164-
val gradleExecutionHelper = GradleExecutionHelper()
165-
val executionSettings =
166-
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
167-
val rootGradleProject = gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
168-
gradleCancellation = GradleConnector.newCancellationTokenSource()
169-
logd("Fetch Gradle project model")
170-
return@execute try {
171-
connection.model<GradleProject>(GradleProject::class.java)
172-
.setJavaHome(executionSettings.javaHome?.let { File(it) })
173-
.withCancellationToken(gradleCancellation!!.token())
174-
.get()
175-
} catch (t: Throwable) {
176-
logw(t, "Couldn't fetch Gradle project model")
177-
null
178-
} finally {
179-
gradleCancellation = null
159+
gradleCancellation = GradleConnector.newCancellationTokenSource()
160+
logd("Fetch Gradle project model")
161+
val gradleProjectPath = project.getGradleRootPath()
162+
if (gradleProjectPath == null) {
163+
logw("Could not get Gradle root project path")
164+
return
165+
}
166+
val rootGradleProject: GradleProject = try {
167+
getGradleModel(project, gradleProjectPath) {
168+
it.withCancellationToken(gradleCancellation!!.token())
180169
}
170+
} catch (t: Throwable) {
171+
logw(t, "Couldn't fetch Gradle project model")
172+
null
173+
} finally {
174+
gradleCancellation = null
181175
} ?: return
182176
project.telemetryService.gradleModuleCount = rootGradleProject.children.size + 1
183177

@@ -189,27 +183,25 @@ class GradleToolingModelService(
189183

190184
val allToolingModels = allApolloGradleProjects.mapIndexedNotNull { index, gradleProject ->
191185
if (isAbortRequested()) return@doRun
192-
gradleExecutionHelper.execute(gradleProject.projectDirectory.canonicalPath, executionSettings) { connection ->
193-
gradleCancellation = GradleConnector.newCancellationTokenSource()
194-
logd("Fetch tooling model for ${gradleProject.path}")
195-
return@execute try {
196-
connection.model<ApolloGradleToolingModel>(ApolloGradleToolingModel::class.java)
197-
.setJavaHome(executionSettings.javaHome?.let { File(it) })
198-
.withCancellationToken(gradleCancellation!!.token())
199-
.get()
200-
.takeIf {
201-
val isCompatibleVersion = it.versionMajor == ApolloGradleToolingModel.VERSION_MAJOR
202-
if (!isCompatibleVersion) {
203-
logw("Incompatible version of Apollo Gradle plugin in module ${gradleProject.path}: ${it.versionMajor} != ${ApolloGradleToolingModel.VERSION_MAJOR}, ignoring")
204-
}
205-
isCompatibleVersion
206-
}
207-
} catch (t: Throwable) {
208-
logw(t, "Couldn't fetch tooling model for ${gradleProject.path}")
209-
null
210-
} finally {
211-
gradleCancellation = null
186+
187+
gradleCancellation = GradleConnector.newCancellationTokenSource()
188+
logd("Fetch tooling model for ${gradleProject.path}")
189+
try {
190+
getGradleModel<ApolloGradleToolingModel>(project, gradleProject.projectDirectory.canonicalPath) {
191+
it.withCancellationToken(gradleCancellation!!.token())
212192
}
193+
?.takeIf {
194+
val isCompatibleVersion = it.versionMajor == ApolloGradleToolingModel.VERSION_MAJOR
195+
if (!isCompatibleVersion) {
196+
logw("Incompatible version of Apollo Gradle plugin in module ${gradleProject.path}: ${it.versionMajor} != ${ApolloGradleToolingModel.VERSION_MAJOR}, ignoring")
197+
}
198+
isCompatibleVersion
199+
}
200+
} catch (t: Throwable) {
201+
logw(t, "Couldn't fetch tooling model for ${gradleProject.path}")
202+
null
203+
} finally {
204+
gradleCancellation = null
213205
}
214206
}
215207

@@ -226,7 +218,7 @@ class GradleToolingModelService(
226218
}
227219
try {
228220
ProgressManager.checkCanceled()
229-
} catch (e: ProcessCanceledException) {
221+
} catch (@Suppress("IncorrectCancellationExceptionHandling") _: ProcessCanceledException) {
230222
logd("Canceled by user")
231223
return true
232224
}

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import com.apollographql.ijplugin.util.logw
44
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
55
import com.intellij.openapi.module.ModuleManager
66
import com.intellij.openapi.project.Project
7+
import org.gradle.tooling.BuildLauncher
8+
import org.gradle.tooling.GradleConnector
9+
import org.gradle.tooling.ModelBuilder
710
import org.gradle.tooling.model.GradleProject
11+
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings
12+
import org.jetbrains.plugins.gradle.util.GradleConstants
13+
import java.io.File
814

915
const val CODEGEN_GRADLE_TASK_NAME = "generateApolloSources"
1016

@@ -19,3 +25,51 @@ fun Project.getGradleRootPath(): String? {
1925
fun GradleProject.allChildrenRecursively(): List<GradleProject> {
2026
return listOf(this) + children.flatMap { it.allChildrenRecursively() }
2127
}
28+
29+
fun runGradleBuild(
30+
project: Project,
31+
gradleProjectPath: String,
32+
configureBuildLauncher: (BuildLauncher) -> BuildLauncher,
33+
) {
34+
val executionSettings = ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(
35+
project,
36+
gradleProjectPath,
37+
GradleConstants.SYSTEM_ID
38+
)
39+
val connection = GradleConnector.newConnector()
40+
.forProjectDirectory(File(gradleProjectPath))
41+
.connect()
42+
val buildLauncher = configureBuildLauncher(
43+
connection.newBuild()
44+
.setJavaHome(executionSettings.javaHome?.let { File(it) })
45+
)
46+
try {
47+
buildLauncher.run()
48+
} finally {
49+
connection.close()
50+
}
51+
}
52+
53+
inline fun <reified T> getGradleModel(
54+
project: Project,
55+
gradleProjectPath: String,
56+
configureModelBuilder: (ModelBuilder<T>) -> ModelBuilder<T>,
57+
): T? {
58+
val executionSettings = ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(
59+
project,
60+
gradleProjectPath,
61+
GradleConstants.SYSTEM_ID
62+
)
63+
val connection = GradleConnector.newConnector()
64+
.forProjectDirectory(File(gradleProjectPath))
65+
.connect()
66+
val modelBuilder = configureModelBuilder(
67+
connection.model(T::class.java)
68+
.setJavaHome(executionSettings.javaHome?.let { File(it) })
69+
)
70+
try {
71+
return modelBuilder.get()
72+
} finally {
73+
connection.close()
74+
}
75+
}

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/navigation/GraphQLNavigation.kt

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

3+
import com.apollographql.ijplugin.util.allSuperTypes
34
import com.apollographql.ijplugin.util.apollo3
45
import com.apollographql.ijplugin.util.apollo4
56
import com.apollographql.ijplugin.util.asKtClass
@@ -86,19 +87,17 @@ fun KtClass.isApolloOperation(): Boolean {
8687
}
8788

8889
fun KtClass.isApolloFragment(): Boolean {
89-
return superTypeListEntries.any {
90-
val superType = it.typeAsUserType?.referenceExpression?.resolveKtName()?.kotlinFqName
91-
superType in APOLLO_FRAGMENT_TYPE
90+
return allSuperTypes().any {
91+
it.fqName in APOLLO_FRAGMENT_TYPE
9292
} ||
9393
// Fallback for fragments in responseBased codegen: they are interfaces generated in a .fragment package.
9494
// This can lead to false positives, but consequences are not dire.
9595
isInterface() && kotlinFqName?.parent()?.shortName == "fragment" && hasGeneratedByApolloComment()
9696
}
9797

9898
fun KtClass.isApolloOperationOrFragment(): Boolean {
99-
return superTypeListEntries.any {
100-
val superType = it.typeAsUserType?.referenceExpression?.resolveKtName()?.kotlinFqName
101-
superType in APOLLO_OPERATION_TYPES || superType in APOLLO_FRAGMENT_TYPE
99+
return allSuperTypes().any {
100+
it.fqName in APOLLO_OPERATION_TYPES || it.fqName in APOLLO_FRAGMENT_TYPE
102101
} ||
103102
// Fallback for fragments in responseBased codegen: they are interfaces generated in a .fragment package.
104103
// This can lead to false positives, but consequences are not dire.

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/project/ApolloProjectService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ interface ApolloProjectService {
1111
V2,
1212
V3,
1313
V4,
14+
V5,
1415
;
1516
val isAtLeastV3 get() = this >= V3
1617
val isAtLeastV4 get() = this >= V4
18+
val isAtLeastV5 get() = this >= V5
1719
}
1820

1921
val apolloVersion: ApolloVersion

0 commit comments

Comments
 (0)