aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2018-06-09 17:58:39 -0500
committerGanesh Viswanathan <dev@genotrance.com>2018-06-09 17:58:39 -0500
commit5e784f09ef3ea5ef3b428d667ca195c47f34db51 (patch)
treeb02b992388ad2a54446a7dc6cd38c800945ff078
parentdf5494293390d749bcb574d7c7484c4df6f97ae6 (diff)
downloadnimgen-0.2.0.tar.gz
nimgen-0.2.0.zip
Add support for renaming generated filesv0.2.0
-rw-r--r--README.md14
-rw-r--r--nimgen.nim54
-rw-r--r--nimgen.nimble2
-rw-r--r--tests/nimgentest.nims2
4 files changed, 62 insertions, 10 deletions
diff --git a/README.md b/README.md
index 568aac6..129ce56 100644
--- a/README.md
+++ b/README.md
@@ -38,6 +38,10 @@ To see examples of nimgen in action check out the following wrappers:-
* git checkout
* 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)
* git checkout
* [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
+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__
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.
diff --git a/nimgen.nim b/nimgen.nim
index c48fdde..9771683 100644
--- a/nimgen.nim
+++ b/nimgen.nim
@@ -3,6 +3,7 @@ import os
import ospaths
import osproc
import parsecfg
+import pegs
import ropes
import sequtils
import streams
@@ -18,6 +19,7 @@ var QUOTES = true
var OUTPUT = ""
var INCLUDES: seq[string] = @[]
var EXCLUDES: seq[string] = @[]
+var RENAMES = initTable[string, string]()
var WILDCARDS = newConfig()
const DOC = """
@@ -134,12 +136,19 @@ proc getKey(ukey: string): tuple[key: string, val: bool] =
# ###
# File loction
-proc getNimout(file: string): string =
- var nimout = file.splitFile().name.replace(re"[\-\.]", "_") & ".nim"
+proc getNimout(file: string, rename=true): string =
+ result = file.splitFile().name.replace(re"[\-\.]", "_") & ".nim"
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 =
for excl in EXCLUDES:
@@ -250,6 +259,32 @@ proc comment(file: string, pattern: string, numlines: string) =
idx += 1
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 fcompile(file: string): string =
return "{.compile: \"$#\".}" % file.replace("\\", "/")
@@ -381,7 +416,7 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
if file in DONE:
return
- echo "Processing " & file
+ echo "Processing $# => $#" % [file, outfile]
DONE.add(file)
fixFuncProtos(file)
@@ -390,7 +425,6 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
if recurse:
var incls = getIncls(file)
for inc in incls:
- incout &= "import " & inc.splitFile().name.replace(re"[\-\.]", "_") & "\n"
var cfg = newOrderedTable[string, string]()
if flags != "": cfg["flags"] = flags
if ppflags != "": cfg["ppflags"] = ppflags
@@ -402,6 +436,8 @@ proc c2nim(fl, outfile, flags, ppflags: string, recurse, inline, preprocess, cta
cfg["dynlib." & i] = i
runFile(inc, cfg)
+ incout &= "import $#\n" % inc.search().getNimout()[0 .. ^5] #inc.splitFile().name.replace(re"[\-\.]", "_") & "\n"
+
var cfile = file
if preprocess:
cfile = "temp-$#.c" % [outfile.extractFilename()]
@@ -518,7 +554,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", "compile", "dynlib", "pragma"] and sfile != "":
+ elif action in @["prepend", "append", "replace", "comment", "rename", "compile", "dynlib", "pragma"] and sfile != "":
if action == "prepend":
if srch != "":
prepend(sfile, cfg[act], cfg[srch])
@@ -535,6 +571,8 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
elif action == "comment":
if srch != "":
comment(sfile, cfg[srch], cfg[act])
+ elif action == "rename":
+ rename(sfile, cfg[act])
elif action == "compile":
compile.add(cfg[act])
elif action == "dynlib":
@@ -579,7 +617,7 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
quit(1)
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) =
if not fileExists(cfg):
diff --git a/nimgen.nimble b/nimgen.nimble
index b2bfac6..3e458ba 100644
--- a/nimgen.nimble
+++ b/nimgen.nimble
@@ -1,6 +1,6 @@
# Package
-version = "0.1.5"
+version = "0.2.0"
author = "genotrance"
description = "c2nim helper to simplify and automate the wrapping of C libraries"
license = "MIT"
diff --git a/tests/nimgentest.nims b/tests/nimgentest.nims
index 41d64e9..2001ef2 100644
--- a/tests/nimgentest.nims
+++ b/tests/nimgentest.nims
@@ -3,7 +3,7 @@ import ospaths
import strutils
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):
comps.add("nimkerberos")