Преглед изворни кода

[build] Reload local.properties when file modified

Karlatemp пре 2 година
родитељ
комит
bc8ff18b07
1 измењених фајлова са 28 додато и 5 уклоњено
  1. 28 5
      buildSrc/src/main/kotlin/LocalProperties.kt

+ 28 - 5
buildSrc/src/main/kotlin/LocalProperties.kt

@@ -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)
 }