Skip to content

Commit db6272e

Browse files
authored
[gradle-plugin] expose the outgoing variants (#6329)
* Directly expose the outgoing variants * Add @ApolloExperimental * Fix KDoc
1 parent bf2f7b8 commit db6272e

File tree

6 files changed

+78
-35
lines changed

6 files changed

+78
-35
lines changed

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo/gradle/api/ApolloExtension.kt

-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ interface ApolloExtension {
9090
*/
9191
val generateSourcesDuringGradleSync: Property<Boolean>
9292

93-
@ApolloExperimental
94-
val useGradleVariants: Property<Boolean>
95-
9693
/**
9794
* Common apollo dependencies using the same version as the Apollo Gradle Plugin currently in the classpath
9895
*/

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo/gradle/api/Service.kt

+30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.apollographql.apollo.compiler.OperationOutputGenerator
1111
import com.apollographql.apollo.compiler.PackageNameGenerator
1212
import org.gradle.api.Action
1313
import org.gradle.api.Task
14+
import org.gradle.api.artifacts.Configuration
15+
import org.gradle.api.component.SoftwareComponent
1416
import org.gradle.api.file.ConfigurableFileCollection
1517
import org.gradle.api.file.Directory
1618
import org.gradle.api.file.DirectoryProperty
@@ -914,4 +916,32 @@ interface Service {
914916

915917
@ApolloExperimental
916918
fun plugin(dependencyNotation: Any, block: Action<CompilerPlugin>)
919+
920+
/**
921+
* Overrides the way the outgoing variants are connected.
922+
* Use this to publish the metadata to an already existing software component.
923+
*
924+
* By default, a new software component named "apollo" is created.
925+
*
926+
* ```kotlin
927+
* service("service") {
928+
* outgoingVariantsConnection {
929+
* addToSoftwareComponent("java")
930+
* }
931+
* }
932+
* ```
933+
*/
934+
@ApolloExperimental
935+
fun outgoingVariantsConnection(action: Action<in OutgoingVariantsConnection>)
936+
937+
/**
938+
* An [OutgoingVariantsConnection] defines how outgoing variants are added to software components.
939+
*/
940+
@ApolloExperimental
941+
interface OutgoingVariantsConnection {
942+
fun addToSoftwareComponent(name: String)
943+
fun addToSoftwareComponent(softwareComponent: SoftwareComponent)
944+
945+
val outgoingVariants: List<Configuration>
946+
}
917947
}

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo/gradle/internal/DefaultApolloExtension.kt

+26-27
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.gradle.api.artifacts.ProjectDependency
2424
import org.gradle.api.attributes.HasConfigurableAttributes
2525
import org.gradle.api.attributes.Usage
2626
import org.gradle.api.component.AdhocComponentWithVariants
27+
import org.gradle.api.component.SoftwareComponent
2728
import org.gradle.api.component.SoftwareComponentFactory
2829
import org.gradle.api.file.FileCollection
2930
import org.gradle.api.file.SourceDirectorySet
@@ -35,7 +36,6 @@ import org.gradle.util.GradleVersion
3536
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
3637
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
3738
import java.io.File
38-
import java.lang.reflect.Field
3939
import java.util.concurrent.Callable
4040
import javax.inject.Inject
4141

@@ -50,7 +50,9 @@ abstract class DefaultApolloExtension(
5050
private val generateApolloSources: TaskProvider<Task>
5151
private var hasExplicitService = false
5252
private val adhocComponentWithVariants: AdhocComponentWithVariants by lazy {
53-
project.adhocComponentWithVariants()
53+
softwareComponentFactory.adhoc("apollo").also {
54+
project.components.add(it)
55+
}
5456
}
5557
private val apolloMetadataConfiguration: Configuration
5658
private var apolloBuildServiceProvider: Provider<ApolloBuildService>
@@ -576,10 +578,28 @@ abstract class DefaultApolloExtension(
576578
* - downstream module publishes jar
577579
* In such scenarios, the user must set `alwaysGenerateTypesMatching.set(listOf(".*"))` on the schema module.
578580
*/
579-
adhocComponentWithVariants.addVariantsFromConfiguration(codegenMetadata.consumable) {}
580-
adhocComponentWithVariants.addVariantsFromConfiguration(upstreamIr.consumable) {}
581-
adhocComponentWithVariants.addVariantsFromConfiguration(codegenSchema.consumable) {}
582-
adhocComponentWithVariants.addVariantsFromConfiguration(otherOptions.consumable) {}
581+
val outgoingVariantsConnection = object : Service.OutgoingVariantsConnection {
582+
override fun addToSoftwareComponent(name: String) {
583+
addToSoftwareComponent(project.components.getByName(name))
584+
}
585+
586+
override fun addToSoftwareComponent(softwareComponent: SoftwareComponent) {
587+
check (softwareComponent is AdhocComponentWithVariants) {
588+
"Software component '$softwareComponent' is not an instance of AdhocComponentWithVariants"
589+
}
590+
outgoingVariants.forEach {
591+
softwareComponent.addVariantsFromConfiguration(it) { }
592+
}
593+
}
594+
595+
override val outgoingVariants: List<Configuration>
596+
get() = listOf(codegenMetadata.consumable, upstreamIr.consumable, codegenSchema.consumable, otherOptions.consumable)
597+
}
598+
if (service.outgoingVariantsConnection != null) {
599+
service.outgoingVariantsConnection!!.execute(outgoingVariantsConnection)
600+
} else {
601+
outgoingVariantsConnection.addToSoftwareComponent(adhocComponentWithVariants)
602+
}
583603

584604
service.upstreamDependencies.forEach {
585605
otherOptions.scope.dependencies.add(it)
@@ -1035,27 +1055,6 @@ abstract class DefaultApolloExtension(
10351055
}
10361056

10371057
override val deps: ApolloDependencies = ApolloDependencies(project.dependencies)
1038-
1039-
private fun Project.adhocComponentWithVariants(): AdhocComponentWithVariants {
1040-
if (useGradleVariants.getOrElse(false)) {
1041-
val javaComponent = project.components.findByName("java")
1042-
if (javaComponent != null) {
1043-
// JVM
1044-
return javaComponent as AdhocComponentWithVariants
1045-
}
1046-
1047-
val kotlin = project.kotlinMultiplatformExtension
1048-
if (kotlin != null) {
1049-
error("Adding variants to multiplatform project is not possible. See https://youtrack.jetbrains.com/issue/KT-58830/")
1050-
}
1051-
1052-
error("Impossible to find an AdHocComponent")
1053-
} else {
1054-
return softwareComponentFactory.adhoc("apollo").also {
1055-
project.components.add(it)
1056-
}
1057-
}
1058-
}
10591058
}
10601059

10611060

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo/gradle/internal/DefaultService.kt

+10-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ abstract class DefaultService @Inject constructor(val project: Project, override
120120

121121
var operationOutputAction: Action<in Service.OperationOutputConnection>? = null
122122
var operationManifestAction: Action<in Service.OperationManifestConnection>? = null
123+
var outgoingVariantsConnection: Action<in Service.OutgoingVariantsConnection>? = null
123124

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

134135
override fun operationManifestConnection(action: Action<in Service.OperationManifestConnection>) {
135136
check(!registered) {
136-
"Apollo: operationOutputConnection {} cannot be configured outside of a service {} block"
137+
"Apollo: operationManifestConnection {} cannot be configured outside of a service {} block"
137138
}
138139

139140
this.operationManifestAction = action
140141
}
141142

143+
override fun outgoingVariantsConnection(action: Action<in Service.OutgoingVariantsConnection>) {
144+
check(!registered) {
145+
"Apollo: outgoingVariantsConnection {} cannot be configured outside of a service {} block"
146+
}
147+
148+
this.outgoingVariantsConnection = action
149+
}
150+
142151
var outputDirAction: Action<in Service.DirectoryConnection>? = null
143152

144153
override fun outputDirConnection(action: Action<in Service.DirectoryConnection>) {

libraries/apollo-gradle-plugin/testProjects/multi-modules-publishing-producer/fragments/build.gradle.kts

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ dependencies {
1313
}
1414

1515
apollo {
16-
useGradleVariants.set(true)
17-
1816
service("service1") {
1917
packageName.set("com.service1")
2018
dependsOn(project(":schema"))
19+
outgoingVariantsConnection {
20+
addToSoftwareComponent("java")
21+
}
2122
}
2223
service("service2") {
2324
packageName.set("com.service2")
2425
dependsOn(project(":schema"))
26+
outgoingVariantsConnection {
27+
addToSoftwareComponent("java")
28+
}
2529
}
2630
}
2731

libraries/apollo-gradle-plugin/testProjects/multi-modules-publishing-producer/schema/build.gradle.kts

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ dependencies {
1313
}
1414

1515
apollo {
16-
useGradleVariants.set(true)
17-
1816
service("service1") {
1917
packageName.set("com.service1")
2018
generateApolloMetadata.set(true)
2119
alwaysGenerateTypesMatching.add(".*")
20+
outgoingVariantsConnection {
21+
addToSoftwareComponent("java")
22+
}
2223
}
2324
service("service2") {
2425
packageName.set("com.service2")
2526
generateApolloMetadata.set(true)
2627
alwaysGenerateTypesMatching.add(".*")
28+
outgoingVariantsConnection {
29+
addToSoftwareComponent("java")
30+
}
2731
}
2832
}
2933

0 commit comments

Comments
 (0)