Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Joey Yakimowich-Payne
fe01e49b31 Add postwildcard section that operates on files after nimgen has run 2018-07-09 10:36:05 +09:00
2 changed files with 73 additions and 22 deletions

View file

@ -109,6 +109,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.postwildcard]_
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] = @[]
gDonePostWildcard: seq[string] = @[]
gDoneInline: seq[string] = @[] gDoneInline: seq[string] = @[]
gProjectDir = "" gProjectDir = ""
@ -13,6 +14,7 @@ var
gExcludes: seq[string] = @[] gExcludes: seq[string] = @[]
gRenames = initTable[string, string]() gRenames = initTable[string, string]()
gWildcards = newConfig() gWildcards = newConfig()
gPostWildcard = newConfig()
type type
c2nimConfigObj = object c2nimConfigObj = object
@ -582,29 +584,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)
@ -643,6 +627,51 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
elif action == "search": elif action == "search":
srch = act srch = act
proc processPostWildcard(nimFile: string, c2nimConfig: var c2nimConfigObj) =
var file = search(nimFile)
if file == "":
return
if file in gDonePostWildcard:
return
gDonePostWildcard.add(file)
var postConfig = newOrderedTable[string, string]()
for pattern in gPostWildcard.keys():
let pat = pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".?")
if nimFile.find(re(pat)).isSome():
for key in gPostWildcard[pattern].keys():
let value = gPostWildcard[pattern][key]
#echo key, ": ", "\"", value, "\" in ", nimFile
postConfig[key & "." & pattern] = value
doActions(nimFile, c2nimConfig, postConfig)
# ###
# 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
@ -670,7 +699,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)
processPostWildcard(nimFile, c2nimConfig)
proc runCfg(cfg: string) = proc runCfg(cfg: string) =
if not fileExists(cfg): if not fileExists(cfg):
@ -743,8 +775,22 @@ 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.postwildcard"):
var wildcard = ""
for postKey in gConfig["n.postwildcard"].keys():
let (key, val) = getKey(postKey)
if val == true:
if key == "wildcard":
wildcard = gConfig["n.postwildcard"][postKey]
else:
gPostWildcard.setSectionKey(
wildcard, postKey, gConfig["n.postwildcard"][postKey]
)
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.postwildcard"]:
continue continue
runFile(file, gConfig[file]) runFile(file, gConfig[file])