Skip to content

Commit

Permalink
Add --offline flag
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberTailor committed Dec 12, 2021
1 parent 1339046 commit e45e3b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 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 @@ -303,7 +306,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 @@ -500,6 +503,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 @@ -540,14 +545,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 @@ -562,7 +567,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 @@ -582,9 +587,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 @@ -600,7 +607,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 @@ -1459,7 +1466,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 @@ -1500,7 +1507,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 @@ -1520,7 +1527,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 @@ -1557,6 +1564,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 @@ -1587,7 +1597,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 @@ -1670,6 +1680,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 @@ -194,6 +195,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 @@ -470,6 +472,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
2 changes: 1 addition & 1 deletion src/nimblepkg/packageinfo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ proc getNameAndVersion*(pkgInfo: PackageInfo): string =
&"{pkgInfo.basicInfo.name}@{pkgInfo.basicInfo.version}"

when isMainModule:
import unittest
import unittest

test "toValidPackageName":
check toValidPackageName("foo__bar") == "foo_bar"
Expand Down

0 comments on commit e45e3b8

Please sign in to comment.