From d696ef5ad7cefdde044bce0277fac172de0f56ea Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Mon, 22 May 2023 20:35:27 +0200 Subject: [PATCH] =?UTF-8?q?Atlas=20tool:=20search=20github=20too,=20no=20n?= =?UTF-8?q?eed=20to=20register=20your=20project=20at=20pa=E2=80=A6=20(#218?= =?UTF-8?q?84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Atlas tool: search github too, no need to register your project at packages.json * added missing file --- tools/atlas/atlas.nim | 4 ++- tools/atlas/atlas.nim.cfg | 1 + tools/atlas/packagesjson.nim | 56 ++++++++++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 tools/atlas/atlas.nim.cfg diff --git a/tools/atlas/atlas.nim b/tools/atlas/atlas.nim index dfa60856ef..fe070c9691 100644 --- a/tools/atlas/atlas.nim +++ b/tools/atlas/atlas.nim @@ -248,7 +248,9 @@ proc toUrl(c: var AtlasContext; p: string): string = fillPackageLookupTable(c) result = c.p.getOrDefault(unicode.toLower p) if result.len == 0: - inc c.errors + result = getUrlFromGithub(p) + if result.len == 0: + inc c.errors proc toName(p: string): PackageName = if p.isUrl: diff --git a/tools/atlas/atlas.nim.cfg b/tools/atlas/atlas.nim.cfg new file mode 100644 index 0000000000..fcace05799 --- /dev/null +++ b/tools/atlas/atlas.nim.cfg @@ -0,0 +1 @@ +--define:ssl diff --git a/tools/atlas/packagesjson.nim b/tools/atlas/packagesjson.nim index 0b85997699..5ceef706f2 100644 --- a/tools/atlas/packagesjson.nim +++ b/tools/atlas/packagesjson.nim @@ -1,5 +1,5 @@ -import std / [json, os, sets, strutils] +import std / [json, os, sets, strutils, httpclient, uri] type Package* = ref object @@ -65,11 +65,57 @@ proc `$`*(pkg: Package): string = if pkg.web.len > 0: result &= " website: " & pkg.web & "\n" +proc toTags(j: JsonNode): seq[string] = + result = @[] + if j.kind == JArray: + for elem in items j: + result.add elem.getStr("") + +proc singleGithubSearch(term: string): JsonNode = + # For example: + # https://api.github.com/search/repositories?q=weave+language:nim + var client = newHttpClient() + try: + let x = client.getContent("https://api.github.com/search/repositories?q=" & encodeUrl(term) & "+language:nim") + result = parseJson(x) + except: + discard "it's a failed search, ignore" + finally: + client.close() + +proc githubSearch(seen: var HashSet[string]; terms: seq[string]) = + for term in terms: + let results = singleGithubSearch(term) + for j in items(results.getOrDefault("items")): + let p = Package( + name: j.getOrDefault("name").getStr, + url: j.getOrDefault("html_url").getStr, + downloadMethod: "git", + tags: toTags(j.getOrDefault("topics")), + description: ", not listed in packages.json", + web: j.getOrDefault("html_url").getStr + ) + if not seen.containsOrIncl(p.url): + echo p + +proc getUrlFromGithub*(term: string): string = + let results = singleGithubSearch(term) + var matches = 0 + result = "" + for j in items(results.getOrDefault("items")): + if cmpIgnoreCase(j.getOrDefault("name").getStr, term) == 0: + if matches == 0: + result = j.getOrDefault("html_url").getStr + inc matches + if matches != 1: + # ambiguous, not ok! + result = "" + proc search*(pkgList: seq[Package]; terms: seq[string]) = - var found = false + var seen = initHashSet[string]() template onFound = echo pkg - found = true + seen.incl pkg.url break forPackage for pkg in pkgList: @@ -86,8 +132,8 @@ proc search*(pkgList: seq[Package]; terms: seq[string]) = onFound() else: echo(pkg) - - if not found and terms.len > 0: + githubSearch seen, terms + if seen.len == 0 and terms.len > 0: echo("No package found.") type PkgCandidates* = array[3, seq[Package]]