-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathoptions.nim
664 lines (600 loc) · 22.7 KB
/
options.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.
import json, strutils, os, parseopt, uri, tables, terminal
import sequtils, sugar
import std/options as std_opt
from httpclient import Proxy, newProxy
import config, version, common, cli
const
nimbledeps* = "nimbledeps"
type
DumpMode* = enum kdumpIni, kdumpJson
Options* = object
forcePrompts*: ForcePrompt
depsOnly*: bool
uninstallRevDeps*: bool
queryVersions*: bool
queryInstalled*: bool
nimbleDir*: string
verbosity*: cli.Priority
action*: Action
config*: Config
nimbleData*: JsonNode ## Nimbledata.json
pkgInfoCache*: TableRef[string, PackageInfo]
showHelp*: bool
showVersion*: bool
noColor*: bool
disableValidation*: bool
continueTestsOnFailure*: bool
## Whether packages' repos should always be downloaded with their history.
forceFullClone*: bool
# Temporary storage of flags that have not been captured by any specific Action.
unknownFlags*: seq[(CmdLineKind, string, string)]
dumpMode*: DumpMode
startDir*: string # Current directory on startup - is top level pkg dir for
# some commands, useful when processing deps
nim*: string # Nim compiler location
localdeps*: bool # True if project local deps mode
developLocaldeps*: bool # True if local deps + nimble develop pkg1 ...
ActionType* = enum
actionNil, actionRefresh, actionInit, actionDump, actionPublish,
actionInstall, actionSearch,
actionList, actionBuild, actionPath, actionUninstall, actionCompile,
actionDoc, actionCustom, actionTasks, actionDevelop, actionCheck,
actionRun
Action* = object
case typ*: ActionType
of actionNil, actionList, actionPublish, actionTasks, actionCheck: nil
of actionRefresh:
optionalURL*: string # Overrides default package list.
of actionInstall, actionPath, actionUninstall, actionDevelop:
packages*: seq[PkgTuple] # Optional only for actionInstall
# and actionDevelop.
passNimFlags*: seq[string]
of actionSearch:
search*: seq[string] # Search string.
of actionInit, actionDump:
projName*: string
vcsOption*: string
of actionCompile, actionDoc, actionBuild:
file*: string
backend*: string
compileOptions: seq[string]
of actionRun:
runFile: Option[string]
compileFlags: seq[string]
runFlags*: seq[string]
of actionCustom:
command*: string
arguments*: seq[string]
custCompileFlags*: seq[string]
custRunFlags*: seq[string]
const
help* = """
Usage: nimble [nimbleopts] COMMAND [cmdopts]
Commands:
install [pkgname, ...] Installs a list of packages.
[-d, --depsOnly] Installs only dependencies of the package.
[opts, ...] Passes options to the Nim compiler.
develop [pkgname, ...] Clones a list of packages for development.
Symlinks the cloned packages or any package
in the current working directory.
check Verifies the validity of a package in the
current working directory.
init [pkgname] Initializes a new Nimble project in the
current directory or if a name is provided a
new directory of the same name.
[--git, --hg] Creates a git/hg repo in the new nimble project.
publish Publishes a package on nim-lang/packages.
The current working directory needs to be the
toplevel directory of the Nimble package.
uninstall [pkgname, ...] Uninstalls a list of packages.
[-i, --inclDeps] Uninstalls package and dependent package(s).
build [opts, ...] [bin] Builds a package. Passes options to the Nim
compiler.
run [opts, ...] [bin] Builds and runs a package.
Binary needs to be specified after any
compilation options if there are several
binaries defined. Any flags after the binary
or -- arg are passed to the binary when it is run.
c, cc, js [opts, ...] f.nim Builds a file inside a package. Passes options
to the Nim compiler.
test Compiles and executes tests
[-c, --continue] Don't stop execution on a failed test.
[opts, ...] Passes options to the Nim compiler.
doc, doc2 [opts, ...] f.nim Builds documentation for a file inside a
package. Passes options to the Nim compiler.
refresh [url] Refreshes the package list. A package list URL
can be optionally specified.
search pkg/tag Searches for a specified package. Search is
performed by tag and by name.
[--ver] Queries remote server for package version.
list Lists all packages.
[--ver] Queries remote server for package version.
[-i, --installed] Lists all installed packages.
tasks Lists the tasks specified in the Nimble
package's Nimble file.
path pkgname ... Shows absolute path to the installed packages
specified.
dump [pkgname] Outputs Nimble package information for
external tools. The argument can be a
.nimble file, a project directory or
the name of an installed package.
Nimble Options:
-h, --help Print this help message.
-v, --version Print version information.
-y, --accept Accept all interactive prompts.
-n, --reject Reject all interactive prompts.
-l, --localdeps Run in project local dependency mode
--ver Query remote server for package version
information when searching or listing packages
--nimbleDir:dirname Set the Nimble directory.
--nim:path Use specified path for Nim compiler
--silent Hide all Nimble and Nim output
--verbose Show all non-debug output.
--debug Show all output including debug messages.
--noColor Don't colorise output.
For more information read the Github readme:
https://github.com/nim-lang/nimble#readme
"""
const noHookActions* = {actionCheck}
proc writeHelp*(quit=true) =
echo(help)
if quit:
raise NimbleQuit(msg: "")
proc writeVersion*() =
echo("nimble v$# compiled at $# $#" %
[nimbleVersion, CompileDate, CompileTime])
const execResult = gorgeEx("git rev-parse HEAD")
when execResult[0].len > 0 and execResult[1] == QuitSuccess:
echo "git hash: ", execResult[0]
else:
{.warning: "Couldn't determine GIT hash: " & execResult[0].}
echo "git hash: couldn't determine git hash"
raise NimbleQuit(msg: "")
proc parseActionType*(action: string): ActionType =
case action.normalize()
of "install":
result = actionInstall
of "path":
result = actionPath
of "build":
result = actionBuild
of "run":
result = actionRun
of "c", "compile", "js", "cpp", "cc":
result = actionCompile
of "doc", "doc2":
result = actionDoc
of "init":
result = actionInit
of "dump":
result = actionDump
of "update", "refresh":
result = actionRefresh
of "search":
result = actionSearch
of "list":
result = actionList
of "uninstall", "remove", "delete", "del", "rm":
result = actionUninstall
of "publish":
result = actionPublish
of "tasks":
result = actionTasks
of "develop":
result = actionDevelop
of "check":
result = actionCheck
else:
result = actionCustom
proc initAction*(options: var Options, key: string) =
## Intialises `options.actions` fields based on `options.actions.typ` and
## `key`.
let keyNorm = key.normalize()
case options.action.typ
of actionInstall, actionPath, actionDevelop, actionUninstall:
options.action.packages = @[]
options.action.passNimFlags = @[]
of actionCompile, actionDoc, actionBuild:
options.action.compileOptions = @[]
options.action.file = ""
if keyNorm == "c" or keyNorm == "compile": options.action.backend = ""
else: options.action.backend = keyNorm
of actionInit:
options.action.projName = ""
options.action.vcsOption = ""
of actionDump:
options.action.projName = ""
options.action.vcsOption = ""
options.forcePrompts = forcePromptYes
of actionRefresh:
options.action.optionalURL = ""
of actionSearch:
options.action.search = @[]
of actionCustom:
options.action.command = key
options.action.arguments = @[]
options.action.custCompileFlags = @[]
options.action.custRunFlags = @[]
of actionPublish, actionList, actionTasks, actionCheck, actionRun,
actionNil: discard
proc prompt*(options: Options, question: string): bool =
## Asks an interactive question and returns the result.
##
## The proc will return immediately without asking the user if the global
## forcePrompts has a value different than dontForcePrompt.
return prompt(options.forcePrompts, question)
proc promptCustom*(options: Options, question, default: string): string =
## Asks an interactive question and returns the result.
##
## The proc will return "default" without asking the user if the global
## forcePrompts is forcePromptYes.
return promptCustom(options.forcePrompts, question, default)
proc promptList*(options: Options, question: string, args: openarray[string]): string =
## Asks an interactive question and returns the result.
##
## The proc will return one of the provided args. If not prompting the first
## options is selected.
return promptList(options.forcePrompts, question, args)
proc getNimbleDir*(options: Options): string =
return options.nimbleDir
proc getPkgsDir*(options: Options): string =
options.getNimbleDir() / "pkgs"
proc getBinDir*(options: Options): string =
options.getNimbleDir() / "bin"
proc setNimbleDir*(options: var Options) =
var
nimbleDir = options.config.nimbleDir
propagate = false
if (options.localdeps and options.action.typ == actionDevelop and
options.action.packages.len != 0):
# Localdeps + nimble develop pkg1 ...
options.developLocaldeps = true
if options.nimbleDir.len != 0:
# --nimbleDir:<dir> takes priority...
nimbleDir = options.nimbleDir
propagate = true
else:
# ...followed by the environment variable.
let env = getEnv("NIMBLE_DIR")
if env.len != 0:
display("Warning:", "Using the environment variable: NIMBLE_DIR='" &
env & "'", Warning, priority = HighPriority)
nimbleDir = env
else:
# ...followed by project local deps mode
if dirExists(nimbledeps) or (options.localdeps and not options.developLocaldeps):
display("Warning:", "Using project local deps mode", Warning,
priority = HighPriority)
nimbleDir = nimbledeps
options.localdeps = true
propagate = true
options.nimbleDir = expandTilde(nimbleDir).absolutePath()
if propagate:
# Propagate custom nimbleDir to child processes
putEnv("NIMBLE_DIR", options.nimbleDir)
# Add $nimbledeps/bin to PATH
let path = getEnv("PATH")
if options.nimbleDir notin path:
putEnv("PATH", options.nimbleDir / "bin" & PathSep & path)
if not options.developLocaldeps:
# Create nimbleDir/pkgs if it doesn't exist - will create nimbleDir as well
let pkgsDir = options.getPkgsDir()
if not dirExists(pkgsDir):
createDir(pkgsDir)
proc parseCommand*(key: string, result: var Options) =
result.action = Action(typ: parseActionType(key))
initAction(result, key)
proc setNimBin*(options: var Options) =
# Find nim binary and set into options
if options.nim.len != 0:
# --nim:<path> takes priority...
if options.nim.splitPath().head.len == 0:
# Just filename, search in PATH - nim_temp shortcut
let pnim = findExe(options.nim)
if pnim.len != 0:
options.nim = pnim
else:
raise newException(NimbleError,
"Unable to find `$1` in $PATH" % options.nim)
elif not options.nim.isAbsolute():
# Relative path
options.nim = expandTilde(options.nim).absolutePath()
if not fileExists(options.nim):
raise newException(NimbleError, "Unable to find `$1`" % options.nim)
else:
# Search PATH
let pnim = findExe("nim")
if pnim.len != 0:
options.nim = pnim
else:
let pnimrod = findExe("nimrod")
if pnimrod.len != 0:
options.nim = pnimrod
if options.nim.len == 0:
# Nim not found in PATH
raise newException(NimbleError,
"Unable to find `nim` binary - add to $PATH or use `--nim`")
proc getNimBin*(options: Options): string =
return options.nim
proc setRunOptions(result: var Options, key, val: string, isArg: bool) =
if result.action.runFile.isNone() and (isArg or val == "--"):
result.action.runFile = some(key)
else:
result.action.runFlags.add(val)
proc parseArgument*(key: string, result: var Options) =
case result.action.typ
of actionNil:
assert false
of actionInstall, actionPath, actionDevelop, actionUninstall:
# Parse pkg@verRange
if '@' in key:
let i = find(key, '@')
let (pkgName, pkgVer) = (key[0 .. i-1], key[i+1 .. key.len-1])
if pkgVer.len == 0:
raise newException(NimbleError, "Version range expected after '@'.")
result.action.packages.add((pkgName, pkgVer.parseVersionRange()))
else:
result.action.packages.add((key, VersionRange(kind: verAny)))
of actionRefresh:
result.action.optionalURL = key
of actionSearch:
result.action.search.add(key)
of actionInit, actionDump:
if result.action.projName != "":
raise newException(
NimbleError, "Can only perform this action on one package at a time."
)
result.action.projName = key
of actionCompile, actionDoc:
result.action.file = key
of actionList, actionPublish:
result.showHelp = true
of actionBuild:
result.action.file = key
of actionRun:
result.setRunOptions(key, key, true)
of actionCustom:
result.action.arguments.add(key)
else:
discard
proc getFlagString(kind: CmdLineKind, flag, val: string): string =
let prefix =
case kind
of cmdShortOption: "-"
of cmdLongOption: "--"
else: ""
if val == "":
return prefix & flag
else:
return prefix & flag & ":" & val
proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
let f = flag.normalize()
# Global flags.
var isGlobalFlag = true
case f
of "help", "h": result.showHelp = true
of "version", "v": result.showVersion = true
of "accept", "y": result.forcePrompts = forcePromptYes
of "reject", "n": result.forcePrompts = forcePromptNo
of "nimbledir": result.nimbleDir = val
of "silent": result.verbosity = SilentPriority
of "verbose": result.verbosity = LowPriority
of "debug": result.verbosity = DebugPriority
of "nocolor": result.noColor = true
of "disablevalidation": result.disableValidation = true
of "nim": result.nim = val
of "localdeps", "l": result.localdeps = true
else: isGlobalFlag = false
var wasFlagHandled = true
# Action-specific flags.
case result.action.typ
of actionSearch, actionList:
case f
of "installed", "i":
result.queryInstalled = true
of "ver":
result.queryVersions = true
else:
wasFlagHandled = false
of actionDump:
case f
of "json": result.dumpMode = kdumpJson
of "ini": result.dumpMode = kdumpIni
else:
wasFlagHandled = false
of actionInstall:
case f
of "depsonly", "d":
result.depsOnly = true
of "passnim", "p":
result.action.passNimFlags.add(val)
else:
if not isGlobalFlag:
result.action.passNimFlags.add(getFlagString(kind, flag, val))
else:
wasFlagHandled = false
of actionInit:
case f
of "git", "hg":
result.action.vcsOption = f
else:
wasFlagHandled = false
of actionUninstall:
case f
of "incldeps", "i":
result.uninstallRevDeps = true
else:
wasFlagHandled = false
of actionCompile, actionDoc, actionBuild:
if not isGlobalFlag:
result.action.compileOptions.add(getFlagString(kind, flag, val))
of actionRun:
result.showHelp = false
result.setRunOptions(flag, getFlagString(kind, flag, val), false)
of actionCustom:
if not isGlobalFlag:
if result.action.command.normalize == "test":
if f == "continue" or f == "c":
result.continueTestsOnFailure = true
# Set run flags for custom task
result.action.custRunFlags.add(getFlagString(kind, flag, val))
else:
wasFlagHandled = false
if not wasFlagHandled and not isGlobalFlag:
result.unknownFlags.add((kind, flag, val))
proc initOptions*(): Options =
# Exported for choosenim
Options(
action: Action(typ: actionNil),
pkgInfoCache: newTable[string, PackageInfo](),
verbosity: HighPriority,
noColor: not isatty(stdout)
)
proc parseMisc(options: var Options) =
# Load nimbledata.json
let nimbledataFilename = options.getNimbleDir() / "nimbledata.json"
if fileExists(nimbledataFilename):
try:
options.nimbleData = parseFile(nimbledataFilename)
except:
raise newException(NimbleError, "Couldn't parse nimbledata.json file " &
"located at " & nimbledataFilename)
else:
options.nimbleData = %{"reverseDeps": newJObject()}
proc handleUnknownFlags(options: var Options) =
if options.action.typ == actionRun:
# ActionRun uses flags that come before the command as compilation flags
# and flags that come after as run flags.
options.action.compileFlags =
map(options.unknownFlags, x => getFlagString(x[0], x[1], x[2]))
options.unknownFlags = @[]
elif options.action.typ == actionCustom:
# actionCustom uses flags that come before the command as compilation flags
# and flags that come after as run flags.
options.action.custCompileFlags =
map(options.unknownFlags, x => getFlagString(x[0], x[1], x[2]))
options.unknownFlags = @[]
else:
# For everything else, handle the flags that came before the command
# normally.
let unknownFlags = options.unknownFlags
options.unknownFlags = @[]
for flag in unknownFlags:
parseFlag(flag[1], flag[2], options, flag[0])
# Any unhandled flags?
if options.unknownFlags.len > 0:
let flag = options.unknownFlags[0]
raise newException(
NimbleError,
"Unknown option: " & getFlagString(flag[0], flag[1], flag[2])
)
proc parseCmdLine*(): Options =
result = initOptions()
# Parse command line params first. A simple `--version` shouldn't require
# a config to be parsed.
for kind, key, val in getOpt():
case kind
of cmdArgument:
if result.action.typ == actionNil:
parseCommand(key, result)
else:
parseArgument(key, result)
of cmdLongOption, cmdShortOption:
parseFlag(key, val, result, kind)
of cmdEnd: assert(false) # cannot happen
handleUnknownFlags(result)
# Set verbosity level.
setVerbosity(result.verbosity)
# Set whether color should be shown.
setShowColor(not result.noColor)
# Parse config.
result.config = parseConfig()
# Parse other things, for example the nimbledata.json file.
parseMisc(result)
if result.action.typ == actionNil and not result.showVersion:
result.showHelp = true
if result.action.typ != actionNil and result.showVersion:
# We've got another command that should be handled. For example:
# nimble run foobar -v
result.showVersion = false
proc getProxy*(options: Options): Proxy =
## Returns ``nil`` if no proxy is specified.
var url = ""
if ($options.config.httpProxy).len > 0:
url = $options.config.httpProxy
else:
try:
if existsEnv("http_proxy"):
url = getEnv("http_proxy")
elif existsEnv("https_proxy"):
url = getEnv("https_proxy")
elif existsEnv("HTTP_PROXY"):
url = getEnv("HTTP_PROXY")
elif existsEnv("HTTPS_PROXY"):
url = getEnv("HTTPS_PROXY")
except ValueError:
display("Warning:", "Unable to parse proxy from environment: " &
getCurrentExceptionMsg(), Warning, HighPriority)
if url.len > 0:
var parsed = parseUri(url)
if parsed.scheme.len == 0 or parsed.hostname.len == 0:
parsed = parseUri("http://" & url)
let auth =
if parsed.username.len > 0: parsed.username & ":" & parsed.password
else: ""
return newProxy($parsed, auth)
else:
return nil
proc briefClone*(options: Options): Options =
## Clones the few important fields and creates a new Options object.
var newOptions = initOptions()
newOptions.config = options.config
newOptions.nimbleData = options.nimbleData
newOptions.nimbleDir = options.nimbleDir
newOptions.forcePrompts = options.forcePrompts
newOptions.pkgInfoCache = options.pkgInfoCache
newOptions.verbosity = options.verbosity
newOptions.nim = options.nim
return newOptions
proc shouldRemoveTmp*(options: Options, file: string): bool =
result = true
if options.verbosity <= DebugPriority:
let msg = "Not removing temporary path because of debug verbosity: " & file
display("Warning:", msg, Warning, MediumPriority)
return false
proc getCompilationFlags*(options: var Options): var seq[string] =
case options.action.typ
of actionBuild, actionDoc, actionCompile:
return options.action.compileOptions
of actionRun:
return options.action.compileFlags
of actionCustom:
return options.action.custCompileFlags
else:
assert false
proc getCompilationFlags*(options: Options): seq[string] =
var opt = options
return opt.getCompilationFlags()
proc getCompilationBinary*(options: Options, pkgInfo: PackageInfo): Option[string] =
case options.action.typ
of actionBuild, actionDoc, actionCompile:
let file = options.action.file.changeFileExt("")
if file.len > 0:
return some(file)
of actionRun:
let optRunFile = options.action.runFile
let runFile =
if optRunFile.get("").len > 0:
optRunFile.get()
elif pkgInfo.bin.len == 1:
toSeq(pkgInfo.bin.keys)[0]
else:
""
if runFile.len > 0:
return some(runFile.changeFileExt(ExeExt))
else:
discard
proc isInstallingTopLevel*(options: Options, dir: string): bool =
return options.startDir == dir