nake.nim 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import std/exitprocs
  54. addExitProc(proc() {.noconv.} =
  55. var
  56. task: string
  57. printTaskList: bool
  58. for kind, key, val in getopt():
  59. case kind
  60. of cmdLongOption, cmdShortOption:
  61. case key.tolowerAscii
  62. of "tasks", "t":
  63. printTaskList = true
  64. else:
  65. echo "Unknown option: ", key, ": ", val
  66. of cmdArgument:
  67. task = key
  68. else: discard
  69. if printTaskList or task.len == 0 or not(tasks.hasKey(task)):
  70. echo "Available tasks:"
  71. for name, task in pairs(tasks):
  72. echo name, " - ", task.desc
  73. quit 0
  74. tasks[task].action())