1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 13:03:35 +08:00

[build] Reload local.properties when file modified

This commit is contained in:
Karlatemp 2023-05-05 23:26:12 +08:00
parent bda5d54bd3
commit bc8ff18b07
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59

View File

@ -58,18 +58,41 @@ fun <T> projectLazy(action: () -> T): Lazy<T> {
private lateinit var localProperties: Properties
private var localPropertiesEdition: Long = 0
private fun Project.loadLocalPropertiesIfNecessary() {
val theFile = rootProject.projectDir.resolve("local.properties")
fun isNecessary(): Boolean {
if (!::localProperties.isInitialized) return true
if (theFile.exists()) {
if (localPropertiesEdition != theFile.lastModified()) {
return true
}
} else {
if (localPropertiesEdition != 0L) { // deleted
return true
}
}
return false
}
if (!isNecessary()) return
private fun Project.loadLocalPropertiesIfAbsent() {
if (::localProperties.isInitialized) return
localProperties = Properties().apply {
rootProject.projectDir.resolve("local.properties").takeIf { it.exists() }?.bufferedReader()?.use {
load(it)
localPropertiesEdition = if (theFile.exists()) {
theFile.bufferedReader().use { load(it) }
theFile.lastModified()
} else {
0
}
}
}
fun Project.getLocalProperty(name: String): String? {
loadLocalPropertiesIfAbsent()
loadLocalPropertiesIfNecessary()
return localProperties.getProperty(name)
}