Skip to content

Commit

Permalink
Create overridable config fields
Browse files Browse the repository at this point in the history
JaunchConfig fields prefixed with "cfg." will now explicitly allow
override via reading .cfg files. This allows us to set default field
values in the .toml but later allow the user to specify these values in
a more-easily modified config file.

The general pattern is illustrated here with the jvmMaxHeap field:
- a cfg.max-heap field is delcared in the toml with a default value
- jvm.max-heap is defined in the toml with a value of cfg.max-heap
- when jvm.max-heap is interpolated it will resolve to the current
  cfg.max-heap value, which may have been overridden by a .cfg file

Required for fiji/fiji#351
  • Loading branch information
hinerm authored and ctrueden committed Dec 11, 2024
1 parent 7c68d40 commit cc8c549
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/commonMain/kotlin/config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ data class JaunchConfig (

/** Arguments to pass to the main class on the Java side. */
val jvmMainArgs: Array<String> = emptyArray(),

/** The list of options overridable by .cfg files understood by Jaunch. */
val cfgVars: Map<String, Any> = emptyMap(),
) {
/** Return true iff the given argument is on the specified list of recognized args. */
fun recognizes(arg: String, recognizedArgs: Array<String>): Boolean {
Expand Down Expand Up @@ -153,6 +156,7 @@ data class JaunchConfig (
programName = config.programName ?: programName,
includes = config.includes + includes,
supportedOptions = config.supportedOptions + supportedOptions,
cfgVars = config.cfgVars + cfgVars,
osAliases = config.osAliases + osAliases,
archAliases = config.archAliases + archAliases,
modes = config.modes + modes,
Expand Down Expand Up @@ -240,6 +244,8 @@ fun readConfig(
var jvmMainClass: List<String>? = null
var jvmMainArgs: List<String>? = null

val cfgVars = mutableMapOf<String, Any>()

// Parse TOML file lines into tokens.
val tokens = mutableListOf<Any>()
tomlFile.lines().forEach { appendTokens(it, tokens) }
Expand Down Expand Up @@ -294,6 +300,13 @@ fun readConfig(
"jvm.runtime-args" -> jvmRuntimeArgs = asList(value)
"jvm.main-class" -> jvmMainClass = asList(value)
"jvm.main-args" -> jvmMainArgs = asList(value)
else -> {
// Parse cfg.* variable assignments into a cfgVars map
if (name.startsWith("cfg.") && value != null) {
cfgVars[name] = value
}
else warn("[TOML] Unsupported key: '$name'")
}
}
}
else -> warn("[TOML] Ignoring extraneous token: '$token' [${token::class.simpleName}]")
Expand All @@ -311,6 +324,7 @@ fun readConfig(
programName = programName,
includes = asArray(includes),
supportedOptions = asArray(supportedOptions),
cfgVars = cfgVars,
osAliases = asArray(osAliases),
archAliases = asArray(archAliases),
modes = asArray(modes),
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun main(args: Array<String>) {

// Declare a set to store option parameter values.
// It will be populated at argument parsing time.
val vars = Vars(appDir, configDir, exeFile)
val vars = Vars(appDir, configDir, exeFile, config.cfgVars)

// Sort out the arguments, keeping the user-specified runtime and main arguments in a struct. At this point,
// it may yet be ambiguous whether certain user args belong with the runtime, the main program, or neither.
Expand Down
12 changes: 11 additions & 1 deletion src/commonMain/kotlin/vars.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Vars class and related functions for working with variables.

class Vars(appDir: File, configDir: File, exeFile: File?) {
class Vars(
appDir: File,
configDir: File,
exeFile: File?,
cfgVars: Map<String, Any>
) {
private val vars = mutableMapOf<String, Any>()

init {
vars["app-dir"] = appDir.path
vars["config-dir"] = configDir.path
if (exeFile?.exists == true) vars["executable"] = exeFile.path

// Parse any Vars that were previously defined in toml.
if (cfgVars is Map) {
vars.putAll(cfgVars)
}

// Build the list of config files
val cfgFiles = mutableListOf<File>()
var cfgName = exeFile?.base?.name
Expand Down

0 comments on commit cc8c549

Please sign in to comment.