Skip to content

Commit

Permalink
Merge pull request #56 from apposed/separate-global-directives
Browse files Browse the repository at this point in the history
Execute global directives before configuring runtimes
  • Loading branch information
hinerm authored Sep 19, 2024
2 parents c7bbbbf + bb32461 commit 2b48423
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 nonGlobalDirectives = 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(nonGlobalDirectives, 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 2b48423

Please sign in to comment.