Skip to content

Commit 0ac2b44

Browse files
authored
Adds support to bin in the declarative parser (#1357)
1 parent 15b8fc3 commit 0ac2b44

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/nimblepkg/declarativeparser.nim

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import std/strutils
55

66
import compiler/[ast, idents, msgs, syntaxes, options, pathutils, lineinfos]
77
import version, packageinfotypes, packageinfo, options, packageparser
8-
import std/[tables, sequtils, strscans]
8+
import std/[tables, sequtils, strscans, strformat]
99

1010
type NimbleFileInfo* = object
1111
nimbleFile*: string
@@ -14,6 +14,7 @@ type NimbleFileInfo* = object
1414
version*: string
1515
tasks*: seq[(string, string)]
1616
features*: Table[string, seq[string]]
17+
bin*: Table[string, string]
1718
hasInstallHooks*: bool
1819
hasErrors*: bool
1920

@@ -31,6 +32,20 @@ proc extractRequires(n: PNode, conf: ConfigRef, result: var seq[string], hasErro
3132
localError(conf, ch.info, "'requires' takes string literals")
3233
hasErrors = true
3334

35+
proc extractSeqLiteral(n: PNode, conf: ConfigRef, varName: string): seq[string] =
36+
## Extracts a sequence literal of the form @["item1", "item2"]
37+
if n.kind == nkPrefix and n[0].kind == nkIdent and n[0].ident.s == "@":
38+
if n[1].kind == nkBracket:
39+
for item in n[1]:
40+
if item.kind in {nkStrLit .. nkTripleStrLit}:
41+
result.add item.strVal
42+
else:
43+
localError(conf, item.info, &"'{varName}' sequence items must be string literals")
44+
else:
45+
localError(conf, n.info, &"'{varName}' must be assigned a sequence of strings")
46+
else:
47+
localError(conf, n.info, &"'{varName}' must be assigned a sequence with @ prefix")
48+
3449
proc extract(n: PNode, conf: ConfigRef, result: var NimbleFileInfo) =
3550
case n.kind
3651
of nkStmtList, nkStmtListExpr:
@@ -83,6 +98,12 @@ proc extract(n: PNode, conf: ConfigRef, result: var NimbleFileInfo) =
8398
else:
8499
localError(conf, n[1].info, "assignments to 'version' must be string literals")
85100
result.hasErrors = true
101+
elif n[0].kind == nkIdent and eqIdent(n[0].ident.s, "bin"):
102+
let binSeq = extractSeqLiteral(n[1], conf, "bin")
103+
for bin in binSeq:
104+
result.bin[bin] = bin
105+
else:
106+
discard
86107
else:
87108
discard
88109

@@ -216,13 +237,14 @@ proc toRequiresInfo*(pkgInfo: PackageInfo, options: Options): PackageInfo =
216237
#we need to use the vm to get the version. Another option could be to use the binary and ask for the version
217238
if pkgInfo.basicInfo.name.isNim:
218239
return pkgInfo.toFullInfo(options)
219-
240+
220241
let nimbleFileInfo = extractRequiresInfo(pkgInfo.myPath)
221242
result = pkgInfo
222243
result.requires = getRequires(nimbleFileInfo, result.activeFeatures)
223244
if pkgInfo.infoKind != pikFull: #dont update as full implies pik requires
224245
result.infoKind = pikRequires
225246
result.features = getFeatures(nimbleFileInfo)
247+
result.bin = nimbleFileInfo.bin
226248

227249
when isMainModule:
228250
for x in tokenizeRequires("jester@#head >= 1.5 & <= 1.8"):

tests/features/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nimble.develop
2+
nimble.paths

tests/tdeclarativeparser.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ suite "Declarative parsing":
3232
@["nim", "json_rpc", "with", "chronicles", "serialization", "stew", "regex"]
3333
for pkg in expectedPkgs:
3434
check pkg in requires.mapIt(it[0])
35+
36+
test "should parse bin from a nimble file":
37+
let nimbleFile = getNimbleFileFromPkgNameHelper("nimlangserver")
38+
let nimbleFileInfo = extractRequiresInfo(nimbleFile)
39+
check nimbleFileInfo.bin.len == 1
40+
check nimbleFileInfo.bin["nimlangserver"] == "nimlangserver"
3541

3642
test "should be able to get all the released PackageVersions from a git local repository using the declarative parser":
3743
var options = initOptions()

0 commit comments

Comments
 (0)