xref: /aosp_15_r20/external/kotlinx.atomicfu/buildSrc/src/main/kotlin/KotlinConfiguration.kt (revision 68017707106cb9da9fed635c150bc497c09c160f)
1 @file:JvmName("KotlinConfiguration")
2 
3 import org.gradle.api.Project
4 import org.gradle.api.artifacts.dsl.RepositoryHandler
5 import java.net.URI
6 import java.util.logging.Logger
7 
8 /*
9  * Functions in this file are responsible for configuring atomicfu build
10  * against a custom development version of Kotlin compiler.
11  * Such configuration is used in Kotlin aggregate builds and builds of Kotlin user projects
12  * in order to check whether not-yet-released changes are compatible with our libraries
13  * (aka "integration testing that substitutes lack of unit testing").
14  */
15 
16 private val LOGGER: Logger = Logger.getLogger("Kotlin settings logger")
17 
18 /**
19  * Should be used for running against a non-released Kotlin compiler on a system test level.
20  *
21  * @return a custom repository with development builds of the Kotlin compiler taken from:
22  *
23  * 1. the Kotlin community project Gradle plugin,
24  * 2. or `kotlin_repo_url` Gradle property (from command line or from `gradle.properties`),
25  *
26  * or null otherwise
27  */
getCustomKotlinRepositoryURLnull28 fun getCustomKotlinRepositoryURL(project: Project): String? {
29     val communityPluginKotlinRepoURL = project.findProperty("community.project.kotlin.repo") as? String
30     val gradlePropertyKotlinRepoURL = project.findProperty("kotlin_repo_url") as? String
31     val kotlinRepoURL = when {
32         communityPluginKotlinRepoURL != null -> communityPluginKotlinRepoURL
33         gradlePropertyKotlinRepoURL != null -> gradlePropertyKotlinRepoURL
34         else -> return null
35     }
36     LOGGER.info("A custom Kotlin repository $kotlinRepoURL was found for project ${project.name}")
37     return kotlinRepoURL
38 }
39 
40 /**
41  * Should be used for running against a non-released Kotlin compiler on a system test level.
42  *
43  * Adds a custom repository with development builds of the Kotlin compiler to [repositoryHandler]
44  * if the URL is provided (see [getCustomKotlinRepositoryURL]).
45  */
addCustomKotlinRepositoryIfEnablednull46 fun addCustomKotlinRepositoryIfEnabled(repositoryHandler: RepositoryHandler, project: Project) {
47     val kotlinRepoURL = getCustomKotlinRepositoryURL(project) ?: return
48     repositoryHandler.maven { url = URI.create(kotlinRepoURL) }
49 
50 }
51 
52 /**
53  * Should be used for running against a non-released Kotlin compiler on a system test level.
54  *
55  * @return a Kotlin version taken from the Kotlin community project Gradle plugin,
56  *         or null otherwise
57  */
getOverridingKotlinVersionnull58 fun getOverridingKotlinVersion(project: Project): String? {
59     val communityPluginKotlinVersion = project.findProperty("community.project.kotlin.version") as? String
60     // add any other ways of overriding the Kotlin version here
61     val kotlinVersion = when {
62         communityPluginKotlinVersion != null -> communityPluginKotlinVersion
63         // add any other ways of overriding the Kotlin version here
64         else -> return null
65     }
66     LOGGER.info("An overriding Kotlin version of $kotlinVersion was found for project ${project.name}")
67     return kotlinVersion
68 }
69 
70 /**
71  * Should be used for running against a non-released Kotlin compiler on a system test level.
72  *
73  * @return a Kotlin language version taken from:
74  *
75  * 1. the Kotlin community project Gradle plugin,
76  * 2. or `kotlin_language_version` Gradle property (from command line or from `gradle.properties`),
77  *
78  * or null otherwise
79  */
getOverridingKotlinLanguageVersionnull80 fun getOverridingKotlinLanguageVersion(project: Project): String? {
81     val communityPluginLanguageVersion = project.findProperty("community.project.kotlin.languageVersion") as? String
82     val gradlePropertyLanguageVersion = project.findProperty("kotlin_language_version") as? String
83     val languageVersion = when {
84         communityPluginLanguageVersion != null -> communityPluginLanguageVersion
85         gradlePropertyLanguageVersion != null -> gradlePropertyLanguageVersion
86         else -> return null
87     }
88     LOGGER.info("An overriding Kotlin language version of $languageVersion was found for project ${project.name}")
89     return languageVersion
90 }
91 
92 /**
93  * Should be used for running against a non-released Kotlin compiler on a system test level.
94  *
95  * @return a Kotlin API version taken from:
96  *
97  * 1. the Kotlin community project Gradle plugin,
98  * 2. or `kotlin_language_version` Gradle property (from command line or from `gradle.properties`),
99  *
100  * or null otherwise
101  */
getOverridingKotlinApiVersionnull102 fun getOverridingKotlinApiVersion(project: Project): String? {
103     val communityPluginApiVersion = project.findProperty("community.project.kotlin.apiVersion") as? String
104     val gradlePropertyApiVersion = project.findProperty("kotlin_api_version") as? String
105     val apiVersion = when {
106         communityPluginApiVersion != null -> communityPluginApiVersion
107         gradlePropertyApiVersion != null -> gradlePropertyApiVersion
108         else -> return null
109     }
110     LOGGER.info("An overriding Kotlin api version of $apiVersion was found for project ${project.name}")
111     return apiVersion
112 }
113