Merge pull request #371 from subsetpark/local-json
Allow specifying local json package list files
This commit is contained in:
commit
10a38a3c90
6 changed files with 152 additions and 62 deletions
|
|
@ -48,17 +48,17 @@ proc refresh(options: Options) =
|
|||
if parameter.len > 0:
|
||||
if parameter.isUrl:
|
||||
let cmdLine = PackageList(name: "commandline", urls: @[parameter])
|
||||
downloadList(cmdLine, options)
|
||||
fetchList(cmdLine, options)
|
||||
else:
|
||||
if parameter notin options.config.packageLists:
|
||||
let msg = "Package list with the specified name not found."
|
||||
raise newException(NimbleError, msg)
|
||||
|
||||
downloadList(options.config.packageLists[parameter], options)
|
||||
fetchList(options.config.packageLists[parameter], options)
|
||||
else:
|
||||
# Try each package list in config
|
||||
for name, list in options.config.packageLists:
|
||||
downloadList(list, options)
|
||||
fetchList(list, options)
|
||||
|
||||
proc checkInstallFile(pkgInfo: PackageInfo,
|
||||
origDir, file: string): bool =
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ type
|
|||
PackageList* = object
|
||||
name*: string
|
||||
urls*: seq[string]
|
||||
path*: string
|
||||
|
||||
proc initConfig(): Config =
|
||||
result.nimbleDir = getHomeDir() / ".nimble"
|
||||
|
|
@ -35,6 +36,7 @@ proc initConfig(): Config =
|
|||
proc initPackageList(): PackageList =
|
||||
result.name = ""
|
||||
result.urls = @[]
|
||||
result.path = ""
|
||||
|
||||
proc addCurrentPkgList(config: var Config, currentPackageList: PackageList) =
|
||||
if currentPackageList.name.len > 0:
|
||||
|
|
@ -53,7 +55,6 @@ proc parseConfig*(): Config =
|
|||
if f != nil:
|
||||
display("Warning", "Using deprecated config file at " & confFile,
|
||||
Warning, HighPriority)
|
||||
|
||||
if f != nil:
|
||||
display("Reading", "config file at " & confFile, priority = LowPriority)
|
||||
var p: CfgParser
|
||||
|
|
@ -64,6 +65,10 @@ proc parseConfig*(): Config =
|
|||
var e = next(p)
|
||||
case e.kind
|
||||
of cfgEof:
|
||||
if currentPackageList.urls.len == 0 and currentPackageList.path == "":
|
||||
raise newException(NimbleError, "Package list '$1' requires either url or path" % currentPackageList.name)
|
||||
if currentPackageList.urls.len > 0 and currentPackageList.path != "":
|
||||
raise newException(NimbleError, "Attempted to specify `url` and `path` for the same package list '$1'" % currentPackageList.name)
|
||||
addCurrentPkgList(result, currentPackageList)
|
||||
break
|
||||
of cfgSectionStart:
|
||||
|
|
@ -97,6 +102,14 @@ proc parseConfig*(): Config =
|
|||
of "packagelist":
|
||||
currentPackageList.urls.add(e.value)
|
||||
else: assert false
|
||||
of "path":
|
||||
case currentSection.normalize
|
||||
of "packagelist":
|
||||
if currentPackageList.path != "":
|
||||
raise newException(NimbleError, "Attempted to specify more than one `path` for the same package list.")
|
||||
else:
|
||||
currentPackageList.path = e.value.normalize
|
||||
else: assert false
|
||||
else:
|
||||
raise newException(NimbleError, "Unable to parse config file:" &
|
||||
" Unknown key: " & e.key)
|
||||
|
|
|
|||
|
|
@ -160,53 +160,68 @@ proc validatePackagesList(path: string): bool =
|
|||
except ValueError, JsonParsingError:
|
||||
return false
|
||||
|
||||
proc downloadList*(list: PackageList, options: Options) =
|
||||
## Downloads the specified package list and saves it in $nimbleDir.
|
||||
display("Downloading", list.name & " package list", priority = HighPriority)
|
||||
proc fetchList*(list: PackageList, options: Options) =
|
||||
## Downloads or copies the specified package list and saves it in $nimbleDir.
|
||||
let verb = if list.urls.len > 0: "Downloading" else: "Copying"
|
||||
display(verb, list.name & " package list", priority = HighPriority)
|
||||
|
||||
var lastError = ""
|
||||
for i in 0 .. <list.urls.len:
|
||||
let url = list.urls[i]
|
||||
display("Trying", url)
|
||||
let tempPath = options.getNimbleDir() / "packages_temp.json"
|
||||
|
||||
# Grab the proxy
|
||||
let proxy = getProxy(options)
|
||||
if not proxy.isNil:
|
||||
var maskedUrl = proxy.url
|
||||
if maskedUrl.password.len > 0: maskedUrl.password = "***"
|
||||
display("Connecting", "to proxy at " & $maskedUrl,
|
||||
priority = LowPriority)
|
||||
|
||||
try:
|
||||
downloadFile(url, tempPath, proxy = getProxy(options))
|
||||
except:
|
||||
let message = "Could not download: " & getCurrentExceptionMsg()
|
||||
display("Warning:", message, Warning)
|
||||
lastError = message
|
||||
continue
|
||||
|
||||
if not validatePackagesList(tempPath):
|
||||
lastError = "Downloaded packages.json file is invalid"
|
||||
display("Warning:", lastError & ", discarding.", Warning)
|
||||
continue
|
||||
|
||||
copyFile(tempPath,
|
||||
options.getNimbleDir() / "packages_$1.json" % list.name.toLowerAscii())
|
||||
display("Success", "Package list downloaded.", Success, HighPriority)
|
||||
var
|
||||
lastError = ""
|
||||
break
|
||||
copyFromPath = ""
|
||||
if list.urls.len > 0:
|
||||
for i in 0 .. <list.urls.len:
|
||||
let url = list.urls[i]
|
||||
display("Trying", url)
|
||||
let tempPath = options.getNimbleDir() / "packages_temp.json"
|
||||
|
||||
# Grab the proxy
|
||||
let proxy = getProxy(options)
|
||||
if not proxy.isNil:
|
||||
var maskedUrl = proxy.url
|
||||
if maskedUrl.password.len > 0: maskedUrl.password = "***"
|
||||
display("Connecting", "to proxy at " & $maskedUrl,
|
||||
priority = LowPriority)
|
||||
|
||||
try:
|
||||
downloadFile(url, tempPath, proxy = proxy)
|
||||
except:
|
||||
let message = "Could not download: " & getCurrentExceptionMsg()
|
||||
display("Warning:", message, Warning)
|
||||
lastError = message
|
||||
continue
|
||||
|
||||
if not validatePackagesList(tempPath):
|
||||
lastError = "Downloaded packages.json file is invalid"
|
||||
display("Warning:", lastError & ", discarding.", Warning)
|
||||
continue
|
||||
|
||||
copyFromPath = tempPath
|
||||
display("Success", "Package list downloaded.", Success, HighPriority)
|
||||
lastError = ""
|
||||
break
|
||||
|
||||
elif list.path != "":
|
||||
if not validatePackagesList(list.path):
|
||||
lastError = "Copied packages.json file is invalid"
|
||||
display("Warning:", lastError & ", discarding.", Warning)
|
||||
else:
|
||||
copyFromPath = list.path
|
||||
display("Success", "Package list copied.", Success, HighPriority)
|
||||
|
||||
if lastError.len != 0:
|
||||
raise newException(NimbleError, "Refresh failed\n" & lastError)
|
||||
|
||||
if copyFromPath.len > 0:
|
||||
copyFile(copyFromPath,
|
||||
options.getNimbleDir() / "packages_$1.json" % list.name.toLowerAscii())
|
||||
|
||||
proc readPackageList(name: string, options: Options): JsonNode =
|
||||
# If packages.json is not present ask the user if they want to download it.
|
||||
if needsRefresh(options):
|
||||
if options.prompt("No local packages.json found, download it from " &
|
||||
"internet?"):
|
||||
for name, list in options.config.packageLists:
|
||||
downloadList(list, options)
|
||||
fetchList(list, options)
|
||||
else:
|
||||
raise newException(NimbleError, "Please run nimble refresh.")
|
||||
return parseFile(options.getNimbleDir() / "packages_" &
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue