Skip to content

Commit c44089b

Browse files
author
jan Anja
authored
Add --offline option (#967)
* Add --offline flag Closes #699 * Add --ofline test in tuninstall.nim * Add --offline test in tnimblerefresh.nim
1 parent b0d2311 commit c44089b

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

src/nimble.nim

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ proc refresh(options: Options) =
3131
## Downloads the package list from the specified URL.
3232
##
3333
## If the download is not successful, an exception is raised.
34+
if options.offline:
35+
raise nimbleError("Cannot refresh package list in offline mode.")
36+
3437
let parameter =
3538
if options.action.typ == actionRefresh:
3639
options.action.optionalURL
@@ -310,7 +313,7 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
310313
PackageDependenciesInfo =
311314
## Returns where package has been installed to, together with paths
312315
## to the packages this package depends on.
313-
##
316+
##
314317
## The return value of this function is used by
315318
## ``processFreeDependencies``
316319
## To gather a list of paths to pass to the Nim compiler.
@@ -507,6 +510,8 @@ proc raiseCannotCloneInExistingDirException(downloadDir: string) =
507510
proc downloadDependency(name: string, dep: LockFileDep, options: Options):
508511
DownloadInfo =
509512
## Downloads a dependency from the lock file.
513+
if options.offline:
514+
raise nimbleError("Cannot download in offline mode.")
510515
511516
if not options.developWithDependencies:
512517
let depDirName = getDependencyDir(name, dep, options)
@@ -547,14 +552,14 @@ proc downloadDependency(name: string, dep: LockFileDep, options: Options):
547552
result = DownloadInfo(
548553
name: name,
549554
dependency: dep,
550-
url: url,
555+
url: url,
551556
version: version,
552557
downloadDir: downloadDir,
553558
vcsRevision: vcsRevision)
554559
555560
proc installDependency(pkgInfo: PackageInfo, downloadInfo: DownloadInfo,
556561
options: Options): PackageInfo =
557-
## Installs an already downloaded dependency of the package `pkgInfo`.
562+
## Installs an already downloaded dependency of the package `pkgInfo`.
558563
let (_, newlyInstalledPkgInfo) = installFromDir(
559564
downloadInfo.downloadDir,
560565
downloadInfo.version,
@@ -569,7 +574,7 @@ proc installDependency(pkgInfo: PackageInfo, downloadInfo: DownloadInfo,
569574
for depDepName in downloadInfo.dependency.dependencies:
570575
let depDep = pkgInfo.lockedDeps[depDepName]
571576
let revDep = (name: depDepName, version: depDep.version,
572-
checksum: depDep.checksums.sha1)
577+
checksum: depDep.checksums.sha1)
573578
options.nimbleData.addRevDep(revDep, newlyInstalledPkgInfo)
574579
575580
return newlyInstalledPkgInfo
@@ -589,9 +594,11 @@ proc processLockedDependencies(pkgInfo: PackageInfo, options: Options):
589594
result.incl developModeDeps[name][]
590595
elif isInstalled(name, dep, options):
591596
result.incl getDependency(name, dep, options)
592-
else:
597+
elif not options.offline:
593598
let downloadResult = downloadDependency(name, dep, options)
594599
result.incl installDependency(pkgInfo, downloadResult, options)
600+
else:
601+
raise nimbleError("Unsatisfied dependency: " & pkgInfo.basicInfo.name)
595602
596603
proc getDownloadInfo*(pv: PkgTuple, options: Options,
597604
doPrompt: bool): (DownloadMethod, string,
@@ -607,7 +614,7 @@ proc getDownloadInfo*(pv: PkgTuple, options: Options,
607614
else:
608615
# If package is not found give the user a chance to refresh
609616
# package.json
610-
if doPrompt and
617+
if doPrompt and not options.offline and
611618
options.prompt(pv.name & " not found in any local packages.json, " &
612619
"check internet for updated packages?"):
613620
refresh(options)
@@ -1466,7 +1473,7 @@ proc validateDevModeDepsWorkingCopiesBeforeLock(
14661473
vekWorkingCopyNeedsLock,
14671474
vekWorkingCopyNeedsMerge,
14681475
}
1469-
1476+
14701477
# Remove not errors from the errors set.
14711478
for name, error in common.dup(errors):
14721479
if error.kind in notAnErrorSet:
@@ -1507,7 +1514,7 @@ proc mergeLockedDependencies*(pkgInfo: PackageInfo, newDeps: LockFileDeps,
15071514
proc displayLockOperationStart(dir: string): bool =
15081515
## Displays a proper log message for starting generating or updating the lock
15091516
## file of a package in directory `dir`.
1510-
1517+
15111518
var doesLockFileExist = dir.lockFileExists
15121519
let msg = if doesLockFileExist:
15131520
updatingTheLockFileMsg
@@ -1527,7 +1534,7 @@ proc displayLockOperationFinish(didLockFileExist: bool) =
15271534
displaySuccess(msg)
15281535

15291536
proc lock(options: Options) =
1530-
## Generates a lock file for the package in the current directory or updates
1537+
## Generates a lock file for the package in the current directory or updates
15311538
## it if it already exists.
15321539

15331540
let currentDir = getCurrentDir()
@@ -1564,6 +1571,9 @@ proc syncWorkingCopy(name: string, path: Path, dependentPkg: PackageInfo,
15641571
## with name `name` at path `path` with the revision from the lock file of
15651572
## `dependentPkg`.
15661573

1574+
if options.offline:
1575+
raise nimbleError("Cannot sync in offline mode.")
1576+
15671577
displayInfo(&"Syncing working copy of package \"{name}\" at \"{path}\"...")
15681578

15691579
let lockedDeps = dependentPkg.lockedDeps
@@ -1594,7 +1604,7 @@ proc syncWorkingCopy(name: string, path: Path, dependentPkg: PackageInfo,
15941604
path, vcsRevision, btRemoteTracking)
15951605
allBranches = localBranches + remoteTrackingBranches
15961606

1597-
var targetBranch =
1607+
var targetBranch =
15981608
if allBranches.len == 0:
15991609
# Te revision is not found on any branch.
16001610
""
@@ -1677,6 +1687,9 @@ proc sync(options: Options) =
16771687
if not pkgInfo.areLockedDepsLoaded:
16781688
raise nimbleError("Cannot execute `sync` when lock file is missing.")
16791689

1690+
if options.offline:
1691+
raise nimbleError("Cannot execute `sync` in offline mode.")
1692+
16801693
if not options.action.listOnly:
16811694
# On `sync` we also want to update Nimble cache with the dependencies'
16821695
# versions from the lock file.

src/nimblepkg/download.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ proc downloadPkg*(url: string, verRange: VersionRange,
451451
## If specified this parameter will cause specific VCS revision to be
452452
## checked out.
453453

454+
if options.offline:
455+
raise nimbleError("Cannot download in offline mode.")
456+
454457
let downloadDir =
455458
if downloadPath == "":
456459
(getNimbleTempDir() / getDownloadDirName(url, verRange, vcsRevision))

src/nimblepkg/options.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type
2727
pkgInfoCache*: TableRef[string, PackageInfo]
2828
showHelp*: bool
2929
showVersion*: bool
30+
offline*: bool
3031
noColor*: bool
3132
disableValidation*: bool
3233
continueTestsOnFailure*: bool
@@ -196,6 +197,7 @@ Nimble Options:
196197
--silent Hide all Nimble and Nim output
197198
--verbose Show all non-debug output.
198199
--debug Show all output including debug messages.
200+
--offline Don't use network.
199201
--noColor Don't colorise output.
200202
--noSSLCheck Don't check SSL certificates.
201203
@@ -472,6 +474,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
472474
of "silent": result.verbosity = SilentPriority
473475
of "verbose": result.verbosity = LowPriority
474476
of "debug": result.verbosity = DebugPriority
477+
of "offline": result.offline = true
475478
of "nocolor": result.noColor = true
476479
of "disablevalidation": result.disableValidation = true
477480
of "nim": result.nim = val

tests/tnimblerefresh.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import unittest, os, strutils
77
import testscommon
88

99
suite "nimble refresh":
10+
test "cannot refresh in --offline mode":
11+
let (output, exitCode) = execNimble(["--offline", "refresh"])
12+
check exitCode != QuitSuccess
13+
check output.contains("Cannot refresh package list in offline mode.")
14+
1015
test "can refresh with default urls":
1116
let (output, exitCode) = execNimble(["refresh"])
1217
checkpoint(output)

tests/tuninstall.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ from nimblepkg/common import cd
1111
from nimblepkg/version import newVersion
1212

1313
suite "uninstall":
14+
test "cannot install packagebin2 in --offline mode":
15+
cleanDir(installDir)
16+
let args = ["--offline", "install", pkgBin2Url]
17+
let (output, exitCode) = execNimbleYes(args)
18+
check exitCode != QuitSuccess
19+
check output.contains("Cannot download in offline mode.")
20+
1421
test "can install packagebin2":
1522
cleanDir(installDir)
1623
let args = ["install", pkgBin2Url]

0 commit comments

Comments
 (0)