Skip to content

Commit

Permalink
Merge pull request #160 from teogor/feature/add-room-schema-arg-provider
Browse files Browse the repository at this point in the history
Enhance Room Plugin with Schema Export Support
  • Loading branch information
teogor authored Oct 27, 2023
2 parents 5a0ee3a + 472700f commit f47499a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ 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")

// Feature :: About
alias(libs.plugins.about.libraries) apply true
}

roomOptions {
enableSchemaProvider = true
}

android {
namespace = "dev.teogor.ceres"
defaultConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@
* limitations under the License.
*/

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
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.create
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType
import org.gradle.process.CommandLineArgumentProvider
import java.io.File

class AndroidRoomConventionPlugin : Plugin<Project> {

override fun apply(target: Project) {
val roomOptions = target.extensions.create<RoomOptionsExtension>(
name = "roomOptions",
)

with(target) {
pluginManager.apply("com.google.devtools.ksp")

// extensions.configure<KspExtension> {
// // 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<KspExtension> {
// 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<VersionCatalogsExtension>().named("libs")
dependencies {
Expand All @@ -56,6 +68,17 @@ class AndroidRoomConventionPlugin : Plugin<Project> {
@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}")
}
}
Original file line number Diff line number Diff line change
@@ -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",
)

0 comments on commit f47499a

Please sign in to comment.