Debian Codegraph @

tests/manyloc/keineschweine/dependencies/nake/nake.nim

raw
1 discard """
2 DO AS THOU WILST PUBLIC LICENSE
3
4 Whoever should stumble upon this document is henceforth and forever
5 entitled to DO AS THOU WILST with aforementioned document and the
6 contents thereof.
7
8 As said in the Olde Country, `Keepe it Gangster'."""
9
10 #[
11 xxx remove this? seems mostly duplicate of: tests/manyloc/nake/nake.nim
12 ]#
13
14 import strutils, parseopt, tables, os
15
16 type
17 PTask* = ref object
18 desc*: string
19 action*: TTaskFunction
20 TTaskFunction* = proc() {.closure.}
21 var
22 tasks* = initTable[string, PTask](16)
23
24 proc newTask*(desc: string; action: TTaskFunction): PTask
25 proc runTask*(name: string) {.inline.}
26 proc shell*(cmd: varargs[string, `$`]): int {.discardable.}
27 proc cd*(dir: string) {.inline.}
28
29 template nakeImports*(): stmt {.immediate.} =
30 import tables, parseopt, strutils, os
31
32 template task*(name: string; description: string; body: stmt): stmt {.dirty, immediate.} =
33 block:
34 var t = newTask(description, proc() {.closure.} =
35 body)
36 tasks[name] = t
37
38 proc newTask*(desc: string; action: TTaskFunction): PTask =
39 new(result)
40 result.desc = desc
41 result.action = action
42 proc runTask*(name: string) = tasks[name].action()
43
44 proc shell*(cmd: varargs[string, `$`]): int =
45 result = execShellCmd(cmd.join(" "))
46 proc cd*(dir: string) = setCurrentDir(dir)
47 template withDir*(dir: string; body: stmt): stmt =
48 ## temporary cd
49 ## withDir "foo":
50 ## # inside foo
51 ## #back to last dir
52 var curDir = getCurrentDir()
53 cd(dir)
54 body
55 cd(curDir)
56
57 when true:
58 if not fileExists("nakefile.nim"):
59 echo "No nakefile.nim found. Current working dir is ", getCurrentDir()
60 quit 1
61 var args = ""
62 for i in 1..paramCount():
63 args.add paramStr(i)
64 args.add " "
65 quit(shell("nim", "c", "-r", "nakefile.nim", args))
66 else:
67 import std/exitprocs
68 addExitProc(proc() {.noconv.} =
69 var
70 task: string
71 printTaskList: bool
72 for kind, key, val in getopt():
73 case kind
74 of cmdLongOption, cmdShortOption:
75 case key.tolower
76 of "tasks", "t":
77 printTaskList = true
78 else:
79 echo "Unknown option: ", key, ": ", val
80 of cmdArgument:
81 task = key
82 else: nil
83 if printTaskList or task.isNil or not(tasks.hasKey(task)):
84 echo "Available tasks:"
85 for name, task in pairs(tasks):
86 echo name, " - ", task.desc
87 quit 0
88 tasks[task].action())