Skip to content

Commit

Permalink
[gradle-plugin] expose the outgoing variants (#6329)
Browse files Browse the repository at this point in the history
* Directly expose the outgoing variants

* Add @ApolloExperimental

* Fix KDoc
  • Loading branch information
martinbonnin authored Dec 17, 2024
1 parent bf2f7b8 commit db6272e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ interface ApolloExtension {
*/
val generateSourcesDuringGradleSync: Property<Boolean>

@ApolloExperimental
val useGradleVariants: Property<Boolean>

/**
* Common apollo dependencies using the same version as the Apollo Gradle Plugin currently in the classpath
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.apollographql.apollo.compiler.OperationOutputGenerator
import com.apollographql.apollo.compiler.PackageNameGenerator
import org.gradle.api.Action
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.component.SoftwareComponent
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
Expand Down Expand Up @@ -914,4 +916,32 @@ interface Service {

@ApolloExperimental
fun plugin(dependencyNotation: Any, block: Action<CompilerPlugin>)

/**
* Overrides the way the outgoing variants are connected.
* Use this to publish the metadata to an already existing software component.
*
* By default, a new software component named "apollo" is created.
*
* ```kotlin
* service("service") {
* outgoingVariantsConnection {
* addToSoftwareComponent("java")
* }
* }
* ```
*/
@ApolloExperimental
fun outgoingVariantsConnection(action: Action<in OutgoingVariantsConnection>)

/**
* An [OutgoingVariantsConnection] defines how outgoing variants are added to software components.
*/
@ApolloExperimental
interface OutgoingVariantsConnection {
fun addToSoftwareComponent(name: String)
fun addToSoftwareComponent(softwareComponent: SoftwareComponent)

val outgoingVariants: List<Configuration>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.attributes.HasConfigurableAttributes
import org.gradle.api.attributes.Usage
import org.gradle.api.component.AdhocComponentWithVariants
import org.gradle.api.component.SoftwareComponent
import org.gradle.api.component.SoftwareComponentFactory
import org.gradle.api.file.FileCollection
import org.gradle.api.file.SourceDirectorySet
Expand All @@ -35,7 +36,6 @@ import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
import java.io.File
import java.lang.reflect.Field
import java.util.concurrent.Callable
import javax.inject.Inject

Expand All @@ -50,7 +50,9 @@ abstract class DefaultApolloExtension(
private val generateApolloSources: TaskProvider<Task>
private var hasExplicitService = false
private val adhocComponentWithVariants: AdhocComponentWithVariants by lazy {
project.adhocComponentWithVariants()
softwareComponentFactory.adhoc("apollo").also {
project.components.add(it)
}
}
private val apolloMetadataConfiguration: Configuration
private var apolloBuildServiceProvider: Provider<ApolloBuildService>
Expand Down Expand Up @@ -576,10 +578,28 @@ abstract class DefaultApolloExtension(
* - downstream module publishes jar
* In such scenarios, the user must set `alwaysGenerateTypesMatching.set(listOf(".*"))` on the schema module.
*/
adhocComponentWithVariants.addVariantsFromConfiguration(codegenMetadata.consumable) {}
adhocComponentWithVariants.addVariantsFromConfiguration(upstreamIr.consumable) {}
adhocComponentWithVariants.addVariantsFromConfiguration(codegenSchema.consumable) {}
adhocComponentWithVariants.addVariantsFromConfiguration(otherOptions.consumable) {}
val outgoingVariantsConnection = object : Service.OutgoingVariantsConnection {
override fun addToSoftwareComponent(name: String) {
addToSoftwareComponent(project.components.getByName(name))
}

override fun addToSoftwareComponent(softwareComponent: SoftwareComponent) {
check (softwareComponent is AdhocComponentWithVariants) {
"Software component '$softwareComponent' is not an instance of AdhocComponentWithVariants"
}
outgoingVariants.forEach {
softwareComponent.addVariantsFromConfiguration(it) { }
}
}

override val outgoingVariants: List<Configuration>
get() = listOf(codegenMetadata.consumable, upstreamIr.consumable, codegenSchema.consumable, otherOptions.consumable)
}
if (service.outgoingVariantsConnection != null) {
service.outgoingVariantsConnection!!.execute(outgoingVariantsConnection)
} else {
outgoingVariantsConnection.addToSoftwareComponent(adhocComponentWithVariants)
}

service.upstreamDependencies.forEach {
otherOptions.scope.dependencies.add(it)
Expand Down Expand Up @@ -1035,27 +1055,6 @@ abstract class DefaultApolloExtension(
}

override val deps: ApolloDependencies = ApolloDependencies(project.dependencies)

private fun Project.adhocComponentWithVariants(): AdhocComponentWithVariants {
if (useGradleVariants.getOrElse(false)) {
val javaComponent = project.components.findByName("java")
if (javaComponent != null) {
// JVM
return javaComponent as AdhocComponentWithVariants
}

val kotlin = project.kotlinMultiplatformExtension
if (kotlin != null) {
error("Adding variants to multiplatform project is not possible. See https://youtrack.jetbrains.com/issue/KT-58830/")
}

error("Impossible to find an AdHocComponent")
} else {
return softwareComponentFactory.adhoc("apollo").also {
project.components.add(it)
}
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ abstract class DefaultService @Inject constructor(val project: Project, override

var operationOutputAction: Action<in Service.OperationOutputConnection>? = null
var operationManifestAction: Action<in Service.OperationManifestConnection>? = null
var outgoingVariantsConnection: Action<in Service.OutgoingVariantsConnection>? = null

@Deprecated("Use operationManifestConnection", replaceWith = ReplaceWith("operationManifestConnection"))
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
Expand All @@ -133,12 +134,20 @@ abstract class DefaultService @Inject constructor(val project: Project, override

override fun operationManifestConnection(action: Action<in Service.OperationManifestConnection>) {
check(!registered) {
"Apollo: operationOutputConnection {} cannot be configured outside of a service {} block"
"Apollo: operationManifestConnection {} cannot be configured outside of a service {} block"
}

this.operationManifestAction = action
}

override fun outgoingVariantsConnection(action: Action<in Service.OutgoingVariantsConnection>) {
check(!registered) {
"Apollo: outgoingVariantsConnection {} cannot be configured outside of a service {} block"
}

this.outgoingVariantsConnection = action
}

var outputDirAction: Action<in Service.DirectoryConnection>? = null

override fun outputDirConnection(action: Action<in Service.DirectoryConnection>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ dependencies {
}

apollo {
useGradleVariants.set(true)

service("service1") {
packageName.set("com.service1")
dependsOn(project(":schema"))
outgoingVariantsConnection {
addToSoftwareComponent("java")
}
}
service("service2") {
packageName.set("com.service2")
dependsOn(project(":schema"))
outgoingVariantsConnection {
addToSoftwareComponent("java")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ dependencies {
}

apollo {
useGradleVariants.set(true)

service("service1") {
packageName.set("com.service1")
generateApolloMetadata.set(true)
alwaysGenerateTypesMatching.add(".*")
outgoingVariantsConnection {
addToSoftwareComponent("java")
}
}
service("service2") {
packageName.set("com.service2")
generateApolloMetadata.set(true)
alwaysGenerateTypesMatching.add(".*")
outgoingVariantsConnection {
addToSoftwareComponent("java")
}
}
}

Expand Down

0 comments on commit db6272e

Please sign in to comment.