Skip to content

Commit 9628c00

Browse files
authored
[gradle-plugin] Remove checkApolloVersion (#6569)
* remove checkApolloVersion * remove obsolete tests
1 parent c03383c commit 9628c00

File tree

3 files changed

+1
-150
lines changed

3 files changed

+1
-150
lines changed

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

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import org.gradle.util.GradleVersion
4343
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
4444
import org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget
4545
import java.io.File
46-
import java.util.concurrent.Callable
4746
import javax.inject.Inject
4847

4948
abstract class DefaultApolloExtension(
@@ -52,7 +51,6 @@ abstract class DefaultApolloExtension(
5251

5352
private var codegenOnGradleSyncConfigured: Boolean = false
5453
private val services = mutableListOf<DefaultService>()
55-
private val checkVersionsTask: TaskProvider<Task>
5654
private val generateApolloSources: TaskProvider<Task>
5755
private var hasExplicitService = false
5856
private val adhocComponentWithVariants: AdhocComponentWithVariants by lazy {
@@ -150,8 +148,6 @@ abstract class DefaultApolloExtension(
150148
"apollo-kotlin requires Gradle version $MIN_GRADLE_VERSION or greater"
151149
}
152150

153-
checkVersionsTask = registerCheckVersionsTask()
154-
155151
/**
156152
* An aggregate task to easily generate all models
157153
*/
@@ -270,72 +266,6 @@ abstract class DefaultApolloExtension(
270266
}
271267
}
272268

273-
/**
274-
* Registers the `checkVersions` task.
275-
*
276-
* `checkVersions` ensures that all declared versions in a build are the same (plugins, direct dependencies but not transitive dependencies).
277-
* The main goal is to make sure that the generated code matches the `apollo-api` version as we historically do not provide compatibility guarantees.
278-
*
279-
* This code has some shortcomings:
280-
* 1. it is too restrictive. Most of the time, codegen x is compatible with runtime y as long as y >= x and the same major version.
281-
* 2. it doesn't work with transitive dependencies. This is fine because Gradle by default uses the greatest version and because of 1. it works most of the time.
282-
* 3. it's a global check and there _could_ be scenarios where this is not desirable.
283-
*
284-
* All of this makes this check ill-defined, but it hasn't been too much of an issue so far, and it's a net gain to catch the plugin/runtime discrepancies that have happened in the past.
285-
*
286-
* If you're reading this because there has been an issue, there are several mitigations:
287-
*
288-
* ## Disabling the task
289-
*
290-
* This is the most immediate and easy solution:
291-
*
292-
* ```kotlin
293-
* tasks.named("checkApolloVersions").configure {enabled = false}
294-
* ```
295-
* ## runtime check
296-
*
297-
* More involved but more correct, check at runtime that the versions match. Requires adding the codegen version in generated sources:
298-
*
299-
* - a new field in [com.apollographql.apollo.api.Operation].
300-
* - or binding an [com.apollographql.apollo.ApolloClient] to a given schema (could be useful for other purposes as well such as schema testing).
301-
*
302-
* ## automatically add the `apollo-api` dependency
303-
*
304-
* That would have the effect of making sure a compatible `apollo-api` is in the classpath. But won't help if `apollo-runtime` is wrong.
305-
*
306-
* All in all, the current solution works but if it becomes an issue, do not hesitate to revisit it.
307-
*/
308-
// Gradle will consider the task never UP-TO-DATE if we pass a lambda to doLast()
309-
@Suppress("ObjectLiteralToLambda")
310-
private fun registerCheckVersionsTask(): TaskProvider<Task> {
311-
return project.tasks.register(ModelNames.checkApolloVersions()) {
312-
val outputFile = BuildDirLayout.versionCheck(project)
313-
314-
it.inputs.property("allVersions", Callable {
315-
val allDeps = (
316-
getDeps(project.buildscript.configurations) +
317-
getDeps(project.configurations)
318-
)
319-
allDeps.distinct().sorted()
320-
})
321-
it.outputs.file(outputFile)
322-
323-
it.doLast(object : Action<Task> {
324-
override fun execute(t: Task) {
325-
val allVersions = it.inputs.properties["allVersions"] as List<*>
326-
327-
check(allVersions.size <= 1) {
328-
"Apollo: All apollo versions should be the same. Found:\n$allVersions"
329-
}
330-
331-
val version = allVersions.firstOrNull()
332-
333-
outputFile.get().asFile.writeText("All versions are consistent: $version")
334-
}
335-
})
336-
}
337-
}
338-
339269
class Configurations(
340270
val consumable: Configuration,
341271
val resolvable: Configuration,
@@ -755,10 +685,6 @@ abstract class DefaultApolloExtension(
755685
}
756686
service.outputDirAction!!.execute(directoryConnection)
757687

758-
directoryConnection.task.configure {
759-
it.dependsOn(checkVersionsTask)
760-
}
761-
762688
if (dataBuildersSourcesBaseTaskProvider != null) {
763689
val dataBuildersDirectoryConnection = DefaultDirectoryConnection(
764690
project = project,
@@ -937,31 +863,6 @@ abstract class DefaultApolloExtension(
937863
// Keep in sync gradle-api-min
938864
const val MIN_GRADLE_VERSION = "8.0"
939865

940-
private fun getDeps(configurations: ConfigurationContainer): List<String> {
941-
// See https://github.com/apollographql/apollo-kotlin/pull/5657
942-
val currentConfigurations = configurations.toList()
943-
return currentConfigurations.flatMap { configuration ->
944-
configuration.dependencies
945-
.filter {
946-
/**
947-
* When using plugins {}, the group is the plugin id, not the maven group
948-
*
949-
* the "_" check is for refreshVersions,
950-
* see https://github.com/jmfayard/refreshVersions/issues/507
951-
*
952-
* Note: we only check external dependencies as reading the group of project dependencies
953-
* is not compatible with isolated projects
954-
*
955-
*/
956-
it is ExternalModuleDependency
957-
&& it.group in listOf("com.apollographql.apollo", "com.apollographql.apollo.external")
958-
&& it.version != "_"
959-
}.mapNotNull { dependency ->
960-
dependency.version
961-
}
962-
}
963-
}
964-
965866
// Don't use `graphqlSourceDirectorySet.isEmpty` here, it doesn't work for some reason
966867
private val SourceDirectorySet.isReallyEmpty
967868
get() = sourceDirectories.isEmpty

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ internal object ModelNames {
2727
fun downloadApolloSchemaRegistry(service: Service) = camelCase("download", service.name, "ApolloSchemaFromRegistry")
2828
fun registerApolloOperations(service: Service) = camelCase("register", service.name, "ApolloOperations")
2929
fun pushApolloSchema() = camelCase("pushApolloSchema")
30-
fun checkApolloVersions() = "checkApolloVersions"
3130
fun convertApolloSchema() = "convertApolloSchema"
3231

3332
fun scopeConfiguration(

libraries/apollo-gradle-plugin/src/test/kotlin/test/ServiceTests.kt

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -152,56 +152,7 @@ class ServiceTests {
152152
assertEquals(TaskOutcome.SUCCESS, result.task(":generateApolloSources")!!.outcome)
153153
}
154154
}
155-
156-
@Test
157-
fun `versions are enforced`() {
158-
withSimpleProject { dir ->
159-
File(dir, "build.gradle").replaceInText("libs.apollo.api", "\"com.apollographql.apollo:apollo-api:1.2.0\"")
160-
161-
var exception: Exception? = null
162-
try {
163-
TestUtils.executeTask("generateApolloSources", dir)
164-
} catch (e: UnexpectedBuildFailure) {
165-
exception = e
166-
Truth.assertThat(e.message).contains("All apollo versions should be the same")
167-
}
168-
assertNotNull(exception)
169-
}
170-
}
171-
172-
@Test
173-
fun `changing a dependency checks versions again`() {
174-
withSimpleProject { dir ->
175-
176-
TestUtils.executeTaskAndAssertSuccess(":generateApolloSources", dir)
177-
178-
val result = TestUtils.executeTask("checkApolloVersions", dir)
179-
assert(result.task(":checkApolloVersions")?.outcome == TaskOutcome.UP_TO_DATE)
180-
181-
File(dir, "build.gradle").replaceInText("libs.apollo.api", "\"com.apollographql.apollo:apollo-api:1.2.0\"")
182-
183-
try {
184-
TestUtils.executeTask("checkApolloVersions", dir)
185-
fail("An exception was expected")
186-
} catch (e: UnexpectedBuildFailure) {
187-
Truth.assertThat(e.message).contains("All apollo versions should be the same")
188-
}
189-
}
190-
}
191-
192-
@Test
193-
fun `versions are enforced even in rootProject`() {
194-
withTestProject("mismatchedVersions") { dir ->
195-
var exception: Exception? = null
196-
try {
197-
TestUtils.executeTask("generateApolloSources", dir)
198-
} catch (e: UnexpectedBuildFailure) {
199-
exception = e
200-
Truth.assertThat(e.message).contains("All apollo versions should be the same")
201-
}
202-
assertNotNull(exception)
203-
}
204-
}
155+
205156

206157
@Test
207158
fun `dependencies are using the plugin version by default`() {

0 commit comments

Comments
 (0)