Skip to content
Draft
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
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
kotlin-plugin = "2.1.21"
intellij-platform-plugin = "2.8.0"
apollo = "5.0.0-alpha.2"
apollo = "5.0.0-alpha.3-SNAPSHOT"
grammarkit-plugin = "2022.3.2.2"
changelog-plugin = "2.2.1"
sqlite-jdbc = "3.43.2.0"
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {
// maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
// maven("https://storage.googleapis.com/apollo-previews/m2/")
mavenCentral()
// mavenLocal()
mavenLocal()

intellijPlatform {
defaultRepositories()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apollographql.ijplugin.codegen

import com.apollographql.ijplugin.codegen.helper.DynamicApolloCompilerHelper
import com.apollographql.ijplugin.gradle.CODEGEN_GRADLE_TASK_NAME
import com.apollographql.ijplugin.gradle.GradleHasSyncedListener
import com.apollographql.ijplugin.gradle.SimpleProgressListener
Expand Down Expand Up @@ -163,9 +164,9 @@ class ApolloCodegenService(
}
}

if (apolloKotlinService?.hasCompilerOptions == true) {
if (apolloKotlinService?.hasCompilerOptions == true && project.apolloKotlinProjectModelService.apolloTasksDependencies != null) {
// We can use the built-in Apollo compiler
ApolloCompilerHelper(project).generateSources(apolloKotlinService)
DynamicApolloCompilerHelper(project, project.apolloKotlinProjectModelService.apolloTasksDependencies!!).generateSources(apolloKotlinService)
} else {
// Fall back to the Gradle codegen task
startGradleCodegen()
Expand Down Expand Up @@ -255,9 +256,11 @@ class ApolloCodegenService(
}

private fun startCodegen() {
if (project.apolloKotlinProjectModelService.getApolloKotlinServices().any { it.hasCompilerOptions }) {
if (project.apolloKotlinProjectModelService.getApolloKotlinServices()
.any { it.hasCompilerOptions } && project.apolloKotlinProjectModelService.apolloTasksDependencies != null
) {
logd("Using Apollo compiler for codegen")
ApolloCompilerHelper(project).generateAllSources()
DynamicApolloCompilerHelper(project, project.apolloKotlinProjectModelService.apolloTasksDependencies!!).generateAllSources()
} else {
logd("Using Gradle codegen task")
startGradleCodegen()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:OptIn(ApolloInternal::class, ApolloExperimental::class)

package com.apollographql.ijplugin.codegen
package com.apollographql.ijplugin.codegen.helper

import com.apollographql.apollo.annotations.ApolloExperimental
import com.apollographql.apollo.annotations.ApolloInternal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.apollographql.ijplugin.codegen.helper

import com.apollographql.ijplugin.gradle.ApolloKotlinService
import com.apollographql.ijplugin.util.logw
import com.intellij.openapi.project.Project
import java.io.File
import java.net.URL
import java.net.URLClassLoader

/**
* Invokes [ApolloCompilerHelper] via introspection and a dedicated classloader using the project's dependencies.
* This ensures the same version of the Apollo libraries as the project's are used to generate the code.
*/
class DynamicApolloCompilerHelper(
private val project: Project,
private val apolloTasksDependencies: Set<String>,
) {
private lateinit var apolloCompilerHelperClass: Class<*>
private lateinit var instance: Any

private fun init() {
if (this::instance.isInitialized && this::apolloCompilerHelperClass.isInitialized) return
val dependencies = apolloTasksDependencies.map { File(it.trim()).toURI().toURL() }
val classLoader = ChildFirstClassLoader(dependencies.toTypedArray<URL>(), ApolloCompilerHelper::class.java.classLoader)
apolloCompilerHelperClass = classLoader.loadClass(ApolloCompilerHelper::class.java.name)
instance = apolloCompilerHelperClass.getDeclaredConstructor(Project::class.java).newInstance(project)
}

fun generateAllSources() {
try {
init()
val method = apolloCompilerHelperClass.getMethod("generateAllSources")
method.invoke(instance)
} catch (e: Exception) {
logw(e, "Failed to generate sources for all services")
}
}

fun generateSources(service: ApolloKotlinService) {
try {
init()
val method = apolloCompilerHelperClass.getMethod("generateSources", ApolloKotlinService::class.java)
method.invoke(instance, service)
} catch (e: Exception) {
logw(e, "Failed to generate sources for service ${service.id}")
}
}
}

private class ChildFirstClassLoader(urls: Array<URL>, parent: ClassLoader) : URLClassLoader(urls, parent) {
override fun loadClass(name: String, resolve: Boolean): Class<*> {
val loadedClass = findLoadedClass(name)
if (loadedClass != null) return loadedClass

// Load the helper from this classloader, but read the classes in the parent
if (name.startsWith("com.apollographql.ijplugin.codegen.helper")) {
val resourceName = name.replace('.', '/') + ".class"
val bytes = parent.getResourceAsStream(resourceName)?.readBytes()
?: throw ClassNotFoundException("Could not read $name from parent classloader")
val clazz = defineClass(name, bytes, 0, bytes.size)
if (resolve) resolveClass(clazz)
return clazz
}

return try {
val clazz = findClass(name)
if (resolve) resolveClass(clazz)
clazz
} catch (_: ClassNotFoundException) {
// Fallback to parent
super.loadClass(name, resolve)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class ApolloKotlinProjectModelService(
private var apolloKotlinServices: Map<ApolloKotlinService.Id, ApolloKotlinService> =
project.projectSettingsState.apolloKotlinServices.associateBy { it.id }

var apolloTasksDependencies: Set<String>? = null
private set

init {
logd("project=${project.name}")
startObserveApolloProject()
Expand Down Expand Up @@ -268,6 +271,9 @@ class ApolloKotlinProjectModelService(
return false
}
allCompilationUnitModels.addAll(compilationUnitModels)

// The apolloTasksDependencies should be the same for all projects
apolloTasksDependencies = projectModel.apolloTasksDependencies
}
val apolloKotlinServices = compilationUnitModelsToApolloKotlinServices(allCompilationUnitModels)

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ listOf(pluginManagement.repositories, dependencyResolutionManagement.repositorie
// maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
// maven("https://storage.googleapis.com/apollo-previews/m2/")
mavenCentral()
// mavenLocal()
mavenLocal()
gradlePluginPortal()
}
}
Expand Down
Loading