diff --git a/build-logic/src/main/kotlin/lib.kt b/build-logic/src/main/kotlin/lib.kt new file mode 100644 index 00000000..7920d03c --- /dev/null +++ b/build-logic/src/main/kotlin/lib.kt @@ -0,0 +1,17 @@ +import com.gradleup.librarian.gradle.Librarian +import org.gradle.api.Project +import org.gradle.api.publish.PublishingExtension +import java.net.URI + +fun Project.lib() { + Librarian.module(this) + + extensions.findByType(PublishingExtension::class.java)?.apply { + repositories { + maven { + name = "local" + url = URI("file://" + rootProject.layout.buildDirectory.dir("m2").get().asFile.path) + } + } + } +} \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/build.gradle.kts b/normalized-cache-apollo-compiler-plugin/build.gradle.kts index f45b9dab..f82f844c 100644 --- a/normalized-cache-apollo-compiler-plugin/build.gradle.kts +++ b/normalized-cache-apollo-compiler-plugin/build.gradle.kts @@ -4,7 +4,7 @@ plugins { id("org.jetbrains.kotlin.jvm") } -Librarian.module(project) +lib() dependencies { compileOnly(libs.apollo.compiler) @@ -14,3 +14,8 @@ dependencies { implementation(libs.kotlin.poet) testImplementation(libs.kotlin.test) } + +tasks.withType(Test::class.java).configureEach { + dependsOn("publishAllPublicationsToLocalRepository") + dependsOn(":normalized-cache:publishAllPublicationsToLocalRepository") +} \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaCodeGenerator.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaCodeGenerator.kt index 505a600a..73da6871 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaCodeGenerator.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaCodeGenerator.kt @@ -6,7 +6,9 @@ import com.apollographql.apollo.annotations.ApolloExperimental import com.apollographql.apollo.ast.GQLDocument import com.apollographql.apollo.ast.Schema import com.apollographql.apollo.ast.toSchema +import com.apollographql.apollo.compiler.ApolloCompiler import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment +import com.apollographql.apollo.compiler.ApolloCompilerPluginLogger import com.apollographql.apollo.compiler.SchemaCodeGenerator import com.apollographql.cache.apollocompilerplugin.VERSION import com.squareup.kotlinpoet.ClassName @@ -58,8 +60,30 @@ internal class CacheSchemaCodeGenerator( file.writeTo(outputDirectory) } + private fun ApolloCompilerPluginEnvironment.logger(): ApolloCompiler.Logger { + val method = this::class.java.methods.first { it.name == "getLogger" } + if (method.returnType.name == "com.apollographql.apollo.compiler.ApolloCompiler\$Logger") { + /** + * The code is running on v5 where logger was converted to return directly ApolloCompiler.Logger. + * + * See https://github.com/apollographql/apollo-kotlin-normalized-cache/pull/178 + */ + return method.invoke(this) as ApolloCompiler.Logger + } + + return logger.toApolloCompilerLogger() + } + + private fun ApolloCompilerPluginLogger.toApolloCompilerLogger(): ApolloCompiler.Logger { + return object : ApolloCompiler.Logger { + override fun warning(message: String) { + return this@toApolloCompilerLogger.error(message) + } + } + } + private fun maxAgeProperty(schema: Schema): PropertySpec { - val maxAges = schema.getMaxAges(environment.logger) + val maxAges = schema.getMaxAges(environment.logger()) val initializer = CodeBlock.builder().apply { add("mapOf(\n") withIndent { diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/getMaxAges.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/getMaxAges.kt index 5c7ce88f..e9b2daca 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/getMaxAges.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/getMaxAges.kt @@ -11,10 +11,12 @@ import com.apollographql.apollo.ast.GQLTypeDefinition import com.apollographql.apollo.ast.Schema import com.apollographql.apollo.ast.SourceLocation import com.apollographql.apollo.ast.pretty +import com.apollographql.apollo.compiler.ApolloCompiler import com.apollographql.apollo.compiler.ApolloCompilerPluginLogger +import java.lang.reflect.Method @OptIn(ApolloExperimental::class) -internal fun Schema.getMaxAges(logger: ApolloCompilerPluginLogger): Map { +internal fun Schema.getMaxAges(logger: ApolloCompiler.Logger): Map { var hasIssues = false val typeDefinitions = this.typeDefinitions fun GQLDirective.maxAgeAndInherit(): Pair { @@ -83,9 +85,20 @@ internal fun Schema.getMaxAges(logger: ApolloCompilerPluginLogger): Map Unit): File { + val workingDir = File("build/testProject") + + workingDir.deleteRecursively() + File("test-data/testProject").copyRecursively(workingDir) + block(workingDir) + return workingDir +} + +private fun File.addBadCacheControl() { + resolve("src/main/graphql/schema.graphqls").replace("# DIRECTIVE_PLACEHOLDER", "@cacheControl(maxAge: -10)") +} + +private fun File.version(version: String) { + resolve("build.gradle.kts").replace("4.2.0", version) +} +private fun File.assertFailure(taskName: String, onFailure: (UnexpectedBuildFailure) -> Unit) { + newRunner().assertFailure(taskName, onFailure) +} + +private fun File.assertSuccess(taskName: String) { + newRunner().assertSuccess(taskName) +} + +private fun File.newRunner(): GradleRunner { + return GradleRunner.create() + .withProjectDir(this) +} + +private fun GradleRunner.assertFailure(taskName: String, onFailure: (UnexpectedBuildFailure) -> Unit) { + try { + withArguments(taskName) + .build() + } catch (e: UnexpectedBuildFailure) { + onFailure(e) + } +} + +private fun GradleRunner.assertSuccess(taskName: String) { + withArguments(taskName) + .build().apply { + assertEquals(TaskOutcome.SUCCESS, task(":$taskName")!!.outcome) + } +} + +private fun File.replace(str: String, replacement: String) { + writeText(readText().replace(str, replacement)) } \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/GetMaxAgesTest.kt b/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/GetMaxAgesTest.kt index 93858946..79bfbd6c 100644 --- a/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/GetMaxAgesTest.kt +++ b/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/GetMaxAgesTest.kt @@ -8,6 +8,8 @@ import com.apollographql.apollo.ast.builtinForeignSchemas import com.apollographql.apollo.ast.internal.SchemaValidationOptions import com.apollographql.apollo.ast.parseAsGQLDocument import com.apollographql.apollo.ast.validateAsSchema +import com.apollographql.apollo.compiler.ApolloCompiler +import com.apollographql.apollo.compiler.ApolloCompilerPlugin import com.apollographql.apollo.compiler.ApolloCompilerPluginLogger import kotlin.test.Test import kotlin.test.assertEquals @@ -70,20 +72,8 @@ class GetMaxAgesTest { ) .getOrThrow() .getMaxAges( - object : ApolloCompilerPluginLogger { - override fun error(message: String) { - fail() - } - - override fun info(message: String) { - fail() - } - - override fun logging(message: String) { - fail() - } - - override fun warn(message: String) { + object : ApolloCompiler.Logger { + override fun warning(message: String) { fail() } } @@ -152,23 +142,11 @@ class GetMaxAgesTest { ) .getOrThrow() .getMaxAges( - object : ApolloCompilerPluginLogger { - override fun error(message: String) { - errors += message - } - - override fun info(message: String) { - fail() - } - - override fun logging(message: String) { - fail() - } - - override fun warn(message: String) { - fail() - } + object : ApolloCompiler.Logger { + override fun warning(message: String) { + errors += message } + } ) } val expectedErrors = listOf( diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/build.gradle.kts b/normalized-cache-apollo-compiler-plugin/test-data/testProject/build.gradle.kts index b7d37123..a5ec379c 100644 --- a/normalized-cache-apollo-compiler-plugin/test-data/testProject/build.gradle.kts +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/build.gradle.kts @@ -6,8 +6,16 @@ plugins { apollo { service("service") { packageName.set("com.example") - plugin("com.apollographql.cache:normalized-cache-apollo-compiler-plugin") { + // We use + as a version here to avoid having to share the version with the main build + // There is also an exclusiveContent filter installed to make sure we resolve the local artifacts + plugin("com.apollographql.cache:normalized-cache-apollo-compiler-plugin:+") { argument("packageName", packageName.get()) } } } + +dependencies { + implementation("com.apollographql.apollo:apollo-api") + implementation("com.apollographql.cache:normalized-cache:+") + testImplementation(libs.kotlin.test) +} \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/settings.gradle.kts b/normalized-cache-apollo-compiler-plugin/test-data/testProject/settings.gradle.kts index ffb34c0b..df2ba1f0 100644 --- a/normalized-cache-apollo-compiler-plugin/test-data/testProject/settings.gradle.kts +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/settings.gradle.kts @@ -1,3 +1,16 @@ -apply(from = "gradle/repositories.gradle.kts") +dependencyResolutionManagement { + repositories { + exclusiveContent { + // Make sure the cache artifacts are the ones from the local maven repo + forRepository{ + maven("../../../build/m2") + } + filter { + includeGroup("com.apollographql.cache") + } + } + + mavenCentral() + } +} -includeBuild("../../..") \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/schema.graphqls b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/schema.graphqls index 7c1bb33f..3a6ec7d4 100644 --- a/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/schema.graphqls +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/schema.graphqls @@ -1,9 +1,14 @@ +extend schema @link(url: "https://specs.apollo.dev/cache/v0.1/", import: ["@cacheControl"]) + type Query { product: Product foo: Int } -type Product @typePolicy(keyFields: "id"){ +type Product + @typePolicy(keyFields: "id") + # DIRECTIVE_PLACEHOLDER +{ id: ID! price: Float } \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/test/kotlin/MainTest.kt b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/test/kotlin/MainTest.kt new file mode 100644 index 00000000..5d6936c1 --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/test/kotlin/MainTest.kt @@ -0,0 +1,10 @@ +import com.example.cache.Cache +import kotlin.test.Test +import kotlin.test.assertEquals + +class MainTest { + @Test + fun mainTest() { + assertEquals("id", Cache.typePolicies.get("Product")?.keyFields?.single()) + } +} \ No newline at end of file diff --git a/normalized-cache-sqlite/build.gradle.kts b/normalized-cache-sqlite/build.gradle.kts index 23acae2a..01371899 100644 --- a/normalized-cache-sqlite/build.gradle.kts +++ b/normalized-cache-sqlite/build.gradle.kts @@ -6,7 +6,7 @@ plugins { id("app.cash.sqldelight") } -Librarian.module(project) +lib() kotlin { configureKmp( diff --git a/normalized-cache/build.gradle.kts b/normalized-cache/build.gradle.kts index 69fafb8d..72d5cfed 100644 --- a/normalized-cache/build.gradle.kts +++ b/normalized-cache/build.gradle.kts @@ -1,11 +1,9 @@ -import com.gradleup.librarian.gradle.Librarian - plugins { id("org.jetbrains.kotlin.multiplatform") id("org.jetbrains.kotlin.plugin.atomicfu") } -Librarian.module(project) +lib() kotlin { configureKmp(