Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get version from Nimble Package?

Tags:

nim-lang

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?

like image 977
baranskistad Avatar asked Feb 27 '26 12:02

baranskistad


2 Answers

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.

like image 77
xbello Avatar answered Mar 02 '26 15:03

xbello


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")
like image 28
Kevin Thibedeau Avatar answered Mar 02 '26 13:03

Kevin Thibedeau