Add after command that operates on files after nimgen has run

This commit is contained in:
Joey Yakimowich-Payne 2018-06-18 19:41:12 +09:00
commit 77e28545ef
2 changed files with 71 additions and 22 deletions

View file

@ -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 ```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]_ _[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 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

View file

@ -2,6 +2,7 @@ import nre, os, ospaths, osproc, parsecfg, pegs, ropes, sequtils, streams, strut
var var
gDoneRecursive: seq[string] = @[] gDoneRecursive: seq[string] = @[]
gDoneAfter: seq[string] = @[]
gDoneInline: seq[string] = @[] gDoneInline: seq[string] = @[]
gProjectDir = getCurrentDir() gProjectDir = getCurrentDir()
@ -15,6 +16,7 @@ var
gExcludes: seq[string] = @[] gExcludes: seq[string] = @[]
gRenames = initTable[string, string]() gRenames = initTable[string, string]()
gWildcards = newConfig() gWildcards = newConfig()
gAfter = newConfig()
type type
c2nimConfigObj = object c2nimConfigObj = object
@ -566,29 +568,11 @@ proc c2nim(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
if outlib != "": if outlib != "":
prepend(outfile, 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 var
srch = "" srch = ""
sfile = search(file)
c2nimConfig = c2nimConfigObj(
flags: "--stdcall", ppflags: "",
recurse: false, inline: false, preprocess: false, ctags: false, defines: false,
dynlib: @[], compile: @[], pragma: @[]
)
for act in cfg.keys(): for act in cfg.keys():
let (action, val) = getKey(act) let (action, val) = getKey(act)
@ -627,6 +611,51 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
elif action == "search": elif action == "search":
srch = act 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": if file.splitFile().ext != ".nim":
var noprocess = false var noprocess = false
@ -654,7 +683,10 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
quit(1) quit(1)
if not noprocess: if not noprocess:
c2nim(file, getNimout(sfile), c2nimConfig) let nimFile = getNimout(sfile)
c2nim(file, nimFile, c2nimConfig)
processAfter(nimFile, c2nimConfig)
proc runCfg(cfg: string) = proc runCfg(cfg: string) =
if not fileExists(cfg): if not fileExists(cfg):
@ -730,12 +762,24 @@ proc runCfg(cfg: string) =
else: else:
gWildcards.setSectionKey(wildcard, wild, gConfig["n.wildcard"][wild]) 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(): 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 continue
runFile(file, gConfig[file]) runFile(file, gConfig[file])
# ### # ###
# Main loop # Main loop