From 846fab99406b604d18d0b8cb6dfa3fbdd67c6b6f Mon Sep 17 00:00:00 2001 From: xTrayambak Date: Wed, 14 Aug 2024 20:31:32 +0530 Subject: [PATCH] (fix) donations: remove hardcoded services in favor of URLs --- src/nimble.nim | 17 +++--------- src/nimblepkg/packageinfo.nim | 44 ++---------------------------- src/nimblepkg/packageinfotypes.nim | 13 ++------- 3 files changed, 9 insertions(+), 65 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 7da2f33c..516cc40c 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -2101,12 +2101,7 @@ proc sponsor(options: Options) = displayInfo(pkg.name) for donation in pkg.donations: - let url = donation.constructDonationURL() - displayInfo( - "$1: $2 ($3)" % [ - $donation.meth, donation.username, url - ] - ) + displayInfo($donation) displayHint("To sponsor this library's developer, run `nimble sponsor " & pkg.name & '`') echo('\n') @@ -2130,14 +2125,10 @@ proc sponsor(options: Options) = displayError("You can contact them directly to sponsor them in some other way instead.") return - for donation in pkg.donations: - let url = donation.constructDonationURL() - displayInfo( - "$1: $2 ($3)" % [ - $donation.meth, donation.username, url - ] - ) + for donationUrl in pkg.donations: + let url = $donationUrl + displayInfo(url) openDefaultBrowser(url) return diff --git a/src/nimblepkg/packageinfo.nim b/src/nimblepkg/packageinfo.nim index cc159d78..777987dc 100644 --- a/src/nimblepkg/packageinfo.nim +++ b/src/nimblepkg/packageinfo.nim @@ -18,21 +18,6 @@ proc initPackageInfo*(): PackageInfo = proc initPackage*(): Package = result = Package(version: notSetVersion) -proc `$`*(meth: DonationMethod): string {.inline.} = - ## Turns a `DonationMethod` into a pretty string. - case meth - of DonationMethod.GitHub: "GitHub" - of DonationMethod.OpenCollective: "OpenCollective" - of DonationMethod.Patreon: "Patreon" - -proc parseDonationMethod*(meth: string): DonationMethod {.inline.} = - case meth - of "github", "gh": return DonationMethod.GitHub - of "opencollective": return DonationMethod.OpenCollective - of "patreon": return DonationMethod.Patreon - else: - raise nimbleError("Invalid donation method: " & meth) - proc isLoaded*(pkgInfo: PackageInfo): bool = return pkgInfo.myPath.len > 0 @@ -116,11 +101,7 @@ proc fromJson(obj: JSonNode): Package = if "donations" in obj: for d in obj.getOrDefault("donations"): - result.donations &= - Donation( - meth: parseDonationMethod(d["meth"].getStr()), - username: d["username"].getStr() - ) + result.donations &= parseUri(d.getStr()) else: result.donations = @[] @@ -382,26 +363,6 @@ proc findPkg*(pkglist: seq[PackageInfo], dep: PkgTuple, r = pkg result = true -proc constructDonationURL*(donation: Donation): string = - ## Constructs a donation URL from a `Donation` object that is made via fields in a packages.json file. - ## It also performs validation to ensure that an invalid URL cannot be accidentally passed on if data in the packages.json file is malformed. - var url = "https://" - - case donation.meth - of DonationMethod.GitHub: - url &= "github.com/sponsors/" & donation.username - of DonationMethod.OpenCollective: - url &= "opencollective.com/" & donation.username - of DonationMethod.Patreon: - url &= "patreon.com/" & donation.username - - try: - let parsed = parseURI(url) - except UriParseError as exc: - raise nimbleError("Failed to parse URL properly whilst constructing donation URL - please report this to the Nimble developers! (" & exc.msg & ')') - - url - proc findAllPkgs*(pkglist: seq[PackageInfo], dep: PkgTuple): seq[PackageInfo] = ## Searches ``pkglist`` for packages of which version is within the range ## of ``dep.ver``. This is similar to ``findPkg`` but returns multiple @@ -443,8 +404,7 @@ proc echoPackage*(pkg: Package) = if pkg.donations.len > 0: echo(" donations:") for i, link in pkg.donations: - let url = constructDonationURL(link) - echo(" " & $link.meth & ": " & link.username & " (" & url & ')') + echo " " & $(i + 1) & ": " & $link proc getDownloadDirName*(pkg: Package, verRange: VersionRange): string = result = pkg.name diff --git a/src/nimblepkg/packageinfotypes.nim b/src/nimblepkg/packageinfotypes.nim index 26be655a..845954f4 100644 --- a/src/nimblepkg/packageinfotypes.nim +++ b/src/nimblepkg/packageinfotypes.nim @@ -1,18 +1,13 @@ # Copyright (C) Dominik Picheta. All rights reserved. # BSD License. Look at license.txt for more info. -import sets, tables +import sets, tables, uri import version, sha1hashes type DownloadMethod* {.pure.} = enum git = "git", hg = "hg" - DonationMethod* {.pure.} = enum - GitHub = "github" - OpenCollective = "opencollective" - Patreon = "patreon" - Checksums* = object sha1*: Sha1Hash @@ -77,9 +72,7 @@ type paths*: seq[string] entryPoints*: seq[string] #useful for tools like the lsp. - Donation* = object - meth*: DonationMethod - username*: string + DonationLink* = URI Package* = object ## Definition of package from packages.json. # Required fields in a package. @@ -93,7 +86,7 @@ type version*: Version dvcsTag*: string web*: string # Info url for humans. - donations*: seq[Donation] ## A list of donation methods that can be used to support its developer. + donations*: seq[DonationLink] ## A list of donation website URIs that can be used to support its developer. alias*: string ## A name of another package, that this package aliases. PackageDependenciesInfo* = tuple[deps: HashSet[PackageInfo], pkg: PackageInfo]