xref: /aosp_15_r20/external/ksp/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/TestProject.kt (revision af87fb4bb8e3042070d2a054e912924f599b22b7)
1 /*
<lambda>null2  * Copyright 2021 Google LLC
3  * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.google.devtools.ksp.gradle.testing
18 
19 import java.io.File
20 
21 /**
22  * Simple wrapper that represents a test project.
23  */
24 class TestProject(
25     val rootDir: File,
26     val testConfig: TestConfig
27 ) {
28     val processorModule = TestModule(
29         rootDir.resolve("processor")
30     ).also {
31         it.plugins.add(PluginDeclaration.kotlin("jvm", testConfig.kotlinBaseVersion))
32         it.dependencies.add(
33             DependencyDeclaration.artifact(
34                 "implementation",
35                 "com.google.devtools.ksp:symbol-processing-api:${testConfig.kspVersion}"
36             )
37         )
38         // add gradle-plugin test classpath as a dependency to be able to load processors.
39         testConfig.processorClasspath.split(File.pathSeparatorChar).forEach { path ->
40             it.dependencies.add(
41                 DependencyDeclaration.files("implementation", path.replace(File.separatorChar, '/'))
42             )
43         }
44     }
45 
46     val appModule = TestModule(
47         rootDir.resolve("app")
48     )
49 
50     fun writeFiles() {
51         writeBuildFile()
52         writeSettingsFile()
53         writeRootProperties()
54         appModule.writeBuildFile()
55         processorModule.writeBuildFile()
56     }
57 
58     private fun writeRootProperties() {
59         val contents = """
60 
61             kotlin.jvm.target.validation.mode=warning
62         """.trimIndent()
63         rootDir.resolve("gradle.properties").appendText(contents)
64     }
65 
66     private fun writeSettingsFile() {
67         val contents = """
68                 include("processor")
69                 include("app")
70                 pluginManagement {
71                     repositories {
72                         maven("${testConfig.mavenRepoPath}")
73                         gradlePluginPortal()
74                         google()
75                         maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
76                     }
77                 }
78         """.trimIndent()
79         rootDir.resolve("settings.gradle.kts").writeText(contents)
80     }
81 
82     fun writeAndroidGradlePropertiesFile() {
83         val contents = """
84             android.useAndroidX=true
85             org.gradle.jvmargs=-Xmx2048M -XX:MaxMetaspaceSize=512m
86         """.trimIndent()
87         rootDir.resolve("gradle.properties").writeText(contents)
88     }
89 
90     private fun writeBuildFile() {
91         val rootBuildFile = buildString {
92             appendln("plugins {")
93             val allPlugins = (processorModule.plugins + appModule.plugins).distinct()
94             allPlugins.forEach {
95                 appendln("""    ${it.text} version "${it.version}" apply false """)
96             }
97             appendln("}")
98             appendln(
99                 """
100             repositories {
101                 maven("${testConfig.mavenRepoPath}")
102                 mavenCentral()
103                 maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
104                 google()
105             }
106             configurations.all {
107                 resolutionStrategy.eachDependency {
108                     if (requested.group == "org.jetbrains.kotlin") {
109                         useVersion("${testConfig.kotlinBaseVersion}")
110                     }
111                 }
112             }
113             subprojects {
114                 repositories {
115                     maven("${testConfig.mavenRepoPath}")
116                     mavenCentral()
117                     maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap/")
118                     google()
119                 }
120                 configurations.all {
121                     resolutionStrategy.eachDependency {
122                         if (requested.group == "org.jetbrains.kotlin") {
123                             useVersion("${testConfig.kotlinBaseVersion}")
124                         }
125                     }
126                 }
127             }
128                 """.trimIndent()
129             )
130         }
131         rootDir.resolve("build.gradle.kts").writeText(rootBuildFile)
132     }
133 }
134