From 9c7c88d41a3a4f59602f1e4b42635551379ccc99 Mon Sep 17 00:00:00 2001 From: kitsunyan Date: Sat, 5 May 2018 21:42:14 +0300 Subject: Copy built packages to package cache directory --- lib/install.nim | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lib/install.nim (limited to 'lib') diff --git a/lib/install.nim b/lib/install.nim new file mode 100644 index 0000000..cec9ca1 --- /dev/null +++ b/lib/install.nim @@ -0,0 +1,56 @@ +import future, os, posix, sequtils, strutils + +let params = commandLineParams() +let destination = params[0] +let paramsStart = 1 + +proc splitCommands(index: int, res: seq[seq[string]]): seq[seq[string]] = + if index < params.len: + let count = params[index].parseInt + let args = params[index + 1 .. index + count] + splitCommands(index + count + 1, res & args) + else: + res + +let commands = splitCommands(paramsStart, @[]) +let targets = lc[x | (y <- commands[1 .. ^1], x <- y), string] + +for file in targets: + try: + let index = file.rfind("/") + let name = if index >= 0: file[index + 1 .. ^1] else: file + copyFile(file, destination & "/" & name) + except: + discard + +proc perror*(s: cstring): void {.importc, header: "".} +template perror*: void = perror(getAppFilename()) + +proc runCommand(params: seq[string]): int = + if params.len > 0: + let pid = fork() + if pid == 0: + let cexec = allocCStringArray(params) + let code = execvp(cexec[0], cexec) + perror() + deallocCStringArray(cexec) + quit(code) + else: + var status: cint = 1 + discard waitpid(pid, status, 0) + if WIFEXITED(status): + WEXITSTATUS(status) + else: + discard kill(getpid(), status) + 1 + else: + 0 + +proc buildParams(index: int, asArg: string): seq[string] = + if commands[index].len > 0: commands[0] & asArg & "--" & commands[index] else: @[] + +let asdepsCode = runCommand(buildParams(1, "--asdeps")) +if asdepsCode == 0: + programResult = runCommand(buildParams(2, "--asexplicit")) +else: + programResult = asdepsCode -- cgit v1.2.3-70-g09d2