-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskutils.nimble
189 lines (129 loc) · 3.58 KB
/
taskutils.nimble
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
func libSrcDir (): string =
"src"
version = "0.2.2"
author = "thenjip"
description = "Various utilities for tasks in NimScript."
license = "MIT"
srcDir = libSrcDir()
requires "nim >= 1.4.0"
import std/[macros, os, sequtils, strformat, strutils, sugar]
func nimbleProjectName (): string =
"taskutils"
func nimbleCache (): string =
".nimblecache"
func nimbleCacheDir (): string =
getCurrentDir() / nimbleCache()
func examplesDir (): string =
"examples"
type
Example {.pure.} = enum
CustomTask
CustomTaskOutputDir
TasksCommonEnvVars
func projectDir (self: Example): string =
const dirs: array[Example, string] = [
examplesDir() / "custom_task",
examplesDir() / "custom_task_&_output_dir",
examplesDir() / "tasks_&_common_env_vars"
]
dirs[self]
func cmdList (self: Example): seq[string] =
const
docsTask = ["nimble", "docs"].join($' ')
testTask = ["nimble", "test"].join($' ')
cmdLists: array[Example, seq[string]] = [
@[docsTask],
@[
docsTask,
["OUTDIR=\"cache/docs\"", docsTask].join($' ')
],
@[
docsTask,
["OUTDIR=\"cache/docs\"", docsTask].join($' '),
testTask,
["OUTDIR=\"cache/test\"", testTask].join($' '),
["OUTDIR=\"cache/test\"", "NIM_BACKEND=js", testTask].join($' ')
]
]
cmdLists[self]
type
Task {.pure.} = enum
Test,
TestExamples,
Docs,
Clean
func name (self: Task): string =
const names: array[Task, string] = [
"test",
"test_examples",
"docs",
"clean"
]
names[self]
macro identifier (self: static Task): untyped =
self.name().newLit()
func description (self: Task): string =
const descriptions: array[Task, string] = [
"Run the tests.",
"Run the tasks in each example projects.",
"Generate the API documentation.",
"Clean the build directory."
]
descriptions[self]
macro define (self: static Task; body: Task -> void): untyped =
let
selfIdent = self.name().ident()
selfLit = self.newLit()
quote do:
task `selfIdent`, `selfLit`.description():
`body`(`selfLit`)
Task.Test.define(
proc (_: Task) =
func testsDir (): string =
"tests"
func runTestCmd (test: string): string =
["e", test].join($' ')
withDir testsDir():
for kind, path in getCurrentDir().walkDir():
if kind == pcFile and path.endsWith(fmt"{ExtSep}nims"):
let test = path
echo(fmt"Suite: {test.splitFile().name}")
test.runTestCmd().selfExec()
)
Task.TestExamples.define(
proc (_: Task) =
for example in Example:
withDir example.projectDir():
for cmd in example.cmdList():
cmd.exec()
)
Task.Docs.define(
proc (task: Task) =
func outputDir (): string =
nimbleCacheDir() / task.name()
func genDocCmd (): string =
const
repoUrl = "https://github.com/thenjip/taskutils"
mainGitBranch = "main"
mainModule = libSrcDir() / nimbleProjectName().addFileExt("nim")
@["doc"]
.concat(
[
"project",
fmt"outdir:{outputDir().quoteShell()}",
fmt"git.url:{repoUrl.quoteShell()}",
fmt"git.devel:{mainGitBranch}",
fmt"git.commit:{mainGitBranch}"
].map(opt => fmt"--{opt}"),
@[mainModule.quoteShell()]
).join($' ')
genDocCmd().selfExec()
withDir outputDir():
"theindex".addFileExt("html").cpFile("index".addFileExt("html"))
)
Task.Clean.define(
proc (_: Task) =
proc tryRmDir (dir: string) =
dir.rmDir()
nimbleCacheDir().tryRmDir()
)