nake.nim 2.2 KB

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