From 658f22117da166bc9a447d4d06c4d3ca0858198b Mon Sep 17 00:00:00 2001 From: Teodor Grigor Date: Fri, 27 Oct 2023 11:25:36 +0300 Subject: [PATCH 1/3] Add RoomSchemaArgProvider for Exporting Room Schemas --- .../main/kotlin/AndroidRoomConventionPlugin.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/plugin/library-convention/src/main/kotlin/AndroidRoomConventionPlugin.kt b/plugin/library-convention/src/main/kotlin/AndroidRoomConventionPlugin.kt index e2d17b16..bafa6a69 100644 --- a/plugin/library-convention/src/main/kotlin/AndroidRoomConventionPlugin.kt +++ b/plugin/library-convention/src/main/kotlin/AndroidRoomConventionPlugin.kt @@ -14,16 +14,18 @@ * limitations under the License. */ +import com.google.devtools.ksp.gradle.KspExtension +import java.io.File import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.api.tasks.InputDirectory import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity +import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.getByType import org.gradle.process.CommandLineArgumentProvider -import java.io.File class AndroidRoomConventionPlugin : Plugin { @@ -31,12 +33,12 @@ class AndroidRoomConventionPlugin : Plugin { with(target) { pluginManager.apply("com.google.devtools.ksp") - // extensions.configure { - // // The schemas directory contains a schema file for each version of the Room database. - // // This is required to enable Room auto migrations. - // // See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration. - // arg(RoomSchemaArgProvider(File(projectDir, "schemas"))) - // } + extensions.configure { + // The schemas directory contains a schema file for each version of the Room database. + // This is required to enable Room auto migrations. + // See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration. + arg(RoomSchemaArgProvider(File(projectDir, "schemas"))) + } val libs = extensions.getByType().named("libs") dependencies { From 752f21bbef6ed7c4586722e301f1cf1b9dbc9f36 Mon Sep 17 00:00:00 2001 From: Teodor Grigor Date: Fri, 27 Oct 2023 11:59:25 +0300 Subject: [PATCH 2/3] Add RoomOptionsExtension for Schema Export Configuration --- .../kotlin/AndroidRoomConventionPlugin.kt | 31 ++++++++++++++++--- .../ceres/models/RoomOptionsExtension.kt | 14 +++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 plugin/library-convention/src/main/kotlin/dev/teogor/ceres/models/RoomOptionsExtension.kt diff --git a/plugin/library-convention/src/main/kotlin/AndroidRoomConventionPlugin.kt b/plugin/library-convention/src/main/kotlin/AndroidRoomConventionPlugin.kt index bafa6a69..1f75ca43 100644 --- a/plugin/library-convention/src/main/kotlin/AndroidRoomConventionPlugin.kt +++ b/plugin/library-convention/src/main/kotlin/AndroidRoomConventionPlugin.kt @@ -15,6 +15,7 @@ */ import com.google.devtools.ksp.gradle.KspExtension +import dev.teogor.ceres.models.RoomOptionsExtension import java.io.File import org.gradle.api.Plugin import org.gradle.api.Project @@ -23,6 +24,7 @@ import org.gradle.api.tasks.InputDirectory import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.kotlin.dsl.configure +import org.gradle.kotlin.dsl.create import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.getByType import org.gradle.process.CommandLineArgumentProvider @@ -30,14 +32,22 @@ import org.gradle.process.CommandLineArgumentProvider class AndroidRoomConventionPlugin : Plugin { override fun apply(target: Project) { + val roomOptions = target.extensions.create( + name = "roomOptions", + ) + with(target) { pluginManager.apply("com.google.devtools.ksp") - extensions.configure { - // The schemas directory contains a schema file for each version of the Room database. - // This is required to enable Room auto migrations. - // See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration. - arg(RoomSchemaArgProvider(File(projectDir, "schemas"))) + afterEvaluate { + extensions.configure { + // The schemas directory contains a schema file for each version of the Room database. + // This is required to enable Room auto migrations. + // See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration. + if(roomOptions.enableSchemaProvider) { + arg(RoomSchemaArgProvider(File(projectDir, roomOptions.schemasPath))) + } + } } val libs = extensions.getByType().named("libs") @@ -58,6 +68,17 @@ class AndroidRoomConventionPlugin : Plugin { @get:PathSensitive(PathSensitivity.RELATIVE) val schemaDir: File, ) : CommandLineArgumentProvider { + init { + if (!schemaDir.exists()) { + val created = schemaDir.mkdirs() + if (created) { + println("Created directory: ${schemaDir.absolutePath}") + } else { + println("Failed to create directory: ${schemaDir.absolutePath}") + } + } + } + override fun asArguments() = listOf("room.schemaLocation=${schemaDir.path}") } } diff --git a/plugin/library-convention/src/main/kotlin/dev/teogor/ceres/models/RoomOptionsExtension.kt b/plugin/library-convention/src/main/kotlin/dev/teogor/ceres/models/RoomOptionsExtension.kt new file mode 100644 index 00000000..1d12295a --- /dev/null +++ b/plugin/library-convention/src/main/kotlin/dev/teogor/ceres/models/RoomOptionsExtension.kt @@ -0,0 +1,14 @@ +package dev.teogor.ceres.models + +/** + * Extension class for configuring Room-related options. + * + * @property enableSchemaProvider Set to `true` to enable the RoomSchemaArgProvider. + * @property schemasPath The relative path to the directory containing Room database schema files. + * This path is used when configuring the RoomSchemaArgProvider and is relative + * to the projectDir. The default value is "schemas." + */ +open class RoomOptionsExtension( + var enableSchemaProvider: Boolean = false, + var schemasPath: String = "schemas", +) From 472700fcea381b27ddebf53a749da74f13cbc775 Mon Sep 17 00:00:00 2001 From: Teodor Grigor Date: Fri, 27 Oct 2023 11:59:42 +0300 Subject: [PATCH 3/3] added showcase of roomOptions --- app/build.gradle.kts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index a38c23aa..4b96c6dc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -22,6 +22,7 @@ plugins { id("dev.teogor.ceres.android.application.jacoco") id("dev.teogor.ceres.android.application.firebase") id("dev.teogor.ceres.android.hilt") + id("dev.teogor.ceres.android.room") id("kotlinx-serialization") id("jacoco") @@ -29,6 +30,10 @@ plugins { alias(libs.plugins.about.libraries) apply true } +roomOptions { + enableSchemaProvider = true +} + android { namespace = "dev.teogor.ceres" defaultConfig {