aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2018-08-19 12:31:21 -0500
committerGanesh Viswanathan <dev@genotrance.com>2018-08-19 12:31:21 -0500
commit1711052eae985e042ab22108f0975d9bb0906abf (patch)
treebdca8d43270799b80078420666e1cacef52497fc
parent7e21b2799eafaad9662bf2d754e94d25eea25100 (diff)
downloadnimgen-1711052eae985e042ab22108f0975d9bb0906abf.tar.gz
nimgen-1711052eae985e042ab22108f0975d9bb0906abf.zip
Add move capability, handle compile duplicate names
-rw-r--r--README.md2
-rw-r--r--nimgen.nimble2
-rw-r--r--src/nimgen/fileops.nim21
-rw-r--r--src/nimgen/gencore.nim14
-rw-r--r--src/nimgen/globals.nim1
-rw-r--r--src/nimgen/runcfg.nim11
6 files changed, 41 insertions, 10 deletions
diff --git a/README.md b/README.md
index 66db546..7cb0f1c 100644
--- a/README.md
+++ b/README.md
@@ -189,6 +189,8 @@ The following keys apply to library source code (before processing) and generate
```replace``` = string value to replace search string in file. Regex captures can be referred to using $1, $2, etc.
+```move``` = search string providing context for location to move the results of a preceding search or regex match
+
```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.
diff --git a/nimgen.nimble b/nimgen.nimble
index 841cb09..6ca5b9e 100644
--- a/nimgen.nimble
+++ b/nimgen.nimble
@@ -1,6 +1,6 @@
# Package
-version = "0.3.1"
+version = "0.4.0"
author = "genotrance"
description = "c2nim helper to simplify and automate the wrapping of C libraries"
license = "MIT"
diff --git a/src/nimgen/fileops.nim b/src/nimgen/fileops.nim
index 4e8f858..e416cf1 100644
--- a/src/nimgen/fileops.nim
+++ b/src/nimgen/fileops.nim
@@ -31,14 +31,25 @@ proc append*(file: string, data: string, search="") =
if idx != -1:
content = content[0..<idy] & data & content[idy..<content.len()]
-proc freplace*(file: string, pattern: string, repl="") =
+proc freplace*(file: string, pattern: string|Regex, repl="") =
withFile(file):
- if pattern in content:
- content = content.replace(pattern, repl)
+ content = content.replace(pattern, repl)
-proc freplace*(file: string, pattern: Regex, repl="") =
+proc move*(file: string, pattern: string|Regex, move: string) =
+ var tomove: seq[string] = @[]
withFile(file):
- content = content.replace(pattern, repl)
+ when pattern is string:
+ tomove.add(pattern)
+
+ when pattern is Regex:
+ var ms = content.findAll(pattern)
+ for i, m in ms:
+ tomove.add(content[m.group(0)[0]])
+
+ content = content.replace(pattern, "")
+
+ for i in tomove:
+ append(file, i, move)
proc comment*(file: string, pattern: string, numlines: string) =
let
diff --git a/src/nimgen/gencore.nim b/src/nimgen/gencore.nim
index 000651e..027510c 100644
--- a/src/nimgen/gencore.nim
+++ b/src/nimgen/gencore.nim
@@ -21,7 +21,19 @@ proc compile*(cpl, flags: string): string =
var data = ""
proc fcompile(file: string): string =
- return "{.compile: \"$#\".}" % file.replace("\\", "/")
+ let fn = file.splitFile().name
+ var
+ ufn = fn
+ uniq = 1
+ while ufn in gCompile:
+ ufn = fn & $uniq
+ uniq += 1
+
+ gCompile.add(ufn)
+ if fn == ufn:
+ return "{.compile: \"$#\".}" % file.replace("\\", "/")
+ else:
+ return "{.compile: (\"../$#\", \"$#.o\").}" % [file.replace("\\", "/"), ufn]
proc dcompile(dir: string) =
for f in walkFiles(dir):
diff --git a/src/nimgen/globals.nim b/src/nimgen/globals.nim
index c371fe2..a83460f 100644
--- a/src/nimgen/globals.nim
+++ b/src/nimgen/globals.nim
@@ -21,6 +21,7 @@ var
gExcludes*: seq[string] = @[]
gRenames* = initTable[string, string]()
gWildcards* = newConfig()
+ gCompile*: seq[string] = @[]
type
c2nimConfigObj* = object
diff --git a/src/nimgen/runcfg.nim b/src/nimgen/runcfg.nim
index 91e7078..5f04b04 100644
--- a/src/nimgen/runcfg.nim
+++ b/src/nimgen/runcfg.nim
@@ -63,9 +63,9 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
gExcludes.delete(gExcludes.find(file))
sfile = file
gDoneRecursive.add(sfile)
- elif action in @["prepend", "append", "replace", "comment",
- "rename", "compile", "dynlib", "pragma",
- "pipe"] and sfile != "":
+ elif action in @["prepend", "append", "replace", "move", "comment",
+ "rename", "compile", "dynlib", "pragma", "pipe"] and
+ sfile != "":
if action == "prepend":
if srch != "":
prepend(sfile, cfg[act], cfg[srch])
@@ -81,6 +81,11 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
freplace(sfile, cfg[srch], cfg[act])
elif rgx != "":
freplace(sfile, toPattern(cfg[rgx]), cfg[act])
+ elif action == "move":
+ if srch != "":
+ move(sfile, cfg[srch], cfg[act])
+ elif rgx != "":
+ move(sfile, toPattern(cfg[rgx]), cfg[act])
elif action == "comment":
if srch != "":
comment(sfile, cfg[srch], cfg[act])