Hi,
We are using nimscript as a task runner for various (non nim ) projects. Typical tasks might be to deploying a docker based project to a remote server, or synchronizing data between local test servers and remote servers.
I would like to be able to have a configuration file that is separate from the main script, preferably a yaml file as that is easy for non-devs to look at and modifiy.
I tried to use parsecfg:
import parsecfg
let dict = loadConfig("robot.cfg")
but this results in "Error: cannot 'importc' variable at compile time; fopen"
Is it possible to open a file in nimscript?
You can use readFile from the OS module at least:
import os
echo readFile("test.nims")
[peter /tmp ] 19055 $ nim e test.nims
Hint: used config file '/home/peter/.choosenim/toolchains/nim-1.6.2/config/nim.cfg' [Conf]
Hint: used config file '/home/peter/.choosenim/toolchains/nim-1.6.2/config/config.nims' [Conf]
import os
echo readFile("test.nims")
Hint: used config file '/tmp/test.nims' [Conf]
With a bit of include trickery you can actually use Nimscript as the config language (without the overhead of booting up the Nim compiler more than once). This is nice because you get proper typechecking and error messages, and you can use procs/templates to introduce additional rules and constructs.
# settings.nims
fps = 30
fullscreen = false
width = 800
height = 480
searchPath "."
searchPath "C:/Users/exelotl/Pictures"
searchPath "C:/Users/exelotl/Music"
# config.nims
import std/[strutils, sequtils]
task foo, "my task":
var fps = 60
var width = 1280
var height = 720
var fullscreen = false
var searchPathList: seq[string]
proc searchPath(s: string) =
searchPathList.add(s)
# apply our config
include "settings.nims"
# invoke a hypothetical program
exec "my_slideshow_maker.exe $# -w $# -h $# --fps:$# --paths:$#" % [
width, height, fps,
searchPathList.join("|"),
(if fullscreen: "-f" else: ""),
]
Here's an example from my GBA toolkit. The doInclude template allows the user to supply the path to their own graphics config file when calling gfxConvert from a task in their project's config.nims.