From 4e6a207ce3d764f73ce3294723c263e38d9b68d3 Mon Sep 17 00:00:00 2001 From: kitsunyan Date: Sun, 10 Jun 2018 00:52:58 +0300 Subject: Refactor tools --- Makefile | 21 +++++++++----------- lib/bisect.nim | 59 +++++++++++++++++++++++++++----------------------------- lib/install.nim | 60 ++++++++++++++++++++++++++++----------------------------- lib/tools.nim | 19 ++++++++++++++++++ 4 files changed, 86 insertions(+), 73 deletions(-) create mode 100644 lib/tools.nim diff --git a/Makefile b/Makefile index a3c9ff7..9a5b0aa 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,7 @@ MAN_PAGES = \ TARGETS = \ completion/bash \ completion/zsh \ - lib/bisect \ - lib/install \ + lib/tools \ src/pakku \ ${MAN_PAGES} @@ -113,16 +112,10 @@ ${MAN_PAGES}: ${MAN_PAGES:=.in} -e '/^[\.'"'"']\\"/d' \ < "${@:=.in}" > "$@" -lib/bisect: lib/bisect.nim +lib/tools: lib/tools.nim $(shell find lib -name \*.nim) @echo "NIM: $@" @nim c ${NIM_OPTIONS} \ - --nimcache:"${NIM_CACHE_DIR}/bisect" \ - -o:"$@" "$<" - -lib/install: lib/install.nim - @echo "NIM: $@" - @nim c ${NIM_OPTIONS} \ - --nimcache:"${NIM_CACHE_DIR}/install" \ + --nimcache:"${NIM_CACHE_DIR}/tools" \ -o:"$@" "$<" src/pakku: src/main.nim $(shell find src -name \*.nim) @@ -154,8 +147,11 @@ install: $(call install,644,'completion/zsh','${ZSHCOMPLETIONSDIR}/_pakku') $(call install,644,'doc/pakku.8','${MANDIR}/man8/pakku.8') $(call install,644,'doc/pakku.conf.5','${MANDIR}/man5/pakku.conf.5') - $(call install,755,'lib/bisect','${PKGLIBDIR}/bisect') - $(call install,755,'lib/install','${PKGLIBDIR}/install') + $(call install,755,'lib/tools','${PKGLIBDIR}/tools') + @echo 'INSTALL: ${PKGLIBDIR}/bisect' + @ln -s tools ${DESTDIR}${PKGLIBDIR}/bisect + @echo 'INSTALL: ${PKGLIBDIR}/install' + @ln -s tools ${DESTDIR}${PKGLIBDIR}/install $(call install,755,'src/pakku','${BINDIR}/pakku') $(call install,644,'pakku.conf','${SYSCONFDIR}/pakku.conf') @@ -164,6 +160,7 @@ uninstall: $(call uninstall,'${ZSHCOMPLETIONSDIR}','_pakku') $(call uninstall,'${MANDIR}/man8','pakku.8') $(call uninstall,'${MANDIR}/man5','pakku.conf.5') + $(call uninstall,'${PKGLIBDIR}','tools') $(call uninstall,'${PKGLIBDIR}','bisect') $(call uninstall,'${PKGLIBDIR}','install') $(call uninstall,'${BINDIR}','pakku') diff --git a/lib/bisect.nim b/lib/bisect.nim index 55d5a60..ea4063c 100644 --- a/lib/bisect.nim +++ b/lib/bisect.nim @@ -1,4 +1,4 @@ -import future, os, osproc, re, strutils +import future, osproc, strutils {.passL: "-lalpm".} @@ -14,36 +14,33 @@ proc getSourceVersion(relativePath: string): seq[string] = else: @[] -let params = commandLineParams() -let compareMethod = params[0] -let relativePath = params[1] -let compareVersion = params[2] +proc handleBisect*(params: seq[string]): int = + let compareMethod = params[0] + let relativePath = params[1] + let compareVersion = params[2] -let (currentVersion, supported) = if compareMethod == "source": - (getSourceVersion(relativePath), true) - else: - (@[], false) - -if not supported: - programResult = 255 -elif currentVersion.len != 3: - programResult = 125 -else: - let epoch = currentVersion[0].strip - let pkgver = currentVersion[1].strip - let pkgrel = currentVersion[2].strip - - if pkgver.len == 0 or pkgrel.len == 0: - programResult = 125 + let (currentVersion, supported) = if compareMethod == "source": + (getSourceVersion(relativePath), true) + else: + (@[], false) + + if not supported: + 255 + elif currentVersion.len != 3: + 125 else: - let version = if epoch.len > 0: - epoch & ":" & pkgver & "-" & pkgrel - else: - pkgver & "-" & pkgrel - - # this output is required by main program - echo(version) - if vercmp(compareVersion, version) > 0: - programResult = 0 + let epoch = currentVersion[0].strip + let pkgver = currentVersion[1].strip + let pkgrel = currentVersion[2].strip + + if pkgver.len == 0 or pkgrel.len == 0: + 125 else: - programResult = 1 + let version = if epoch.len > 0: + epoch & ":" & pkgver & "-" & pkgrel + else: + pkgver & "-" & pkgrel + + # this output is required by main program + echo(version) + if vercmp(compareVersion, version) > 0: 0 else: 1 diff --git a/lib/install.nim b/lib/install.nim index 67e5f96..9539a62 100644 --- a/lib/install.nim +++ b/lib/install.nim @@ -1,35 +1,15 @@ -import future, os, posix, sequtils, strutils +import future, os, posix, strutils -let params = commandLineParams() -let destination = params[0] -let uid = params[1].parseInt -let gid = params[2].parseInt -let paramsStart = 3 - -proc splitCommands(index: int, res: seq[seq[string]]): seq[seq[string]] = +proc splitCommands(params: seq[string], 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) + splitCommands(params, index + count + 1, res & args) else: res -let commands = splitCommands(paramsStart, @[]) -let targets = lc[x | (y <- commands[1 .. ^1], x <- y), string] - -if uid >= 0 and gid >= 0: - for file in targets: - try: - let index = file.rfind("/") - let name = if index >= 0: file[index + 1 .. ^1] else: file - let dest = destination & "/" & name - copyFile(file, dest) - discard chown(dest, (Uid) uid, (Gid) gid) - except: - discard - proc perror*(s: cstring): void {.importc, header: "".} -template perror*: void = perror(getAppFilename()) +template perror*: void = perror(paramStr(0)) proc runCommand(params: seq[string]): int = if params.len > 0: @@ -51,11 +31,31 @@ proc runCommand(params: seq[string]): int = else: 0 -proc buildParams(index: int, asArg: string): seq[string] = +proc buildParams(commands: seq[seq[string]], 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 +proc handleInstall*(params: seq[string]): int = + let destination = params[0] + let uid = params[1].parseInt + let gid = params[2].parseInt + let paramsStart = 3 + + let commands = splitCommands(params, paramsStart, @[]) + let targets = lc[x | (y <- commands[1 .. ^1], x <- y), string] + + if uid >= 0 and gid >= 0: + for file in targets: + try: + let index = file.rfind("/") + let name = if index >= 0: file[index + 1 .. ^1] else: file + let dest = destination & "/" & name + copyFile(file, dest) + discard chown(dest, (Uid) uid, (Gid) gid) + except: + discard + + let asdepsCode = runCommand(commands.buildParams(1, "--asdeps")) + if asdepsCode == 0: + runCommand(commands.buildParams(2, "--asexplicit")) + else: + asdepsCode diff --git a/lib/tools.nim b/lib/tools.nim new file mode 100644 index 0000000..82cafff --- /dev/null +++ b/lib/tools.nim @@ -0,0 +1,19 @@ +import os, ospaths +import bisect, install + +let fileName = paramStr(0).splitFile().name +let appName = getAppFilename().splitFile().name + +let paramsFull = commandLineParams() +let (tool, params) = if fileName == appName: + (paramsFull[0], paramsFull[1 .. ^1]) + else: + (fileName, paramsFull) + +programResult = case tool: + of "bisect": + handleBisect(params) + of "install": + handleInstall(params) + else: + 1 \ No newline at end of file -- cgit v1.2.3-70-g09d2