-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBuildConfig.kt
More file actions
107 lines (101 loc) · 5.15 KB
/
BuildConfig.kt
File metadata and controls
107 lines (101 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// © 2026-present https://github.com/cengiz-pz
//
import java.io.File
import java.util.Properties
/**
* Immutable value object holding build-wide settings and per-module extension points,
* loaded from four properties files.
*
* Obtain an instance via [Project.loadBuildConfig] (defined in `ProjectExtensions.kt`),
* which is available in any project build script that applies `id("base-conventions")`:
*
* ```kotlin
* plugins { id("base-conventions") }
*
* val buildConfig = loadBuildConfig()
* println(buildConfig.gradleProjectName) // "my-plugin"
* println(buildConfig.androidExtraProperties) // Map<String, String>
* ```
*
* ## Source files
*
* | Property group | File (relative to repo root) |
* |--------------------------|-----------------------------------------------|
* | [gradleProjectName] | `gradle/config/build.properties` |
* | [rootExtraProperties] | `gradle/config/build.properties` |
* | [rootExtraGradle] | `gradle/config/build.properties` |
* | [addonExtraProperties] | `addon/config/addon-build.properties` |
* | [addonExtraGradle] | `addon/config/addon-build.properties` |
* | [androidExtraProperties] | `android/config/android-build.properties` |
* | [androidExtraGradle] | `android/config/android-build.properties` |
* | [iosExtraProperties] | `ios/config/ios-build.properties` |
* | [iosExtraGradle] | `ios/config/ios-build.properties` |
*
* ## Key conventions in per-module files
*
* ```properties
* # Forwarded to project.extra["myFlag"] in that module
* extra.myFlag=true
*
* # Applied via project.apply(from = …) in that module
* gradle.myExtras=config/my-extras.gradle.kts
* ```
*/
data class BuildConfig(
/** Gradle root project name, e.g. `my-plugin`. */
val gradleProjectName: String,
/** Extra `project.extra` entries for the root project (`root.extra.*` keys). */
val rootExtraProperties: Map<String, String> = emptyMap(),
/** Extra Gradle scripts for the root project (`root.gradle.*` keys; values are file paths). */
val rootExtraGradle: Map<String, String> = emptyMap(),
/** Extra `project.extra` entries for the `:addon` sub-project (`extra.*` keys). */
val addonExtraProperties: Map<String, String> = emptyMap(),
/** Extra Gradle scripts for the `:addon` sub-project (`gradle.*` keys). */
val addonExtraGradle: Map<String, String> = emptyMap(),
/** Extra `project.extra` entries for the `:android` sub-project (`extra.*` keys). */
val androidExtraProperties: Map<String, String> = emptyMap(),
/** Extra Gradle scripts for the `:android` sub-project (`gradle.*` keys). */
val androidExtraGradle: Map<String, String> = emptyMap(),
/** Extra `project.extra` entries for the `:ios` sub-project (`extra.*` keys). */
val iosExtraProperties: Map<String, String> = emptyMap(),
/** Extra Gradle scripts for the `:ios` sub-project (`gradle.*` keys). */
val iosExtraGradle: Map<String, String> = emptyMap(),
) {
companion object {
/**
* Loads a [BuildConfig] from all four properties files.
*
* @param gradleRootDir `rootProject.rootDir` - the `gradle/` directory.
*/
fun load(gradleRootDir: File): BuildConfig {
val repoRoot = gradleRootDir.parentFile
val buildProps = loadPropsFile(gradleRootDir.resolve("config/build.properties"))
val addonProps = loadPropsFile(repoRoot.resolve("addon/config/addon-build.properties"))
val androidProps = loadPropsFile(repoRoot.resolve("android/config/android-build.properties"))
val iosProps = loadPropsFile(repoRoot.resolve("ios/config/ios-build.properties"))
return BuildConfig(
gradleProjectName = buildProps.require("gradleProjectName"),
rootExtraProperties = buildProps.extractPrefixed("root.extra."),
rootExtraGradle = buildProps.extractPrefixed("root.gradle."),
addonExtraProperties = addonProps.extractPrefixed("extra."),
addonExtraGradle = addonProps.extractPrefixed("gradle."),
androidExtraProperties = androidProps.extractPrefixed("extra."),
androidExtraGradle = androidProps.extractPrefixed("gradle."),
iosExtraProperties = iosProps.extractPrefixed("extra."),
iosExtraGradle = iosProps.extractPrefixed("gradle."),
)
}
private fun loadPropsFile(file: File): Properties {
check(file.exists()) { "Properties file not found: ${file.absolutePath}" }
return Properties().also { props -> file.inputStream().use { props.load(it) } }
}
}
}
private fun Properties.require(key: String): String =
getProperty(key)?.trim()
?: error("Required key '$key' is missing from build.properties.")
private fun Properties.extractPrefixed(prefix: String): Map<String, String> =
stringPropertyNames()
.filter { it.startsWith(prefix) }
.associate { key -> key.removePrefix(prefix) to getProperty(key).trim() }