Skip to content

Commit

Permalink
util: add utility methods to AbstractMain to standardize how to outpu…
Browse files Browse the repository at this point in the history
…t version info in both Java and NodeJS
  • Loading branch information
briansfrank committed Nov 18, 2024
1 parent 649eb93 commit 87b8cb1
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/util/fan/AbstractMain.fan
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,52 @@ abstract class AbstractMain
rows.each |row| { out.printLine(" ${row[0]} ${row[1]}") }
}

//////////////////////////////////////////////////////////////////////////
// Utils
//////////////////////////////////////////////////////////////////////////

** Get map of the standard runtime props including:
** - java version info if JVM
** - node version if running in NodeJS
** - fan version
** Also see `printProps`
static Str:Str runtimeProps([Str:Str]? acc := null)
{
env := Env.cur
if (acc == null) acc = Str:Str[:] { ordered = true }
addVar := |Str name| { acc.setNotNull(name, env.vars[name]) }

// java version
addVar("java.version")
addVar("java.vm.name")
addVar("java.vm.vendor")
addVar("java.vm.version")
addVar("java.home")

// node version
addVar("node.version")
addVar("node.path")

// fantom version
acc["fan.version"] = Pod.find("sys").version.toStr
acc["fan.home"] = Env.cur.homeDir.osPath

return acc
}

** Print a map of key/value pairs to output with values justified.
** Options:
** - 'out': OutStream to print to (default is Env.cur.out)
** - 'indent': Str spaces (default is "")
static Void printProps(Str:Str props, [Str:Obj]? opts := null)
{
out := opts?.get("out") as OutStream ?: Env.cur.out
indent := opts?.get("indent")?.toStr ?: ""
maxName := 4
props.each |v, n| { maxName = maxName.max(n.size) }
props.each |v, n| { out.printLine(indent+ "$n:".padr(maxName+2) + v) }
}

//////////////////////////////////////////////////////////////////////////
// Run
//////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 87b8cb1

Please sign in to comment.