Skip to content

Commit

Permalink
Adds the paths feature option (#1185)
Browse files Browse the repository at this point in the history
* Adds the `paths` feature option

##1166

* re-enable tests

* do not allow `paths` escape the package root dir

* improves `isSubdirOf`
  • Loading branch information
jmgomez authored Apr 4, 2024
1 parent 75c88fe commit a6443cd
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The Nimble changelog can be found




## Repository information

This repository has two main branches: `master` and `stable`.
Expand Down
36 changes: 29 additions & 7 deletions src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ proc processFreeDependencies(pkgInfo: PackageInfo,
for i in reverseDependencies:
addRevDep(options.nimbleData, i, pkgInfo)

proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string],
proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[seq[string]],
args: seq[string], options: Options) =
## Builds a package as specified by ``pkgInfo``.
# Handle pre-`build` hook.
Expand All @@ -184,7 +184,8 @@ proc buildFromDir(pkgInfo: PackageInfo, paths: HashSet[string],
args = args
args.add "-d:NimblePkgVersion=" & $pkgInfo.basicInfo.version
for path in paths:
args.add("--path:" & path.quoteShell)
for p in path:
args.add("--path:" & p.quoteShell)
if options.verbosity >= HighPriority:
# Hide Nim hints by default
args.add("--hints:off")
Expand Down Expand Up @@ -337,7 +338,7 @@ proc processLockedDependencies(pkgInfo: PackageInfo, options: Options):
HashSet[PackageInfo]

proc getDependenciesPaths(pkgInfo: PackageInfo, options: Options):
HashSet[string]
HashSet[seq[string]]

proc processAllDependencies(pkgInfo: PackageInfo, options: Options):
HashSet[PackageInfo] =
Expand All @@ -356,6 +357,25 @@ proc allDependencies(pkgInfo: PackageInfo, options: Options): HashSet[PackageInf
for requires in pkgInfo.taskRequires.values:
result.incl pkgInfo.processFreeDependencies(requires, options)

proc isSubdirOf(subdir, baseDir: string): bool =
let
normalizedSubdir = subdir.normalizedPath
normalizedBaseDir = baseDir.normalizedPath & DirSep

when defined(windows):
normalizedSubdir.toLower.startsWith(normalizedBaseDir.toLower)
else:
normalizedSubdir.startsWith(normalizedBaseDir)

proc expandPaths(pkgInfo: PackageInfo, options: Options): seq[string] =
var pkgInfo = pkgInfo.toFullInfo(options)
let baseDir = pkgInfo.getRealDir()
result = @[baseDir]
for relativePath in pkgInfo.paths:
let path = baseDir & "/" & relativePath
if path.isSubdirOf(baseDir):
result.add path

proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
url: string, first: bool, fromLockFile: bool,
vcsRevision = notSetSha1Hash,
Expand Down Expand Up @@ -440,7 +460,7 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
# if the build fails then the old package will still be installed.

if pkgInfo.bin.len > 0 and not isNimPackage:
let paths = result.deps.map(dep => dep.getRealDir())
let paths = result.deps.map(dep => dep.expandPaths(options))
let flags = if options.action.typ in {actionInstall, actionPath, actionUninstall, actionDevelop}:
options.action.passNimFlags
else:
Expand Down Expand Up @@ -781,9 +801,9 @@ proc install(packages: seq[PkgTuple], options: Options,
raise

proc getDependenciesPaths(pkgInfo: PackageInfo, options: Options):
HashSet[string] =
HashSet[seq[string]] =
let deps = pkgInfo.processAllDependencies(options)
return deps.map(dep => dep.getRealDir())
return deps.map(dep => dep.expandPaths(options))

proc build(pkgInfo: PackageInfo, options: Options) =
## Builds the package `pkgInfo`.
Expand Down Expand Up @@ -1123,6 +1143,7 @@ proc dump(options: Options) =
fn "binDir", p.binDir
fn "srcDir", p.srcDir
fn "backend", p.backend
fn "paths", p.paths
if json:
s = j.pretty
echo s
Expand Down Expand Up @@ -1502,7 +1523,8 @@ proc updatePathsFile(pkgInfo: PackageInfo, options: Options) =
let paths = pkgInfo.getDependenciesPaths(options)
var pathsFileContent = "--noNimblePath\n"
for path in paths:
pathsFileContent &= &"--path:{path.escape}\n"
for p in path:
pathsFileContent &= &"--path:{p.escape}\n"
var action = if fileExists(nimblePathsFileName): "updated" else: "generated"
writeFile(nimblePathsFileName, pathsFileContent)
displayInfo(&"\"{nimblePathsFileName}\" is {action}.")
Expand Down
3 changes: 2 additions & 1 deletion src/nimblepkg/nimscriptapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var
backend*: string ## The package's backend.

skipDirs*, skipFiles*, skipExt*, installDirs*, installFiles*,
installExt*, bin*: seq[string] = @[] ## Nimble metadata.
installExt*, bin*, paths*: seq[string] = @[] ## Nimble metadata.
requiresData*: seq[string] = @[] ## The package's dependencies.
taskRequiresData*: Table[string, seq[string]] ## Task dependencies
foreignDeps*: seq[string] = @[] ## The foreign dependencies. Only
Expand Down Expand Up @@ -141,6 +141,7 @@ proc printPkgInfo(): string =
printSeqIfLen installDirs
printSeqIfLen installFiles
printSeqIfLen installExt
printSeqIfLen paths
printSeqIfLen bin
printSeqIfLen "nimbleTasks", nimbleTasks.unzip()[0]
printSeqIfLen beforeHooks
Expand Down
3 changes: 2 additions & 1 deletion src/nimblepkg/packageinfotypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ type
lockedDeps*: AllLockFileDeps
metaData*: PackageMetaData
isLink*: bool

paths*: seq[string]

Package* = object ## Definition of package from packages.json.
# Required fields in a package.
name*: string
Expand Down
2 changes: 2 additions & 0 deletions src/nimblepkg/packageparser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ proc readPackageInfoFromNimble(path: string; result: var PackageInfo) =
of "afterhooks":
for i in ev.value.multiSplit:
result.postHooks.incl(i.normalize)
of "paths":
result.paths.add(ev.value.multiSplit)
else:
raise nimbleError("Invalid field: " & ev.key)
of "deps", "dependencies":
Expand Down
1 change: 1 addition & 0 deletions tests/testdump/testdump.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ description = "Test package for dump command"
version = "0.1.0"
author = "nigredo-tori"
license = "BSD"
paths = @["path"]
6 changes: 5 additions & 1 deletion tests/tnimbledump.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ bin: ""
binDir: ""
srcDir: ""
backend: "c"
paths: "path"
"""
let (outp, exitCode) = execNimble("dump", "--ini", "testdump")
check: exitCode == 0
Expand All @@ -77,7 +78,10 @@ backend: "c"
"bin": [],
"binDir": "",
"srcDir": "",
"backend": "c"
"backend": "c",
"paths": [
"path"
]
}
"""
let (outp, exitCode) = execNimble("dump", "--json", "testdump")
Expand Down

0 comments on commit a6443cd

Please sign in to comment.