Is there some way I can read the version variable from my project's .nimble file? Some languages include built in functions that will give you this value, does Nim have one of these functions?
The .nimble file can be parsed with parsecfg. This parsing can be as complex as shown in the documentation with a while loop and considering all possible cases, or you can trust that the .nimble file follows the standard and includes a line like:
version = 0.1.2
In this case you can just search for that case like this:
import parsecfg
var p: Config = loadConfig("./yourPackageName.nimble")
echo p.getSectionValue("", "version")
The empty string passed to getSectionValue points to the root of the config file, and then you extract the version value from there.
Enhancing the answer from @xbello, this can be evaluated statically so that the .nimble file doesn't need to present at run time. The key is to leverage staticRead() to read the file at compile time and pass it into loadConfig() as a stream.
import std/[parsecfg, streams]
const version =
staticRead("./proj.nimble").newStringStream.loadConfig.getSectionValue("", "version")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With