Skip to content
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

Check minimum Kotlin version in the plugin #3475

Merged
merged 2 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Here's the current matrix of supported features per platform:

## Requirements

Apollo Android runs on the following platforms:
Some platforms have specific requirements:

* Android API level 15+
* JDK 8+
Expand Down
2 changes: 1 addition & 1 deletion apollo-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fun addShadowImplementation(dependency: Dependency) {
dependencies {
compileOnly(groovy.util.Eval.x(project, "x.dep.minGradleApi"))
//compileOnly(groovy.util.Eval.x(project, "x.dep.gradleApi"))
compileOnly(groovy.util.Eval.x(project, "x.dep.kotlin.plugin"))
compileOnly(groovy.util.Eval.x(project, "x.dep.kotlinPluginMin"))
compileOnly(groovy.util.Eval.x(project, "x.dep.android.minPlugin"))

addShadowImplementation(projects.apolloCompiler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ abstract class ApolloGenerateSourcesTask : DefaultTask() {
"1.5" -> TargetLanguage.KOTLIN_1_5
null -> {
// User didn't specify a version: defaults to the Kotlin plugin's version
val majorMinor = project.getKotlinPluginVersion().take(3)
val majorMinor = project.getKotlinPluginVersion()!!.take(3)
if (majorMinor == "1.4") {
TargetLanguage.KOTLIN_1_4
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import org.gradle.api.provider.Property
import org.gradle.api.tasks.TaskProvider
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation
import java.io.File

Expand All @@ -47,6 +49,24 @@ abstract class DefaultApolloExtension(
"apollo-android requires Gradle version $MIN_GRADLE_VERSION or greater"
}

project.plugins.all {
if (it is KotlinBasePluginWrapper) {
val version = project.getKotlinPluginVersion()!!
.split(".")
.take(2)
.map { it.toInt() }

val isKotlinSupported = when {
version[0] > 1 -> true
version[0] == 1 -> version[1] >= 4
else -> false
}
require(isKotlinSupported) {
"Apollo Android requires Kotlin plugin version 1.4 or more (found '${project.getKotlinPluginVersion()}')"
}
}
}
KotlinVersion.CURRENT
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
apolloConfiguration = project.configurations.create(ModelNames.apolloConfiguration()) {
it.isCanBeConsumed = false
it.isCanBeResolved = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "org.jetbrains.kotlin.jvm") {
useModule(groovy.util.Eval.x(extra, "x.dep.kotlin.plugin"))
useModule(groovy.util.Eval.x(extra, "x.dep.kotlinPlugin"))
}

if (requested.id.id == "com.apollographql.apollo3") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.apollographql.apollo3.gradle.test

import com.apollographql.apollo3.gradle.util.TestUtils
import com.google.common.truth.Truth
import junit.framework.Assert.fail
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.testkit.runner.UnexpectedBuildFailure
import org.junit.Test
import java.io.File

class KotlinPluginVersionTests {
private fun File.setKotlinPluginVersion(version: String) {
val gradleScript = resolve("build.gradle.kts")
gradleScript.writeText(gradleScript.readText().replace("KOTLIN_VERSION", version))
}

@Test
fun kotlin13Fails() {
TestUtils.withTestProject("kotlin-plugin-version") { dir ->
dir.setKotlinPluginVersion("1.3.0")
// Kotlin 1.3 uses DefaultSourceDirectorySet() which is removed in recent Gradle versions
try {
TestUtils.executeGradleWithVersion(dir, "5.6", "help")
fail("An exception was expected")
} catch (e: UnexpectedBuildFailure) {
Truth.assertThat(e.message).contains("Apollo Android requires Kotlin plugin version 1.4")
}
}
}

@Test
fun kotlin14Succeeds() {
TestUtils.withTestProject("kotlin-plugin-version") { dir ->
dir.setKotlinPluginVersion("1.4.32")
val result = TestUtils.executeTask("help", dir)

Truth.assertThat(result.task(":help")!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ import java.io.File
object TestUtils {
class Plugin(val artifact: String?, val id: String)

val javaPlugin = Plugin(id = "java", artifact = null)
val androidApplicationPlugin = Plugin(id = "com.android.application", artifact = "android.plugin")
val androidLibraryPlugin = Plugin(id = "com.android.library", artifact = "android.plugin")
val kotlinJvmPlugin = Plugin(id = "org.jetbrains.kotlin.jvm", artifact = "kotlin.plugin")
val kotlinAndroidPlugin = Plugin(id = "org.jetbrains.kotlin.android", artifact = "kotlin.plugin")
val kotlinJvmPlugin = Plugin(id = "org.jetbrains.kotlin.jvm", artifact = "kotlinPlugin")
val kotlinAndroidPlugin = Plugin(id = "org.jetbrains.kotlin.android", artifact = "kotlinPlugin")
val apolloPlugin = Plugin(id = "com.apollographql.apollo3", artifact = "apollo.plugin")
val apolloPluginAndroid = Plugin(id = "com.apollographql.android", artifact = "apollo.plugin")


fun withDirectory(block: (File) -> Unit) {
val dest = File(File(System.getProperty("user.dir")), "build/testProject")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ buildscript {
}
dependencies {
classpath(groovy.util.Eval.x(project, "x.dep.apollo.plugin"))
classpath(groovy.util.Eval.x(project, "x.dep.kotlin.plugin"))
classpath(groovy.util.Eval.x(project, "x.dep.kotlinPlugin"))
}
}
2 changes: 1 addition & 1 deletion apollo-gradle-plugin/testProjects/buildscript.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ project.buildscript.repositories {
}

project.buildscript.dependencies.apply {
add("classpath", groovy.util.Eval.x(project, "x.dep.kotlin.plugin"))
add("classpath", groovy.util.Eval.x(project, "x.dep.kotlinPlugin"))
add("classpath", groovy.util.Eval.x(project, "x.dep.apollo.plugin"))
add("classpath", groovy.util.Eval.x(project, "x.dep.android.plugin"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pluginManagement {
eachPlugin {
when (requested.id.id) {
"com.apollographql.apollo3" -> useModule(groovy.util.Eval.x(settings, "x.dep.apollo.plugin"))
"org.jetbrains.kotlin.jvm" -> useModule(groovy.util.Eval.x(settings, "x.dep.kotlin.plugin"))
"org.jetbrains.kotlin.jvm" -> useModule(groovy.util.Eval.x(settings, "x.dep.kotlinPlugin"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import com.apollographql.apollo3.gradle.api.ApolloExtension

buildscript {
apply(from = "../../../gradle/dependencies.gradle")

repositories {
maven {
url = uri("../../../build/localMaven")
}
mavenCentral()
}
dependencies {
classpath(groovy.util.Eval.x(project, "x.dep.apollo.plugin"))
// This project is run with Gradle 5.4 and Kotlin 1.5 doesn't support that so stick with 1.4.32
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:KOTLIN_VERSION")
}
}

apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "com.apollographql.apollo3")

repositories {
maven {
url = uri("../../../build/localMaven")
}
mavenCentral()
}

dependencies {
add("implementation", groovy.util.Eval.x(project, "x.dep.apollo.api"))
}

configure<ApolloExtension> {
packageName.set("com.example")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query GetRandom {
random
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
random: String
}
2 changes: 1 addition & 1 deletion benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
}
dependencies {
classpath(groovy.util.Eval.x(project, "x.dep.android.plugin"))
classpath(groovy.util.Eval.x(project, "x.dep.kotlin.plugin"))
classpath(groovy.util.Eval.x(project, "x.dep.kotlinPlugin"))
classpath(groovy.util.Eval.x(project, "x.dep.kspGradlePlugin"))

classpath("com.apollographql.apollo3:apollo-gradle-plugin:${properties.get("apolloVersion")}")
Expand Down
2 changes: 1 addition & 1 deletion build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
implementation(groovy.util.Eval.x(project, "x.dep.android.plugin"))
implementation(groovy.util.Eval.x(project, "x.dep.gradleJapiCmpPlugin"))
implementation(groovy.util.Eval.x(project, "x.dep.gradleMetalavaPlugin"))
implementation(groovy.util.Eval.x(project, "x.dep.kotlin.plugin"))
implementation(groovy.util.Eval.x(project, "x.dep.kotlinPlugin"))
implementation(groovy.util.Eval.x(project, "x.dep.kotlin.coroutines"))
implementation(groovy.util.Eval.x(project, "x.dep.sqldelight.plugin"))
implementation(groovy.util.Eval.x(project, "x.dep.gradlePublishPlugin"))
Expand Down
2 changes: 1 addition & 1 deletion build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id.startsWith("org.jetbrains.kotlin.jvm")) {
useModule(groovy.util.Eval.x(settings, "x.dep.kotlin.plugin"))
useModule(groovy.util.Eval.x(settings, "x.dep.kotlinPlugin"))
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def versions = [
jetbrainsAnnotations: '13.0',
junit : '4.13.2',
kotlin : '1.5.31',
kotlinPluginMin : '1.4.0',
kotlinCoroutines : '1.5.1',
moshi : '1.12.0',
moshix : '0.13.0',
Expand Down Expand Up @@ -58,7 +59,6 @@ ext.dep = [
junit : "junit:junit:$versions.junit",
kotlinJunit : "org.jetbrains.kotlin:kotlin-test", // the Kotlin plugin resolves the version
kotlin : [
plugin : "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin",
coroutines : "org.jetbrains.kotlinx:kotlinx-coroutines-core:$versions.kotlinCoroutines",
coroutinesRx2 : "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:$versions.kotlinCoroutines",
coroutinesRx3 : "org.jetbrains.kotlinx:kotlinx-coroutines-rx3:$versions.kotlinCoroutines",
Expand Down Expand Up @@ -122,7 +122,9 @@ ext.dep = [
minGradleApi : "dev.gradleplugins:gradle-api:5.6",
graphqlKotlin : "com.expediagroup:graphql-kotlin-spring-server:4.1.0",
testParameterInjector : "com.google.testparameterinjector:test-parameter-injector:1.4",
binaryCompatibilityValidator: "org.jetbrains.kotlinx:binary-compatibility-validator:0.8.0-RC"
binaryCompatibilityValidator: "org.jetbrains.kotlinx:binary-compatibility-validator:0.8.0-RC",
kotlinPluginMin: "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlinPluginMin",
kotlinPlugin: "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
]
ext.androidConfig = [
compileSdkVersion: 30,
Expand Down