-
Notifications
You must be signed in to change notification settings - Fork 190
/
nimscriptapi.nim
227 lines (188 loc) · 6.75 KB
/
nimscriptapi.nim
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.
## This module is implicitly imported in NimScript .nimble files.
import system except getCommand, setCommand, switch, `--`
import strformat, strutils, tables, sequtils
export tables
when (NimMajor, NimMinor) < (1, 3):
when not defined(nimscript):
import os
else:
import os
var
packageName* = "" ## Set this to the package name. It
## is usually not required to do that, nims' filename is
## the default.
version*: string ## The package's version.
author*: string ## The package's author.
description*: string ## The package's description.
license*: string ## The package's license.
srcDir*: string ## The package's source directory.
binDir*: string ## The package's binary directory.
backend*: string ## The package's backend.
skipDirs*, skipFiles*, skipExt*, installDirs*, installFiles*,
installExt*, bin*: seq[string] = @[] ## Nimble metadata.
requiresData*: seq[string] = @[] ## The package's dependencies.
foreignDeps*: seq[string] = @[] ## The foreign dependencies. Only
## exported for 'distros.nim'.
nimbleTasks: seq[tuple[name, description: string]] = @[]
beforeHooks: seq[string] = @[]
afterHooks: seq[string] = @[]
flags: Table[string, seq[string]]
namedBin*: Table[string, string]
command = "e"
project = ""
success = false
retVal = true
proc requires*(deps: varargs[string]) =
## Call this to set the list of requirements of your Nimble
## package.
for d in deps: requiresData.add(d)
proc getParams(): tuple[scriptFile, projectFile, outFile, actionName: string,
commandLineParams: seq[string]] =
# Called by nimscriptwrapper.nim:execNimscript()
# nim e --flags /full/path/to/file.nims /full/path/to/file.nimble /full/path/to/file.out action
for i in 2 .. paramCount():
let
param = paramStr(i)
if param[0] != '-':
if result.scriptFile.len == 0:
result.scriptFile = param
elif result.projectFile.len == 0:
result.projectFile = param
elif result.outFile.len == 0:
result.outFile = param
elif result.actionName.len == 0:
result.actionName = param.normalize
else:
result.commandLineParams.add param
else:
result.commandLineParams.add param
const
# Command line values are const so that thisDir() works at compile time
(scriptFile, projectFile, outFile, actionName, commandLineParams*) = getParams()
proc getCommand*(): string =
return command
proc setCommand*(cmd: string, prj = "") =
command = cmd
if prj.len != 0:
project = prj
proc switch*(key: string, value="") =
if flags.hasKey(key):
flags[key].add(value)
else:
flags[key] = @[value]
template `--`*(key, val: untyped) =
switch(astToStr(key), strip astToStr(val))
template `--`*(key: untyped) =
switch(astToStr(key), "")
template printIfLen(varName) =
if varName.len != 0:
result &= astToStr(varName) & ": \"\"\"" & varName & "\"\"\"\n"
template printSeqIfLen(name: string, varName: untyped) =
if varName.len != 0:
result &= name & ": \"" & varName.join(", ") & "\"\n"
template printSeqIfLen(varName) =
printSeqIfLen(astToStr(varName), varName)
proc printPkgInfo(): string =
if backend.len == 0:
backend = "c"
# Forward `namedBin` entries in `bin`
for k, v in namedBin:
let idx = bin.find(k)
if idx == -1:
bin.add k & "=" & v
else:
bin[idx] = k & "=" & v
result = "[Package]\n"
if packageName.len != 0:
result &= "name: \"" & packageName & "\"\n"
printIfLen version
printIfLen author
printIfLen description
printIfLen license
printIfLen srcDir
printIfLen binDir
printIfLen backend
printSeqIfLen skipDirs
printSeqIfLen skipFiles
printSeqIfLen skipExt
printSeqIfLen installDirs
printSeqIfLen installFiles
printSeqIfLen installExt
printSeqIfLen bin
printSeqIfLen "nimbleTasks", nimbleTasks.unzip()[0]
printSeqIfLen beforeHooks
printSeqIfLen afterHooks
if requiresData.len != 0:
result &= "\n[Deps]\n"
result &= &"requires: \"{requiresData.join(\", \")}\"\n"
proc onExit*() =
if actionName.len == 0 or actionName == "help":
var maxNameLen = 8
for (name, _) in nimbleTasks:
maxNameLen = max(maxNameLen, name.len)
for (name, description) in nimbleTasks:
echo alignLeft(name, maxNameLen + 2), description
if "printPkgInfo".normalize == actionName:
if outFile.len != 0:
writeFile(outFile, printPkgInfo())
else:
var
output = ""
output &= "\"success\": " & $success & ", "
output &= "\"command\": \"" & command & "\", "
if project.len != 0:
output &= "\"project\": \"" & project & "\", "
if flags.len != 0:
output &= "\"flags\": {"
for key, val in flags.pairs:
output &= "\"" & key & "\": ["
for v in val:
let v = if v.len > 0 and v[0] == '"': strutils.unescape(v)
else: v
output &= v.escape & ", "
output = output[0 .. ^3] & "], "
output = output[0 .. ^3] & "}, "
output &= "\"retVal\": " & $retVal
if outFile.len != 0:
writeFile(outFile, "{" & output & "}")
# TODO: New release of Nim will move this `task` template under a
# `when not defined(nimble)`. This will allow us to override it in the future.
template task*(name: untyped; description: string; body: untyped): untyped =
## Defines a task. Hidden tasks are supported via an empty description.
## Example:
##
## .. code-block:: nim
## task build, "default build is via the C backend":
## setCommand "c"
proc `name Task`*() = body
nimbleTasks.add (astToStr(name), description)
if actionName.len == 0 or actionName == "help":
success = true
elif actionName == astToStr(name).normalize:
success = true
`name Task`()
template before*(action: untyped, body: untyped): untyped =
## Defines a block of code which is evaluated before ``action`` is executed.
proc `action Before`*(): bool =
result = true
body
beforeHooks.add astToStr(action)
if (astToStr(action) & "Before").normalize == actionName:
success = true
retVal = `action Before`()
template after*(action: untyped, body: untyped): untyped =
## Defines a block of code which is evaluated after ``action`` is executed.
proc `action After`*(): bool =
result = true
body
afterHooks.add astToStr(action)
if (astToStr(action) & "After").normalize == actionName:
success = true
retVal = `action After`()
proc getPkgDir*(): string =
## Returns the package directory containing the .nimble file currently
## being evaluated.
result = projectFile.rsplit(seps={'/', '\\', ':'}, maxsplit=1)[0]
proc thisDir*(): string = getPkgDir()