aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2018-11-20 22:56:30 -0600
committerGanesh Viswanathan <dev@genotrance.com>2018-11-20 22:56:30 -0600
commit63585e889ecfaf6f99148696986a1ac1fa85f60d (patch)
tree74236ea63439cd22de2c2738ba1861ab360ef4b2
parent882b38f4f642b4d351836f1f1f9645a46b3e7610 (diff)
downloadnimterop-63585e889ecfaf6f99148696986a1ac1fa85f60d.tar.gz
nimterop-63585e889ecfaf6f99148696986a1ac1fa85f60d.zip
Add git support
-rw-r--r--README.md2
-rw-r--r--nimterop/getters.nim1
-rw-r--r--nimterop/git.nim94
3 files changed, 96 insertions, 1 deletions
diff --git a/README.md b/README.md
index 95d4207..4df46fc 100644
--- a/README.md
+++ b/README.md
@@ -58,6 +58,8 @@ Detailed documentation is still forthcoming.
`cImport()` - import all supported definitions from specific import header file
+`gitPull()` - pull a git repository prior to C/C++ interop
+
__Implementation Details__
In order to use the tree-sitter C library at compile-time, it has to be compiled into a separate binary called `toast` (to AST) since the Nim VM doesn't yet support FFI. `toast` takes a C/C++ file and runs it through the tree-sitter API which returns an AST data structure. This is then printed out to stdout in a Lisp S-Expression format.
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index dd6ca40..d31034a 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -31,7 +31,6 @@ proc getGccPaths*(mode = "c"): string =
proc getLineCol*(node: ref Ast): tuple[line, col: int] =
result.line = 1
result.col = 1
- echo gCode[node.start .. node.stop-1]
for i in 0 .. node.start-1:
if gCode[i] == '\n':
result.col = 0
diff --git a/nimterop/git.nim b/nimterop/git.nim
new file mode 100644
index 0000000..f06e9ba
--- /dev/null
+++ b/nimterop/git.nim
@@ -0,0 +1,94 @@
+import macros, os, regex, strformat, strutils
+
+import globals
+
+proc execAction*(cmd: string): string =
+ var
+ ccmd = ""
+ ret = 0
+ when defined(Windows):
+ ccmd = "cmd /c " & cmd
+ when defined(Linux) or defined(MacOSX):
+ ccmd = "bash -c '" & cmd & "'"
+
+ (result, ret) = gorgeEx(ccmd)
+ if ret != 0:
+ echo "Command failed: " & $ret
+ echo ccmd
+ echo result
+ quit(1)
+
+macro extractZip*(zipfile, outdir: static[string]): untyped =
+ var cmd = "unzip -o $#"
+ if defined(Windows):
+ cmd = "powershell -nologo -noprofile -command \"& { Add-Type -A " &
+ "'System.IO.Compression.FileSystem'; " &
+ "[IO.Compression.ZipFile]::ExtractToDirectory('$#', '.'); }\""
+
+ echo "Extracting " & zipfile
+ discard execAction(&"cd \"{getProjectPath()/outdir}\" && " & cmd % zipfile)
+
+macro downloadUrl*(url, outdir: static[string]): untyped =
+ let
+ file = url.extractFilename()
+ ext = file.splitFile().ext.toLowerAscii()
+
+ var cmd = "curl $# -o $#"
+ if defined(Windows):
+ cmd = "powershell wget $# -OutFile $#"
+
+ if not (ext == ".zip" and fileExists(getProjectPath()/outdir/file)):
+ echo "Downloading " & file
+ discard execAction(cmd % [url, getProjectPath()/outdir/file])
+
+ if ext == ".zip":
+ discard quote do:
+ extractZip(`file`, `outdir`)
+
+macro gitReset*(outdir: static[string]): untyped =
+ echo "Resetting " & outdir
+
+ let cmd = &"cd \"{getProjectPath()/outdir}\" && git reset --hard"
+ while execAction(cmd).contains("Permission denied"):
+ sleep(1000)
+ echo " Retrying ..."
+
+macro gitCheckout*(file, outdir: static[string]): untyped =
+ echo "Resetting " & file
+
+ let cmd = &"cd \"{getProjectPath()/outdir}\" && git checkout $#" % file.replace(outdir & "/", "")
+ while execAction(cmd).contains("Permission denied"):
+ sleep(500)
+ echo " Retrying ..."
+
+macro gitPull*(url: static[string], outdirN = "", plistN = "", checkoutN = ""): untyped =
+ let
+ outdir = getProjectPath()/outdirN.strVal()
+ plist = plistN.strVal()
+ checkout = checkoutN.strVal()
+
+ if dirExists(outdir/".git"):
+ discard quote do:
+ gitReset(`outdirN`)
+ return
+ else:
+ echo execAction(&"mkdir \"{outdir}\"")
+
+ echo "Setting up Git repo: " & url
+ discard execAction(&"cd \"{outdir}\" && git init .")
+ discard execAction(&"cd \"{outdir}\" && git remote add origin " & url)
+
+ if plist.len() != 0:
+ let sparsefile = &"{outdir}/.git/info/sparse-checkout"
+
+ discard execAction(&"cd \"{outdir}\" && git config core.sparsecheckout true")
+ writeFile(sparsefile, plist)
+ echo "Wrote"
+
+ if checkout.len() != 0:
+ echo "Checking out " & checkout
+ discard execAction(&"cd \"{outdir}\" && git pull --tags origin master")
+ discard execAction(&"cd \"{outdir}\" && git checkout {checkout}")
+ else:
+ echo "Pulling repository"
+ discard execAction(&"cd \"{outdir}\" && git pull --depth=1 origin master")