Add git support

This commit is contained in:
Ganesh Viswanathan 2018-11-20 22:56:30 -06:00
commit 63585e889e
3 changed files with 96 additions and 1 deletions

View file

@ -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.

View file

@ -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

94
nimterop/git.nim Normal file
View file

@ -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")