Skip to content

Commit

Permalink
Adds requires flag which allows to add extra packages to the depend…
Browse files Browse the repository at this point in the history
…ency resolution (#1266)

* Adds `requires` flag which allows to add extra packages to the dependency resolution

* adds `used` to test

* Update src/nimble.nim

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
  • Loading branch information
jmgomez and Araq authored Oct 1, 2024
1 parent 4fb6f8e commit 1114068
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ proc processFreeDependenciesSAT(rootPkgInfo: PackageInfo, options: Options): Has
return satProccesedPackages
var solvedPkgs = newSeq[SolvedPackage]()
var pkgsToInstall: seq[(string, Version)] = @[]
var rootPkgInfo = rootPkgInfo
rootPkgInfo.requires &= options.extraRequires
var pkgList = initPkgList(rootPkgInfo, options).mapIt(it.toFullInfo(options))
var allPkgsInfo: seq[PackageInfo] = pkgList & rootPkgInfo
#Remove from the pkglist the packages that exists in lock file and has a different vcsRevision
Expand Down Expand Up @@ -153,12 +155,14 @@ proc processFreeDependencies(pkgInfo: PackageInfo,
## during build phase.
assert not pkgInfo.isMinimal,
"processFreeDependencies needs pkgInfo.requires"

var requirements = requirements
var pkgList {.global.}: seq[PackageInfo]
once:
pkgList = initPkgList(pkgInfo, options)
if options.useSatSolver:
return processFreeDependenciesSAT(pkgInfo, options)
else:
requirements.add options.extraRequires

display("Verifying", "dependencies for $1@$2" %
[pkgInfo.basicInfo.name, $pkgInfo.basicInfo.version],
Expand Down
4 changes: 4 additions & 0 deletions src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type
# For now, it is used only by the run action and it is ignored by others.
pkgCachePath*: string # Cache used to store package downloads
useSatSolver*: bool
extraRequires*: seq[PkgTuple] # extra requires parsed from the command line

ActionType* = enum
actionNil, actionRefresh, actionInit, actionDump, actionPublish, actionUpgrade
Expand Down Expand Up @@ -245,6 +246,7 @@ Nimble Options:
--useSystemNim Use system nim and ignore nim from the lock
file if any
--solver:sat|legacy Use the SAT solver or the legacy (default) for dependency resolution.
--requires Add extra packages to the dependency resolution. Uses the same syntax as the Nimble file. Example: nimble install --requires "pkg1; pkg2 >= 1.2"
For more information read the GitHub readme:
https://github.com/nim-lang/nimble#readme
Expand Down Expand Up @@ -566,6 +568,8 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
result.useSatSolver = false
else:
raise nimbleError("Unknown solver option: " & val)
of "requires":
result.extraRequires = val.split(";").mapIt(it.strip.parseRequires())
else: isGlobalFlag = false

var wasFlagHandled = true
Expand Down
6 changes: 6 additions & 0 deletions tests/requireflag/requireflag.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version = "0.1.0"
author = "Ivan Yonchovski"
description = "Nim package manager."
license = "BSD"

requires "timezones == 0.5.4"
1 change: 1 addition & 0 deletions tests/requireflag/src/def.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "def727"
1 change: 1 addition & 0 deletions tests/tester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import tuninstall
import ttaskdeps
import tsat
import tniminstall
import trequireflag
# nonim tests are very slow and (often) break the CI.

# import tnonim
21 changes: 21 additions & 0 deletions tests/trequireflag.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{.used.}
import unittest, os
import testscommon
from nimblepkg/common import cd

suite "requires flag":
test "can add additional requirements to package with legacy solver":
cleanDir(installDir)
cd "requireflag":
let (outp, exitCode) = execNimble("--requires: stew; results > 0.1", "--solver:legacy", "install")
check exitCode == QuitSuccess
check outp.processOutput.inLines("Success: results installed successfully.")
check outp.processOutput.inLines("Success: stew installed successfully.")

test "can add additional requirements to package with sat solver":
cleanDir(installDir)
cd "requireflag":
let (outp, exitCode) = execNimble("--requires: stew; results > 0.1", "--solver:sat", "install")
check exitCode == QuitSuccess
check outp.processOutput.inLines("Success: results installed successfully.")
check outp.processOutput.inLines("Success: stew installed successfully.")

0 comments on commit 1114068

Please sign in to comment.