From 77e28545efe6d0a4f34283cbdd051d1ce0f4667d Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Mon, 18 Jun 2018 19:41:12 +0900 Subject: [PATCH] Add `after` command that operates on files after nimgen has run --- README.md | 5 ++++ nimgen.nim | 88 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 71 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 423b706..ff7b2b4 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,11 @@ File wildcards such as *.nim, ssl*.h, etc. can be used to perform tasks across a ```wildcard``` = pattern to match against. All keys following the wildcard declaration will apply to matched files +_[n.after]_ +The same as n.wildcard, but instead of operating on the files before nimgen is done generating, it operates after, so you can modify Nim files or other files that result from nimgen or c2nim. + +```wildcard``` = pattern to match against. All keys following the wildcard declaration will apply to matched files + _[sourcefile]_ The following keys apply to library source code and help with generating the .nim files. -win, -lin and -osx can be used for OS specific tasks. E.g. dynlib-win, pragma-win diff --git a/nimgen.nim b/nimgen.nim index 942afc6..0bdde29 100644 --- a/nimgen.nim +++ b/nimgen.nim @@ -2,6 +2,7 @@ import nre, os, ospaths, osproc, parsecfg, pegs, ropes, sequtils, streams, strut var gDoneRecursive: seq[string] = @[] + gDoneAfter: seq[string] = @[] gDoneInline: seq[string] = @[] gProjectDir = getCurrentDir() @@ -15,6 +16,7 @@ var gExcludes: seq[string] = @[] gRenames = initTable[string, string]() gWildcards = newConfig() + gAfter = newConfig() type c2nimConfigObj = object @@ -566,29 +568,11 @@ proc c2nim(fl, outfile: string, c2nimConfig: c2nimConfigObj) = if outlib != "": prepend(outfile, outlib) -# ### -# Processor - -proc runFile(file: string, cfgin: OrderedTableRef) = - var - cfg = cfgin - sfile = search(file) - - for pattern in gWildcards.keys(): - let pat = pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".?") - if file.find(re(pat)).isSome(): - echo "Appending " & file & " " & pattern - for key in gWildcards[pattern].keys(): - cfg[key & "." & pattern] = gWildcards[pattern][key] +proc doActions(file: string, c2nimConfig: var c2nimConfigObj, cfg: OrderedTableRef) = var srch = "" - - c2nimConfig = c2nimConfigObj( - flags: "--stdcall", ppflags: "", - recurse: false, inline: false, preprocess: false, ctags: false, defines: false, - dynlib: @[], compile: @[], pragma: @[] - ) + sfile = search(file) for act in cfg.keys(): let (action, val) = getKey(act) @@ -627,6 +611,51 @@ proc runFile(file: string, cfgin: OrderedTableRef) = elif action == "search": srch = act +proc processAfter(nimFile: string, c2nimConfig: var c2nimConfigObj) = + var file = search(nimFile) + if file == "": + return + + if file in gDoneAfter: + return + + gDoneAfter.add(file) + + var afterConfig = newOrderedTable[string, string]() + for pattern in gAfter.keys(): + let pat = pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".?") + if nimFile.find(re(pat)).isSome(): + for key in gAfter[pattern].keys(): + let value = gAfter[pattern][key] + #echo key, ": ", "\"", value, "\" in ", nimFile + afterConfig[key & "." & pattern] = value + + doActions(nimFile, c2nimConfig, afterConfig) + +# ### +# Processor + +proc runFile(file: string, cfgin: OrderedTableRef) = + var + cfg = cfgin + sfile = search(file) + + for pattern in gWildcards.keys(): + let pat = pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".?") + if file.find(re(pat)).isSome(): + echo "Appending " & file & " " & pattern + for key in gWildcards[pattern].keys(): + cfg[key & "." & pattern] = gWildcards[pattern][key] + + var + c2nimConfig = c2nimConfigObj( + flags: "--stdcall", ppflags: "", + recurse: false, inline: false, preprocess: false, ctags: false, defines: false, + dynlib: @[], compile: @[], pragma: @[] + ) + + doActions(file, c2nimConfig, cfg) + if file.splitFile().ext != ".nim": var noprocess = false @@ -654,7 +683,10 @@ proc runFile(file: string, cfgin: OrderedTableRef) = quit(1) if not noprocess: - c2nim(file, getNimout(sfile), c2nimConfig) + let nimFile = getNimout(sfile) + c2nim(file, nimFile, c2nimConfig) + + processAfter(nimFile, c2nimConfig) proc runCfg(cfg: string) = if not fileExists(cfg): @@ -730,12 +762,24 @@ proc runCfg(cfg: string) = else: gWildcards.setSectionKey(wildcard, wild, gConfig["n.wildcard"][wild]) + if gConfig.hasKey("n.after"): + var wildcard = "" + for afterKey in gConfig["n.after"].keys(): + let (key, val) = getKey(afterKey) + if val == true: + if key == "wildcard": + wildcard = gConfig["n.after"][afterKey] + else: + gAfter.setSectionKey(wildcard, afterKey, gConfig["n.after"][afterKey]) + + for file in gConfig.keys(): - if file in @["n.global", "n.include", "n.exclude", "n.prepare", "n.wildcard"]: + if file in @["n.global", "n.include", "n.exclude", "n.prepare", "n.wildcard", "n.after"]: continue runFile(file, gConfig[file]) + # ### # Main loop