Add support for executing a command on a file.

$file in the command string references the file name. The stdout of the
command will become the file's new contents. This is useful for doing
things that nimgen might not support, like using sed or grep or some
other command line tool.
This commit is contained in:
Joey Yakimowich-Payne 2018-06-16 15:33:47 +09:00
commit e371546d52
2 changed files with 11 additions and 1 deletions

View file

@ -228,6 +228,12 @@ proc prepend(file: string, data: string, search="") =
if idx != -1:
content = content[0..<idx] & data & content[idx..<content.len()]
proc execute(file: string, command: string) =
withFile(file):
let cmd = command % ["file", file]
let commandResult = execProc(cmd)
content = commandResult
proc append(file: string, data: string, search="") =
withFile(file):
if search == "":
@ -587,7 +593,7 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
if action == "create":
createDir(file.splitPath().head)
writeFile(file, cfg[act])
elif action in @["prepend", "append", "replace", "comment", "rename", "compile", "dynlib", "pragma"] and sfile != "":
elif action in @["prepend", "append", "replace", "comment", "rename", "compile", "dynlib", "pragma", "execute"] and sfile != "":
if action == "prepend":
if srch != "":
prepend(sfile, cfg[act], cfg[srch])
@ -612,6 +618,8 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
c2nimConfig.dynlib.add(cfg[act])
elif action == "pragma":
c2nimConfig.pragma.add(cfg[act])
elif action == "execute":
execute(sfile, cfg[act])
srch = ""
elif action == "search":
srch = act