## Resolution + env gating for the universal battle-test runner. ## ## Deliberately thin: everything that already exists in common_libs/test_framework ## (compileBots) is imported, not reimplemented. We only add: ## - stem derivation (strip _garage, see bot_compiler.nim:9) ## - `.battletest.json` per-garage overrides ## - the SKIP-when-no-jars policy import std/[os, strutils, json] import ../../common_libs/test_framework/server_manager ## DefaultServerJar (import only, never modified) type Overrides* = object rounds*: int ## 0 = not overridden opponents*: seq[string] ## empty = not overridden ResolvedBot* = object dir*: string ## directory to hand to the battle runner (or stage) stem*: string ## bot name, _garage stripped rounds*: int ## effective rounds for this bot (0 = global) opponents*: seq[string] ## per-garage override, empty = global matrix const GarageSuffix* = "_garage" const OverrideFile* = ".battletest.json" proc stemOf*(path: string): string = ## Mirrors bot_compiler.nim:9 — last path component minus _garage. result = lastPathPart(absolutePath(path)) if result.endsWith(GarageSuffix): result = result[0 ..< result.len - GarageSuffix.len] proc innerBotDir*(path: string): string = ## Some garages keep the actual bot in a nested `/` subdir ## (e.g. DevControlBot_garage/DevControlBot/). Descend into it when the ## given dir has no src/ and no binary but has a same-named subdir. let abs = absolutePath(path) let stem = stemOf(abs) if not dirExists(abs / "src") and not fileExists(abs / stem) and dirExists(abs / stem): return abs / stem abs proc readOverrides*(path: string): Overrides = let f = absolutePath(path) / OverrideFile if not fileExists(f): return try: let n = parseJson(readFile(f)) if n.hasKey("rounds"): result.rounds = n["rounds"].getInt() if n.hasKey("opponents"): for o in n["opponents"].getElems(): result.opponents.add o.getStr() except JsonParsingError as e: raise newException(ValueError, f & " is not valid JSON: " & e.msg) proc resolveBot*(path: string): ResolvedBot = ## path may be a garage (`X_garage`) or a bot dir. Returns the dir the ## runner should use plus the bot's stem and any .battletest.json overrides. if not dirExists(path): raise newException(IOError, "bot path is not a directory: " & path) result.dir = innerBotDir(path) result.stem = stemOf(result.dir) let ov = readOverrides(result.dir) result.rounds = ov.rounds result.opponents = ov.opponents # --- env gating ------------------------------------------------------------- const RepoRoot* = currentSourcePath().parentDir.parentDir.parentDir AdversaryRoot* = currentSourcePath().parentDir / "adversaries" const DefaultRunnerJarHint* = "/home/davide/Projects/tank-royale/runner/examples/lib/robocode-tankroyale-runner.jar" type Gate* = object ok*: bool ## false => SKIP (quit 0), not a failure reason*: string serverJar*: string runnerJar*: string proc gate*(serverOverride = "", runnerOverride = ""): Gate = ## SKIP (exit 0) when the JARs are missing — root AGENTS.md prescribes this ## and no garage implements it yet. Explicit overrides bypass the env. result.serverJar = if serverOverride.len > 0: serverOverride else: getEnv("TR_SERVER_JAR", DefaultServerJar) ## same default as server_manager.nim result.runnerJar = if runnerOverride.len > 0: runnerOverride else: getEnv("TR_BATTLE_RUNNER", DefaultRunnerJarHint) if not fileExists(result.serverJar): result.reason = "TR_SERVER_JAR does not exist: " & result.serverJar elif not fileExists(result.runnerJar): result.reason = "TR_BATTLE_RUNNER does not exist: " & result.runnerJar else: result.ok = true