Add support for renaming generated files

This commit is contained in:
Ganesh Viswanathan 2018-06-09 17:58:39 -05:00
commit 5e784f09ef
4 changed files with 62 additions and 10 deletions

View file

@ -38,6 +38,10 @@ To see examples of nimgen in action check out the following wrappers:-
* git checkout * git checkout
* Compile C code into binary * Compile C code into binary
* [nim7z](https://github.com/genotrance/nim7z) - 7z decoder wrapper: [docs](http://nimgen.genotrance.com/nim7z)
* git sparse checkout
* [nimarchive](https://github.com/genotrance/nimarchive) - libarchive wrapper: [docs](http://nimgen.genotrance.com/nimarchive)
* git sparse checkout
* [nimbigwig](https://github.com/genotrance/nimbigwig) - libbigWig wrapper: [docs](http://nimgen.genotrance.com/nimbigwig) * [nimbigwig](https://github.com/genotrance/nimbigwig) - libbigWig wrapper: [docs](http://nimgen.genotrance.com/nimbigwig)
* git checkout * git checkout
* [nimfuzz](https://github.com/genotrance/nimfuzz) - fts_fuzzy_match wrapper: [docs](http://nimgen.genotrance.com/nimfuzz) * [nimfuzz](https://github.com/genotrance/nimfuzz) - fts_fuzzy_match wrapper: [docs](http://nimgen.genotrance.com/nimfuzz)
@ -143,6 +147,16 @@ The following keys apply to library source code (before processing) and generate
```comment``` = number of lines to comment from search location ```comment``` = number of lines to comment from search location
The following key only applies before processing and allows renaming the generated .nim files as required to enable successful wrapping. This may be for organizational purposes or to prevent usage of non-nim supported strings in module names (E.g. first letter is a number). Destination is relative to output directory if defined.
```rename``` = string value to rename generated filename. E.g. rename = "$replace(7=s7)"
`/` = create a directory/module hierarchy
`$nimout` = refer to the original filename
`$replace(srch1=repl1, srch2=reply2)` = rename specific portions in `$nimout`
__Feedback__ __Feedback__
Nimgen is a work in progress and any feedback or suggestions are welcome. It is hosted on [GitHub](https://github.com/genotrance/nimgen) with an MIT license so issues, forks and PRs are most appreciated. Nimgen is a work in progress and any feedback or suggestions are welcome. It is hosted on [GitHub](https://github.com/genotrance/nimgen) with an MIT license so issues, forks and PRs are most appreciated.

View file

@ -3,6 +3,7 @@ import os
import ospaths import ospaths
import osproc import osproc
import parsecfg import parsecfg
import pegs
import ropes import ropes
import sequtils import sequtils
import streams import streams
@ -18,6 +19,7 @@ var QUOTES = true
var OUTPUT = "" var OUTPUT = ""
var INCLUDES: seq[string] = @[] var INCLUDES: seq[string] = @[]
var EXCLUDES: seq[string] = @[] var EXCLUDES: seq[string] = @[]
var RENAMES = initTable[string, string]()
var WILDCARDS = newConfig() var WILDCARDS = newConfig()
const DOC = """ const DOC = """
@ -134,12 +136,19 @@ proc getKey(ukey: string): tuple[key: string, val: bool] =
# ### # ###
# File loction # File loction
proc getNimout(file: string): string = proc getNimout(file: string, rename=true): string =
var nimout = file.splitFile().name.replace(re"[\-\.]", "_") & ".nim" result = file.splitFile().name.replace(re"[\-\.]", "_") & ".nim"
if OUTPUT != "": if OUTPUT != "":
nimout = OUTPUT/nimout result = OUTPUT/result
return nimout if not rename:
return
if RENAMES.hasKey(file):
result = RENAMES[file]
if not dirExists(parentDir(result)):
createDir(parentDir(result))
proc exclude(file: string): bool = proc exclude(file: string): bool =
for excl in EXCLUDES: for excl in EXCLUDES:
@ -250,6 +259,32 @@ proc comment(file: string, pattern: string, numlines: string) =
idx += 1 idx += 1
break break
proc rename(file: string, renfile: string) =
if file.splitFile().ext == ".nim":
return
var
nimout = getNimout(file, false)
newname = renfile.replace("$nimout", extractFilename(nimout))
#if newname =~ peg"(!\$.)*{'$replace'\s*'('\s*{(!\,\S)+}\s*','\s*{(!\)\S)+}\s*')'}":
if newname =~ peg"(!\$.)*{'$replace'\s*'('\s*{(!\)\S)+}')'}":
var final = nimout.extractFilename()
for entry in matches[1].split(","):
let spl = entry.split("=")
if spl.len() != 2:
echo "Bad replace syntax: " & renfile
quit(1)
var
srch = spl[0].strip()
repl = spl[1].strip()
final = final.replace(srch, repl)
newname = newname.replace(matches[0], final)
RENAMES[file] = OUTPUT/newname
proc compile(dir="", file=""): string = proc compile(dir="", file=""): string =
proc fcompile(file: string): string = proc fcompile(file: string): string =
return "{.compile: \"$#\".}" % file.replace("\\", "/") return "{.compile: \"$#\".}" % file.replace("\\", "/")
@ -381,7 +416,7 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
if file in DONE: if file in DONE:
return return
echo "Processing " & file echo "Processing $# => $#" % [file, outfile]
DONE.add(file) DONE.add(file)
fixFuncProtos(file) fixFuncProtos(file)
@ -390,7 +425,6 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
if recurse: if recurse:
var incls = getIncls(file) var incls = getIncls(file)
for inc in incls: for inc in incls:
incout &= "import " & inc.splitFile().name.replace(re"[\-\.]", "_") & "\n"
var cfg = newOrderedTable[string, string]() var cfg = newOrderedTable[string, string]()
if flags != "": cfg["flags"] = flags if flags != "": cfg["flags"] = flags
if ppflags != "": cfg["ppflags"] = ppflags if ppflags != "": cfg["ppflags"] = ppflags
@ -402,6 +436,8 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
cfg["dynlib." & i] = i cfg["dynlib." & i] = i
runFile(inc, cfg) runFile(inc, cfg)
incout &= "import $#\n" % inc.search().getNimout()[0 .. ^5] #inc.splitFile().name.replace(re"[\-\.]", "_") & "\n"
var cfile = file var cfile = file
if preprocess: if preprocess:
cfile = "temp-$#.c" % [outfile.extractFilename()] cfile = "temp-$#.c" % [outfile.extractFilename()]
@ -518,7 +554,7 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
if action == "create": if action == "create":
createDir(file.splitPath().head) createDir(file.splitPath().head)
writeFile(file, cfg[act]) writeFile(file, cfg[act])
elif action in @["prepend", "append", "replace", "comment", "compile", "dynlib", "pragma"] and sfile != "": elif action in @["prepend", "append", "replace", "comment", "rename", "compile", "dynlib", "pragma"] and sfile != "":
if action == "prepend": if action == "prepend":
if srch != "": if srch != "":
prepend(sfile, cfg[act], cfg[srch]) prepend(sfile, cfg[act], cfg[srch])
@ -535,6 +571,8 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
elif action == "comment": elif action == "comment":
if srch != "": if srch != "":
comment(sfile, cfg[srch], cfg[act]) comment(sfile, cfg[srch], cfg[act])
elif action == "rename":
rename(sfile, cfg[act])
elif action == "compile": elif action == "compile":
compile.add(cfg[act]) compile.add(cfg[act])
elif action == "dynlib": elif action == "dynlib":
@ -579,7 +617,7 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
quit(1) quit(1)
if not noprocess: if not noprocess:
c2nim(file, getNimout(file), flags, ppflags, recurse, inline, preprocess, ctags, defines, dynlib, compile, pragma) c2nim(file, getNimout(sfile), flags, ppflags, recurse, inline, preprocess, ctags, defines, dynlib, compile, pragma)
proc runCfg(cfg: string) = proc runCfg(cfg: string) =
if not fileExists(cfg): if not fileExists(cfg):

View file

@ -1,6 +1,6 @@
# Package # Package
version = "0.1.5" version = "0.2.0"
author = "genotrance" author = "genotrance"
description = "c2nim helper to simplify and automate the wrapping of C libraries" description = "c2nim helper to simplify and automate the wrapping of C libraries"
license = "MIT" license = "MIT"

View file

@ -3,7 +3,7 @@ import ospaths
import strutils import strutils
var full = false var full = false
var comps = @["libsvm", "nimbass", "nimbigwig", "nimfuzz", "nimrax", "nimssl", "nimssh2"] var comps = @["libsvm", "nim7z", "nimarchive", "nimbass", "nimbigwig", "nimfuzz", "nimrax", "nimssl", "nimssh2"]
if detectOs(Windows): if detectOs(Windows):
comps.add("nimkerberos") comps.add("nimkerberos")