1+ /*
2+ * Copyright 2022 The Android Open Source Project
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ import com.google.devtools.ksp.gradle.KspExtension
18+ import org.gradle.api.Plugin
19+ import org.gradle.api.Project
20+ import org.gradle.api.artifacts.VersionCatalogsExtension
21+ import org.gradle.api.tasks.InputDirectory
22+ import org.gradle.api.tasks.PathSensitive
23+ import org.gradle.api.tasks.PathSensitivity
24+ import org.gradle.kotlin.dsl.configure
25+ import org.gradle.kotlin.dsl.dependencies
26+ import org.gradle.kotlin.dsl.getByType
27+ import org.gradle.process.CommandLineArgumentProvider
28+ import java.io.File
29+
30+ class AndroidRoomConventionPlugin : Plugin <Project > {
31+
32+ override fun apply (target : Project ) {
33+ with (target) {
34+ pluginManager.apply (" com.google.devtools.ksp" )
35+
36+ extensions.configure<KspExtension > {
37+ // The schemas directory contains a schema file for each version of the Room database.
38+ // This is required to enable Room auto migrations.
39+ // See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration.
40+ arg(RoomSchemaArgProvider (File (projectDir, " schemas" )))
41+ }
42+
43+ val libs = extensions.getByType<VersionCatalogsExtension >().named(" libs" )
44+ dependencies {
45+ add(" implementation" , libs.findLibrary(" room.runtime" ).get())
46+ add(" implementation" , libs.findLibrary(" room.ktx" ).get())
47+ add(" ksp" , libs.findLibrary(" room.compiler" ).get())
48+ }
49+ }
50+ }
51+
52+ /* *
53+ * https://issuetracker.google.com/issues/132245929
54+ * [Export schemas](https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schemas)
55+ */
56+ class RoomSchemaArgProvider (
57+ @get:InputDirectory
58+ @get:PathSensitive(PathSensitivity .RELATIVE )
59+ val schemaDir : File ,
60+ ) : CommandLineArgumentProvider {
61+ override fun asArguments () = listOf (" room.schemaLocation=${schemaDir.path} " )
62+ }
63+ }
0 commit comments