Skip to content

Commit d2c6aac

Browse files
committed
Use package info for nimble path when available (#1344)
This change updates the nimble path command so it respects the local package configuration when executed within a nimble package.
1 parent 20e2e4d commit d2c6aac

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

src/nimble.nim

+29-21
Original file line numberDiff line numberDiff line change
@@ -1130,27 +1130,35 @@ proc listPaths(options: Options) =
11301130
if options.action.packages.len == 0:
11311131
raise nimbleError("A package name needs to be specified")
11321132

1133-
var errors = 0
1134-
let pkgs = getInstalledPkgsMin(options.getPkgsDir(), options)
1135-
for name, version in options.action.packages.items:
1136-
var installed: seq[VersionAndPath] = @[]
1137-
# There may be several, list all available ones and sort by version.
1138-
for pkg in pkgs:
1139-
if name == pkg.basicInfo.name and withinRange(pkg.basicInfo.version, version):
1140-
installed.add((pkg.basicInfo.version, pkg.getRealDir))
1141-
1142-
if installed.len > 0:
1143-
sort(installed, cmp[VersionAndPath], Descending)
1144-
# The output for this command is used by tools so we do not use display().
1145-
for pkg in installed:
1146-
echo pkg.path
1147-
else:
1148-
display("Warning:", "Package '$1' is not installed" % name, Warning,
1149-
MediumPriority)
1150-
errors += 1
1151-
if errors > 0:
1152-
raise nimbleError(
1153-
"At least one of the specified packages was not found")
1133+
let pkgInfo = maybeGetPkgInfo(getCurrentDir(), options)
1134+
if pkgInfo.isSome:
1135+
let searchNames = options.action.packages.mapIt(it.name).toHashSet
1136+
for dep in pkgInfo.get.processAllDependencies(options):
1137+
if dep.basicInfo.name in searchNames:
1138+
for path in dep.expandPaths(options):
1139+
echo path
1140+
else:
1141+
var errors = 0
1142+
let pkgs = getInstalledPkgsMin(options.getPkgsDir(), options)
1143+
for name, version in options.action.packages.items:
1144+
var installed: seq[VersionAndPath] = @[]
1145+
# There may be several, list all available ones and sort by version.
1146+
for pkg in pkgs:
1147+
if name == pkg.basicInfo.name and withinRange(pkg.basicInfo.version, version):
1148+
installed.add((pkg.basicInfo.version, pkg.getRealDir))
1149+
1150+
if installed.len > 0:
1151+
sort(installed, cmp[VersionAndPath], Descending)
1152+
# The output for this command is used by tools so we do not use display().
1153+
for pkg in installed:
1154+
echo pkg.path
1155+
else:
1156+
display("Warning:", "Package '$1' is not installed" % name, Warning,
1157+
MediumPriority)
1158+
errors += 1
1159+
if errors > 0:
1160+
raise nimbleError(
1161+
"At least one of the specified packages was not found")
11541162

11551163
proc join(x: seq[PkgTuple]; y: string): string =
11561164
if x.len == 0: return ""

src/nimblepkg/packageparser.nim

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright (C) Dominik Picheta. All rights reserved.
22
# BSD License. Look at license.txt for more info.
3-
import parsecfg, sets, streams, strutils, os, tables, sugar, strformat
3+
import std/[parsecfg, sets, streams, strutils, os, tables, sugar, strformat, options]
44
from sequtils import apply, map, toSeq
55

66
import common, version, tools, nimscriptwrapper, options, cli, sha1hashes,
@@ -389,6 +389,12 @@ proc getPkgInfo*(dir: string, options: Options, forValidation = false):
389389
let nimbleFile = findNimbleFile(dir, true, options)
390390
result = getPkgInfoFromFile(nimbleFile, options, forValidation)
391391

392+
proc maybeGetPkgInfo*(dir: string, options: Options): Option[PackageInfo] =
393+
try:
394+
return some(getPkgInfo(dir, options))
395+
except NimbleError:
396+
return none(PackageInfo)
397+
392398
proc getInstalledPkgs*(libsDir: string, options: Options): seq[PackageInfo] =
393399
## Gets a list of installed packages.
394400
##

tests/pathWithDevelop/nimble.develop

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": 1,
3+
"includes": [],
4+
"dependencies": [
5+
"../deps"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version = "0.1.0"
2+
author = "Ivan Yonchovski"
3+
description = "Nim package manager."
4+
license = "BSD"
5+
6+
requires "deps"

tests/tpathcommand.nim

+14
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@ suite "path command":
2222
check exitCode == QuitSuccess
2323
check execNimble("path", "[email protected]").exitCode == QuitSuccess
2424
check execNimble("path", "[email protected]").exitCode != QuitSuccess
25+
26+
test "Use current nimble package to determine path when possible":
27+
cd "deps":
28+
check execNimbleYes("install").exitCode == QuitSuccess
29+
let (output, exitCode) = execNimble("path", "timezones")
30+
check(exitCode == QuitSuccess)
31+
check output.strip() == getPackageDir(pkgsDir, "timezones-0.5.4")
32+
33+
test "Respect develop overrides for nimble packages":
34+
cd "pathWithDevelop":
35+
let (output, exitCode) = execNimble("path", "deps")
36+
check(exitCode == QuitSuccess)
37+
checkpoint "Nimble path output was: " & output
38+
check output.startsWith(expandFilename("../deps"))

0 commit comments

Comments
 (0)