Skip to content

Commit 66a8c01

Browse files
authored
WIP local default (#1446)
* WIP local default * Fixes local deps tests * Adds `-g` to test * Fixes CI * Removes unused module * When there is no nimble file in the current directory, the default is global * Fixes CI
1 parent b890189 commit 66a8c01

File tree

9 files changed

+80
-69
lines changed

9 files changed

+80
-69
lines changed

src/nimblepkg/nimblesat.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ proc getPackageMinimalVersionsFromRepo*(repoDir: string, pkg: PkgTuple, version:
646646
continue
647647

648648
doCheckout(downloadMethod, tempDir, tag, options)
649-
let nimbleFile = findNimbleFile(tempDir, true, options)
649+
let nimbleFile = findNimbleFile(tempDir, true, options, warn = false)
650650
if options.satResult.pass in {satNimSelection, satFallbackToVmParser}:
651651
result.addUnique getPkgInfoFromDirWithDeclarativeParser(tempDir, options).getMinimalInfo(options)
652652
elif options.useDeclarativeParser:

src/nimblepkg/nimscriptexecutor.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import os, strutils, sets
55

6-
import packageparser, common, packageinfo, options, nimscriptwrapper, cli
6+
import packageparser, common, options, nimscriptwrapper, cli
77

88
proc execHook*(options: Options, hookAction: ActionType, before: bool): bool =
99
## Returns whether to continue.

src/nimblepkg/options.nim

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ Nimble Options:
252252
-y, --accept Accept all interactive prompts.
253253
-n, --reject Reject all interactive prompts.
254254
-l, --localdeps Run in project local dependency mode.
255+
-g, --global Run in global dependency mode.
255256
-p, --package For which package in the dependency tree the
256257
command should be executed. If not provided by
257258
default it applies to the current directory
@@ -440,11 +441,49 @@ proc setPackageCache(options: var Options, baseDir: string) =
440441
if options.verbosity >= LowPriority:
441442
display("Info:", "Package cache path " & options.pkgCachePath, priority = LowPriority)
442443

444+
proc isSubdirOf*(subdir, baseDir: string): bool =
445+
let
446+
normalizedSubdir = subdir.normalizedPath
447+
normalizedBaseDir = baseDir.normalizedPath & DirSep
448+
449+
when defined(windows):
450+
normalizedSubdir.toLower.startsWith(normalizedBaseDir.toLower)
451+
else:
452+
normalizedSubdir.startsWith(normalizedBaseDir)
453+
454+
proc findNimbleFile*(dir: string; error: bool, options: Options, warn = true): string =
455+
var hits = 0
456+
for kind, path in walkDir(dir):
457+
if kind in {pcFile, pcLinkToFile}:
458+
let ext = path.splitFile.ext
459+
case ext
460+
of ".babel", ".nimble":
461+
result = path
462+
inc hits
463+
else: discard
464+
if hits >= 2:
465+
raise nimbleError(
466+
"Only one .nimble file should be present in " & dir)
467+
elif hits == 0:
468+
if error:
469+
raise nimbleError(
470+
"Could not find a file with a .nimble extension inside the specified " &
471+
"directory: $1" % dir)
472+
else:
473+
if not dir.isSubdirOf(options.nimBinariesDir) and warn:
474+
displayWarning(&"No .nimble file found for {dir}")
475+
443476
proc setNimbleDir*(options: var Options) =
444477
var
445478
nimbleDir = options.config.nimbleDir
446479
propagate = false
447480

481+
#if there is no .nimble file in the current directory, we default to global deps mode
482+
let thereIsNimbleFile = findNimbleFile(getCurrentDir(), error = false, options, warn = false) != ""
483+
if not thereIsNimbleFile and options.action.typ != actionDevelop:
484+
options.localdeps = false
485+
486+
448487
if options.action.typ == actionDevelop:
449488
options.forceFullClone = true
450489

@@ -468,10 +507,7 @@ proc setNimbleDir*(options: var Options) =
468507
setPackageCache(options, nimbleDir)
469508
else:
470509
# ...followed by project local deps mode
471-
if dirExists(nimbledeps) or (options.localdeps and not options.developLocaldeps):
472-
if not options.showVersion:
473-
display("Warning:", "Using project local deps mode", Warning,
474-
priority = HighPriority)
510+
if dirExists(nimbledeps) or (options.localdeps and not options.developLocaldeps):
475511
nimbleDir = nimbledeps
476512
options.localdeps = true
477513
propagate = true
@@ -633,6 +669,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
633669
of "disablevalidation": result.disableValidation = true
634670
of "nim": result.nimBin = some makeNimBin(result, val)
635671
of "localdeps", "l": result.localdeps = true
672+
of "global", "g": result.localdeps = false
636673
of "nosslcheck": result.disableSslCertCheck = true
637674
of "nolockfile": result.disableLockFile = true
638675
of "tarballs", "t": result.enableTarballs = true
@@ -805,7 +842,8 @@ proc initOptions*(): Options =
805842
useSatSolver: true,
806843
useDeclarativeParser: false,
807844
legacy: false, #default to legacy code path for nimble < 1.0.0
808-
satResult: SatResult()
845+
satResult: SatResult(),
846+
localDeps: true
809847
)
810848

811849
proc handleUnknownFlags(options: var Options) =
@@ -964,16 +1002,6 @@ proc lockFile*(options: Options, dir: string): string =
9641002
proc lockFileExists*(options: Options, dir: string): bool =
9651003
return options.lockFile(dir).fileExists
9661004

967-
proc isSubdirOf*(subdir, baseDir: string): bool =
968-
let
969-
normalizedSubdir = subdir.normalizedPath
970-
normalizedBaseDir = baseDir.normalizedPath & DirSep
971-
972-
when defined(windows):
973-
normalizedSubdir.toLower.startsWith(normalizedBaseDir.toLower)
974-
else:
975-
normalizedSubdir.startsWith(normalizedBaseDir)
976-
9771005
proc isDevelopment*(pkg: PackageInfo, options: Options): bool =
9781006
### Returns true if the package is a development package.
9791007
### A development package is a root package that is not installed.

src/nimblepkg/packageinfo.nim

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -262,27 +262,6 @@ proc getPackageList*(options: Options): seq[Package] =
262262
result.add(pkg)
263263
namesAdded.incl(pkg.name)
264264

265-
proc findNimbleFile*(dir: string; error: bool, options: Options): string =
266-
var hits = 0
267-
for kind, path in walkDir(dir):
268-
if kind in {pcFile, pcLinkToFile}:
269-
let ext = path.splitFile.ext
270-
case ext
271-
of ".babel", ".nimble":
272-
result = path
273-
inc hits
274-
else: discard
275-
if hits >= 2:
276-
raise nimbleError(
277-
"Only one .nimble file should be present in " & dir)
278-
elif hits == 0:
279-
if error:
280-
raise nimbleError(
281-
"Could not find a file with a .nimble extension inside the specified " &
282-
"directory: $1" % dir)
283-
else:
284-
if not dir.isSubdirOf(options.nimBinariesDir):
285-
displayWarning(&"No .nimble file found for {dir}")
286265

287266
proc setNameVersionChecksum*(pkgInfo: var PackageInfo, pkgDir: string) =
288267
let (name, version, checksum) = getNameVersionChecksum(pkgDir)

tests/tdeclarativeparser.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ suite "Declarative parsing":
6868
options.useDeclarativeParser = true
6969
options.noColor = true
7070
options.verbosity = DebugPriority
71+
options.localDeps = false
7172
setSuppressMessages(false)
7273
removeDir(options.pkgCachePath)
7374
let pv = parseRequires("nimfp >= 0.3.4")
@@ -84,6 +85,7 @@ suite "Declarative parsing":
8485
@["0.3.4", "0.3.5", "0.3.6", "0.4.5", "0.4.4"].mapIt(newVersion(it))
8586
for version in availableVersions:
8687
check version in packageVersions.mapIt(it.version)
88+
8789
check fileExists(repoDir / TaggedVersionsFileName)
8890

8991
test "should be able to install a package using the declarative parser":

tests/testscommon.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ proc execNimble*(args: varargs[string]): ProcessOutput =
3434
quotedArgs.insert("--noColor")
3535
if not args.anyIt("--nimbleDir:" in it or "-l" == it or "--local" == it):
3636
quotedArgs.insert("--nimbleDir:" & installDir)
37+
if not args.anyIt("-l" == it or "--local" == it):
38+
quotedArgs.insert("--global") #default to global mode for tests
3739
quotedArgs.insert(nimblePath)
3840
quotedArgs = quotedArgs.map((x: string) => x.quoteShell)
3941

tests/tissues.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ suite "issues":
198198
const localNimbleDir = "./nimbleDir"
199199
cleanDir localNimbleDir
200200
let (_, exitCode) = execCmdEx(
201-
&"{nimblePath} -y --nimbleDir={localNimbleDir} install")
201+
&"{nimblePath} -y --nimbleDir={localNimbleDir} install -g")
202202
check exitCode == QuitSuccess
203203
let dummyPkgDir = getPackageDir(
204204
localNimbleDir / nimblePackagesDirName, "dummy-0.1.0")

tests/tlocaldeps.nim

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
{.used.}
55

6-
import unittest, os, osproc, strutils, strformat
6+
import unittest, os, osproc, strformat
77
import testscommon
88
from nimblepkg/common import cd
99
import nimblepkg/options
@@ -18,20 +18,17 @@ suite "project local deps mode":
1818
cd "localdeps":
1919
removeFile("localdeps")
2020
cleanDir("nimbledeps")
21-
createDir("nimbledeps")
22-
let (output, exitCode) = execCmdEx(nimblePath & " install -y")
21+
let (_, exitCode) = execCmdEx(nimblePath & " install -y")
2322
check exitCode == QuitSuccess
24-
check output.contains("project local deps mode")
25-
check output.contains("Succeeded")
23+
check dirExists("nimbledeps")
2624

2725
test "--localdeps flag":
2826
cd "localdeps":
2927
removeFile("localdeps")
3028
cleanDir("nimbledeps")
31-
let (output, exitCode) = execCmdEx(nimblePath & " install -y -l")
29+
let (_, exitCode) = execCmdEx(nimblePath & " install -y -l")
3230
check exitCode == QuitSuccess
33-
check output.contains("project local deps mode")
34-
check output.contains("Succeeded")
31+
check dirExists("nimbledeps")
3532

3633
test "localdeps develop":
3734
cleanDir("nimbledeps")

tests/tsat.nim

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ suite "SAT solver":
321321
"https://raw.githubusercontent.com/nim-lang/packages/master/packages.json",
322322
"https://nim-lang.org/nimble/packages.json"
323323
])
324+
options.localDeps = false
324325
let pv = parseRequires("nimfp >= 0.3.4")
325326
let downloadRes = pv.downloadPkgFromUrl(options)[0] #This is just to setup the test. We need a git dir to work on
326327
let repoDir = downloadRes.dir
@@ -337,6 +338,7 @@ suite "SAT solver":
337338
var options = initOptions()
338339
options.maxTaggedVersions = 0 #all
339340
options.nimBin = some options.makeNimBin("nim")
341+
options.localDeps = false
340342
options.config.packageLists["official"] = PackageList(name: "Official", urls: @[
341343
"https://raw.githubusercontent.com/nim-lang/packages/master/packages.json",
342344
"https://nim-lang.org/nimble/packages.json"
@@ -364,29 +366,30 @@ suite "SAT solver":
364366
check version in packageVersions.mapIt(it.version)
365367
check fileExists(repoDir / TaggedVersionsFileName)
366368

367-
test "should not use the global tagged cache when in local but a local one":
368-
var options = initOptions()
369-
options.localDeps = true
370-
options.maxTaggedVersions = 0 #all
371-
options.nimBin = some options.makeNimBin("nim")
372-
options.config.packageLists["official"] = PackageList(name: "Official", urls: @[
373-
"https://raw.githubusercontent.com/nim-lang/packages/master/packages.json",
374-
"https://nim-lang.org/nimble/packages.json"
375-
])
376-
options.setNimbleDir()
377-
echo "NIMBLE DIR", options.getNimbleDir()
378-
for dir in walkDir(".", true):
379-
if dir.kind == PathComponent.pcDir and dir.path.startsWith("githubcom_vegansknimfp"):
380-
echo "Removing dir", dir.path
381-
removeDir(dir.path)
382-
383-
let pvPrev = parseRequires("nimfp >= 0.3.4")
384-
let downloadResPrev = pvPrev.downloadPkgFromUrl(options)[0]
385-
let repoDirPrev = downloadResPrev.dir
386-
discard getPackageMinimalVersionsFromRepo(repoDirPrev, pvPrev, downloadResPrev.version, DownloadMethod.git, options)
387-
check not fileExists(repoDirPrev / TaggedVersionsFileName)
369+
#Desactivate tests as it goes against local deps mode by default. Need to be redone
370+
# test "should not use the global tagged cache when in local but a local one":
371+
# cd "localdeps":
372+
# var options = initOptions()
373+
# options.localDeps = true
374+
# options.maxTaggedVersions = 0 #all
375+
# options.nimBin = some options.makeNimBin("nim")
376+
# options.config.packageLists["official"] = PackageList(name: "Official", urls: @[
377+
# "https://raw.githubusercontent.com/nim-lang/packages/master/packages.json",
378+
# "https://nim-lang.org/nimble/packages.json"
379+
# ])
380+
# options.setNimbleDir()
381+
# for dir in walkDir(".", true):
382+
# if dir.kind == PathComponent.pcDir and dir.path.startsWith("githubcom_vegansknimfp"):
383+
# echo "Removing dir", dir.path
384+
# removeDir(dir.path)
385+
386+
# let pvPrev = parseRequires("nimfp >= 0.3.4")
387+
# let downloadResPrev = pvPrev.downloadPkgFromUrl(options)[0]
388+
# let repoDirPrev = downloadResPrev.dir
389+
# discard getPackageMinimalVersionsFromRepo(repoDirPrev, pvPrev, downloadResPrev.version, DownloadMethod.git, options)
390+
# check not fileExists(repoDirPrev / TaggedVersionsFileName)
388391

389-
check fileExists("nimbledeps" / "pkgcache" / "tagged" / "nimfp.json")
392+
# check fileExists("nimbledeps" / "pkgcache" / "tagged" / "nimfp.json")
390393

391394
test "if a dependency is unsatisfable, it should fallback to the previous version of the depency when available":
392395
let pkgVersionTable = {

0 commit comments

Comments
 (0)