Skip to content

Commit

Permalink
Add --offline option (#967)
Browse files Browse the repository at this point in the history
* Add --offline flag

Closes #699

* Add --ofline test in tuninstall.nim

* Add --offline test in tnimblerefresh.nim
  • Loading branch information
jan Anja authored Dec 14, 2021
1 parent b0d2311 commit c44089b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ proc refresh(options: Options) =
## Downloads the package list from the specified URL.
##
## If the download is not successful, an exception is raised.
if options.offline:
raise nimbleError("Cannot refresh package list in offline mode.")

let parameter =
if options.action.typ == actionRefresh:
options.action.optionalURL
Expand Down Expand Up @@ -310,7 +313,7 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
PackageDependenciesInfo =
## Returns where package has been installed to, together with paths
## to the packages this package depends on.
##
##
## The return value of this function is used by
## ``processFreeDependencies``
## To gather a list of paths to pass to the Nim compiler.
Expand Down Expand Up @@ -507,6 +510,8 @@ proc raiseCannotCloneInExistingDirException(downloadDir: string) =
proc downloadDependency(name: string, dep: LockFileDep, options: Options):
DownloadInfo =
## Downloads a dependency from the lock file.
if options.offline:
raise nimbleError("Cannot download in offline mode.")

if not options.developWithDependencies:
let depDirName = getDependencyDir(name, dep, options)
Expand Down Expand Up @@ -547,14 +552,14 @@ proc downloadDependency(name: string, dep: LockFileDep, options: Options):
result = DownloadInfo(
name: name,
dependency: dep,
url: url,
url: url,
version: version,
downloadDir: downloadDir,
vcsRevision: vcsRevision)

proc installDependency(pkgInfo: PackageInfo, downloadInfo: DownloadInfo,
options: Options): PackageInfo =
## Installs an already downloaded dependency of the package `pkgInfo`.
## Installs an already downloaded dependency of the package `pkgInfo`.
let (_, newlyInstalledPkgInfo) = installFromDir(
downloadInfo.downloadDir,
downloadInfo.version,
Expand All @@ -569,7 +574,7 @@ proc installDependency(pkgInfo: PackageInfo, downloadInfo: DownloadInfo,
for depDepName in downloadInfo.dependency.dependencies:
let depDep = pkgInfo.lockedDeps[depDepName]
let revDep = (name: depDepName, version: depDep.version,
checksum: depDep.checksums.sha1)
checksum: depDep.checksums.sha1)
options.nimbleData.addRevDep(revDep, newlyInstalledPkgInfo)

return newlyInstalledPkgInfo
Expand All @@ -589,9 +594,11 @@ proc processLockedDependencies(pkgInfo: PackageInfo, options: Options):
result.incl developModeDeps[name][]
elif isInstalled(name, dep, options):
result.incl getDependency(name, dep, options)
else:
elif not options.offline:
let downloadResult = downloadDependency(name, dep, options)
result.incl installDependency(pkgInfo, downloadResult, options)
else:
raise nimbleError("Unsatisfied dependency: " & pkgInfo.basicInfo.name)

proc getDownloadInfo*(pv: PkgTuple, options: Options,
doPrompt: bool): (DownloadMethod, string,
Expand All @@ -607,7 +614,7 @@ proc getDownloadInfo*(pv: PkgTuple, options: Options,
else:
# If package is not found give the user a chance to refresh
# package.json
if doPrompt and
if doPrompt and not options.offline and
options.prompt(pv.name & " not found in any local packages.json, " &
"check internet for updated packages?"):
refresh(options)
Expand Down Expand Up @@ -1466,7 +1473,7 @@ proc validateDevModeDepsWorkingCopiesBeforeLock(
vekWorkingCopyNeedsLock,
vekWorkingCopyNeedsMerge,
}

# Remove not errors from the errors set.
for name, error in common.dup(errors):
if error.kind in notAnErrorSet:
Expand Down Expand Up @@ -1507,7 +1514,7 @@ proc mergeLockedDependencies*(pkgInfo: PackageInfo, newDeps: LockFileDeps,
proc displayLockOperationStart(dir: string): bool =
## Displays a proper log message for starting generating or updating the lock
## file of a package in directory `dir`.

var doesLockFileExist = dir.lockFileExists
let msg = if doesLockFileExist:
updatingTheLockFileMsg
Expand All @@ -1527,7 +1534,7 @@ proc displayLockOperationFinish(didLockFileExist: bool) =
displaySuccess(msg)

proc lock(options: Options) =
## Generates a lock file for the package in the current directory or updates
## Generates a lock file for the package in the current directory or updates
## it if it already exists.

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

if options.offline:
raise nimbleError("Cannot sync in offline mode.")

displayInfo(&"Syncing working copy of package \"{name}\" at \"{path}\"...")

let lockedDeps = dependentPkg.lockedDeps
Expand Down Expand Up @@ -1594,7 +1604,7 @@ proc syncWorkingCopy(name: string, path: Path, dependentPkg: PackageInfo,
path, vcsRevision, btRemoteTracking)
allBranches = localBranches + remoteTrackingBranches

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

if options.offline:
raise nimbleError("Cannot execute `sync` in offline mode.")

if not options.action.listOnly:
# On `sync` we also want to update Nimble cache with the dependencies'
# versions from the lock file.
Expand Down
3 changes: 3 additions & 0 deletions src/nimblepkg/download.nim
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ proc downloadPkg*(url: string, verRange: VersionRange,
## If specified this parameter will cause specific VCS revision to be
## checked out.

if options.offline:
raise nimbleError("Cannot download in offline mode.")

let downloadDir =
if downloadPath == "":
(getNimbleTempDir() / getDownloadDirName(url, verRange, vcsRevision))
Expand Down
3 changes: 3 additions & 0 deletions src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type
pkgInfoCache*: TableRef[string, PackageInfo]
showHelp*: bool
showVersion*: bool
offline*: bool
noColor*: bool
disableValidation*: bool
continueTestsOnFailure*: bool
Expand Down Expand Up @@ -196,6 +197,7 @@ Nimble Options:
--silent Hide all Nimble and Nim output
--verbose Show all non-debug output.
--debug Show all output including debug messages.
--offline Don't use network.
--noColor Don't colorise output.
--noSSLCheck Don't check SSL certificates.
Expand Down Expand Up @@ -472,6 +474,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
of "silent": result.verbosity = SilentPriority
of "verbose": result.verbosity = LowPriority
of "debug": result.verbosity = DebugPriority
of "offline": result.offline = true
of "nocolor": result.noColor = true
of "disablevalidation": result.disableValidation = true
of "nim": result.nim = val
Expand Down
5 changes: 5 additions & 0 deletions tests/tnimblerefresh.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import unittest, os, strutils
import testscommon

suite "nimble refresh":
test "cannot refresh in --offline mode":
let (output, exitCode) = execNimble(["--offline", "refresh"])
check exitCode != QuitSuccess
check output.contains("Cannot refresh package list in offline mode.")

test "can refresh with default urls":
let (output, exitCode) = execNimble(["refresh"])
checkpoint(output)
Expand Down
7 changes: 7 additions & 0 deletions tests/tuninstall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ from nimblepkg/common import cd
from nimblepkg/version import newVersion

suite "uninstall":
test "cannot install packagebin2 in --offline mode":
cleanDir(installDir)
let args = ["--offline", "install", pkgBin2Url]
let (output, exitCode) = execNimbleYes(args)
check exitCode != QuitSuccess
check output.contains("Cannot download in offline mode.")

test "can install packagebin2":
cleanDir(installDir)
let args = ["install", pkgBin2Url]
Expand Down

0 comments on commit c44089b

Please sign in to comment.