nim-libp2p-experimental/tools/pinner.nim

46 lines
1.3 KiB
Nim

import os
import strscans
import algorithm
import tables
import sequtils
import strutils
import json
import osproc
const PinFile = ".pinned"
removeDir("nimbledeps")
createDir("nimbledeps")
discard execCmd("nimble install -dy")
var allDeps: Table[string, string]
let nimblePkgs =
if dirExists("nimbledeps/pkgs"): "nimbledeps/pkgs"
else: "nimbledeps/pkgs2"
for (_, dependency) in walkDir(nimblePkgs):
let
jsonContent = parseJson(readFile(dependency & "/nimblemeta.json"))
fileContent =
if "metaData" in jsonContent: jsonContent["metaData"]
else: jsonContent
let url = fileContent.getOrDefault("url").getStr("")
var version = fileContent.getOrDefault("vcsRevision").getStr("")
var packageName = dependency.split('/')[^1].split('-')[0]
if version.len == 0:
version = execCmdEx("git ls-remote " & url).output.split()[0]
if version.len > 0 and url.len > 0:
let fullPackage = url & "@#" & version
if packageName in allDeps and allDeps[packageName] != fullPackage:
echo "Warning: duplicate package " & packageName & ":"
echo allDeps[packageName]
echo fullPackage
echo ""
allDeps[packageName] = fullPackage
else:
echo "Failed to get url & version for ", dependency
let asString = toSeq(allDeps.pairs).mapIt(it[0] & ";" & it[1]).sorted().join("\n")
writeFile(PinFile, asString)
echo asString