Skip to content

Commit

Permalink
Execute global directives before runtime config
Browse files Browse the repository at this point in the history
Global directives are necessarily runtime-agnostic, and may have impact
on building the subsequent runtimes (e.g. in the case of applying
updates and then building a classpath that includes those updated
libraries).

Thus we split directive execution into two phases: first global
directives, then runtime directives (after runtimes have been
configured).

Closes #47
Closes #55
  • Loading branch information
hinerm committed Sep 18, 2024
1 parent c7bbbbf commit beea943
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions src/commonMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,21 @@ fun main(args: Array<String>) {

applyModeHints(config.modes, hints, vars)

val runtimes = configureRuntimes(config, configDir, hints, vars)
val (launchDirectives, configDirectives) = calculateDirectives(config, hints, vars)

// Declare the global (runtime-agnostic) directives.
val globalDirectiveFunctions: DirectivesMap = mutableMapOf(
"help" to { _ -> help(exeFile, programName, supportedOptions) },
"apply-update" to { _ -> applyUpdate(appDir, appDir / "update") }
)

// Execute the global directives (e.g. applying updates) before configuring
// runtimes (e.g. building classpaths)
val runtimeDirectives = executeGlobalDirectives(globalDirectiveFunctions,
configDirectives, userArgs)

val runtimes = configureRuntimes(config, configDir, hints, vars)

debugBanner("BUILDING ARGUMENT LISTS")

// Ensure that the user arguments meet our expectations.
Expand All @@ -96,16 +108,9 @@ fun main(args: Array<String>) {
vars.interpolateInto(programArgs.main)
}

// Declare the global (runtime-agnostic) directives.
val globalDirectiveFunctions: DirectivesMap = mutableMapOf(
"help" to { _ -> help(exeFile, programName, supportedOptions) },
"apply-update" to { _ -> applyUpdate(appDir, appDir / "update") }
)

// Finally, execute all the directives! \^_^/
executeDirectives(globalDirectiveFunctions,
configDirectives, launchDirectives,
runtimes, userArgs, argsInContext)
executeDirectives(runtimeDirectives, launchDirectives, runtimes, userArgs,
argsInContext)
}

// -- Program flow functions --
Expand Down Expand Up @@ -490,8 +495,39 @@ private fun unknownArg(
return runtimes.firstOrNull { it.recognizes(arg) > 0 } == null
}

private fun executeDirectives(
/**
* Executes any global (runtime-independent) directives in the given
* configDirectives list.
*
* Returns a new immutable list containing any directives that could not be
* executed globally.
*/
private fun executeGlobalDirectives(
globalDirectiveFunctions: DirectivesMap,
configDirectives: List<String>,
userArgs: ProgramArgs
): List<String> {
debugBanner("EXECUTING GLOBAL DIRECTIVES")

// Execute the runtime-independent directives.
debug()
debug("Executing runtime-independent directives...")
val runtimeDirectives = mutableListOf<String>()
for (directive in configDirectives) {
// Execute the directive globally if possible.
val doDirective = globalDirectiveFunctions[directive]
if (doDirective != null) {
doDirective(userArgs)
continue
}

// Any non-global directives will need to be handled by the runtimes
runtimeDirectives.add(directive)
}
return runtimeDirectives.toList()
}

private fun executeDirectives(
configDirectives: List<String>,
launchDirectives: List<String>,
runtimes: List<RuntimeConfig>,
Expand All @@ -504,14 +540,6 @@ private fun executeDirectives(
debug()
debug("Executing configurator-side directives...")
for (directive in configDirectives) {
// Execute the directive globally if possible.
val doDirective = globalDirectiveFunctions[directive]
if (doDirective != null) {
doDirective(userArgs)
continue
}

// Not a global directive -- delegate execution to the runtimes.
var success = false
val (activatedRuntimes, dormantRuntimes) =
runtimes.partition { it.directive in launchDirectives }
Expand Down

0 comments on commit beea943

Please sign in to comment.