From d35cb3bf497271e321f9c909ba12d25e1ec3b3a0 Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Tue, 7 May 2024 15:34:28 -0500 Subject: [PATCH] Read in vars from matching .cfg files in app-dir --- src/commonMain/kotlin/vars.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/commonMain/kotlin/vars.kt b/src/commonMain/kotlin/vars.kt index a13fbe5..a091f29 100644 --- a/src/commonMain/kotlin/vars.kt +++ b/src/commonMain/kotlin/vars.kt @@ -7,6 +7,24 @@ class Vars(appDir: File, configDir: File, exeFile: File?) { vars["app-dir"] = appDir.path vars["config-dir"] = configDir.path if (exeFile?.exists == true) vars["executable"] = exeFile.path + + // Read matching .cfg files, containing key=value pairs, from the app-dir. + var cfgName = exeFile?.base?.name + val cfgFiles = mutableListOf() + while (cfgName != null) { + val cfgFile = appDir / "$cfgName.cfg" + if (cfgFile.exists) cfgFiles += cfgFile + val dash = cfgName.lastIndexOf("-") + cfgName = if (dash < 0) null else cfgName.substring(0, dash) + } + for (cfgFile in cfgFiles.reversed()) { + vars += cfgFile.lines() + .filter { it.indexOf("=") >= 0 } + .associate { + val eq = it.indexOf("=") + it.substring(0, eq).trim() to it.substring(eq + 1).trim() + } + } } fun calculate(items: Array, hints: Set): List {