1/*
2 * Copyright (C) 2023 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 *      http://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
17import androidx.build.KmpPlatformsKt
18import org.apache.commons.io.FileUtils
19import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
20import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink
21import org.jetbrains.kotlin.konan.target.Family
22
23plugins {
24    id("AndroidXPlugin")
25    id("com.android.library")
26    id("com.google.devtools.ksp")
27    id("androidx.room")
28    alias(libs.plugins.kotlinSerialization)
29}
30
31androidXMultiplatform {
32    androidTarget()
33    ios()
34    jvm()
35    linux()
36    mac()
37    sourceSets {
38        commonTest {
39            dependencies {
40                implementation(libs.kotlinStdlib)
41                implementation(project(":room:room-runtime"))
42                implementation(project(":room:room-testing"))
43                implementation(project(":room:room-paging"))
44                implementation(project(":sqlite:sqlite-bundled"))
45                implementation(project(":kruth:kruth"))
46                implementation(project(":paging:paging-common"))
47                implementation(libs.kotlinTest)
48                implementation(libs.kotlinCoroutinesTest)
49                implementation(libs.kotlinSerializationCore)
50            }
51        }
52        androidInstrumentedTest {
53            dependsOn(commonTest)
54            dependencies {
55                implementation(libs.kotlinTestJunit)
56                implementation(libs.testRunner)
57                implementation(libs.testCore)
58            }
59        }
60        jvmTest {
61            dependsOn(commonTest)
62            dependencies {
63                implementation(libs.kotlinTestJunit)
64            }
65        }
66        nativeTest {
67            dependsOn(commonTest)
68        }
69        nonIosNativeTest {
70            dependsOn(nativeTest)
71        }
72        if (KmpPlatformsKt.enableMac(project)) {
73            iosTest {
74                dependsOn(nativeTest)
75            }
76        }
77        targets.configureEach { target ->
78            if (target.platformType == KotlinPlatformType.native) {
79                def test = target.compilations["test"]
80                if (target.konanTarget.family == Family.IOS) {
81                    test.defaultSourceSet {
82                        dependsOn(iosTest)
83                    }
84                } else {
85                    test.defaultSourceSet {
86                        dependsOn(nonIosNativeTest)
87                    }
88                }
89            }
90        }
91    }
92}
93
94// TODO(b/325111583): Create a helper function to configure KSP with KMP targets
95dependencies {
96    def roomCompilerDependency = project(":room:room-compiler")
97    add("kspAndroidAndroidTest", roomCompilerDependency) // Android instrumentation test
98    add("kspAndroidTest", roomCompilerDependency) // Android unit test
99    add("kspJvmTest", roomCompilerDependency)
100    add("kspLinuxX64Test", roomCompilerDependency)
101    if (KmpPlatformsKt.enableMac(project)) {
102        add("kspIosSimulatorArm64Test", roomCompilerDependency)
103        add("kspIosX64Test", roomCompilerDependency)
104        add("kspMacosX64Test", roomCompilerDependency)
105        add("kspMacosArm64Test", roomCompilerDependency)
106    }
107}
108
109android {
110    namespace = "androidx.room.integration.multiplatformtestapp"
111}
112
113// Copy schema files to the iOS binary output test directory that will be part of the bundle's
114// resources and read with NSBundle. This needs to be replaced with a more proper mechanism.
115// TODO(b/317909626): Should be configured by Room Gradle Plugin
116tasks.withType(KotlinNativeLink).configureEach { task ->
117    if (name.contains("linkDebugTestIos")) {
118        def inputSchemaDir = layout.projectDirectory.dir("schemas-ksp").getAsFile()
119        def outputSchemaDir = new File(task.destinationDirectory.getAsFile().get(), "/schemas-ksp")
120        task.doLast {
121            FileUtils.copyDirectory(inputSchemaDir, outputSchemaDir)
122        }
123    }
124}
125
126room {
127    schemaDirectory(
128            provider { layout.projectDirectory.dir("schemas-ksp").getAsFile().getAbsolutePath() }
129    )
130}
131
132androidx {
133}
134