Skip to content

[4.x] Backport #6460 and #6477 #6487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.util.CheckedDisposable
import com.intellij.openapi.vfs.VfsUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.gradle.tooling.CancellationTokenSource
import org.gradle.tooling.GradleConnector
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
import java.util.concurrent.Executors

@Service(Service.Level.PROJECT)
class ApolloCodegenService(
private val project: Project,
private val coroutineScope: CoroutineScope,
) : Disposable {
private var documentChangesDisposable: CheckedDisposable? = null
private var fileEditorChangesDisposable: CheckedDisposable? = null
Expand All @@ -53,8 +55,6 @@ class ApolloCodegenService(

private var gradleCodegenCancellation: CancellationTokenSource? = null

private val gradleExecutorService = Executors.newSingleThreadExecutor()

init {
logd("project=${project.name}")
startOrStopCodegenObservers()
Expand Down Expand Up @@ -181,10 +181,9 @@ class ApolloCodegenService(

val modules = ModuleManager.getInstance(project).modules
val rootProjectPath = project.getGradleRootPath() ?: return
val executionSettings =
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)

gradleExecutorService.submit {
coroutineScope.launch {
val executionSettings =
ExternalSystemApiUtil.getExecutionSettings<GradleExecutionSettings>(project, rootProjectPath, GradleConstants.SYSTEM_ID)
val gradleExecutionHelper = GradleExecutionHelper()
gradleExecutionHelper.execute(rootProjectPath, executionSettings) { connection ->
gradleCodegenCancellation = GradleConnector.newCancellationTokenSource()
Expand Down Expand Up @@ -257,6 +256,5 @@ class ApolloCodegenService(
override fun dispose() {
logd("project=${project.name}")
stopContinuousGradleCodegen()
gradleExecutorService.shutdown()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.apollographql.ijplugin.settings.ProjectSettingsState
import com.apollographql.ijplugin.settings.projectSettingsState
import com.apollographql.ijplugin.telemetry.telemetryService
import com.apollographql.ijplugin.util.dispose
import com.apollographql.ijplugin.util.executeOnPooledThread
import com.apollographql.ijplugin.util.isNotDisposed
import com.apollographql.ijplugin.util.logd
import com.apollographql.ijplugin.util.logw
Expand All @@ -25,6 +24,8 @@ import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.util.CheckedDisposable
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFileManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.gradle.tooling.CancellationTokenSource
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.model.GradleProject
Expand All @@ -36,6 +37,7 @@ import java.io.File
@Service(Service.Level.PROJECT)
class GradleToolingModelService(
private val project: Project,
private val coroutineScope: CoroutineScope,
) : Disposable {
private var gradleHasSyncedDisposable: CheckedDisposable? = null

Expand Down Expand Up @@ -141,7 +143,7 @@ class GradleToolingModelService(
return
}

fetchToolingModelsTask = FetchToolingModelsTask().also { executeOnPooledThread { it.run() } }
fetchToolingModelsTask = FetchToolingModelsTask().also { coroutineScope.launch { it.run() } }
}

private inner class FetchToolingModelsTask : Runnable {
Expand Down
Loading