Skip to content

Commit

Permalink
(fix) donations: remove hardcoded services in favor of URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
xTrayambak committed Aug 14, 2024
1 parent e780ff0 commit 846fab9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 65 deletions.
17 changes: 4 additions & 13 deletions src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down
44 changes: 2 additions & 42 deletions src/nimblepkg/packageinfo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = @[]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions src/nimblepkg/packageinfotypes.nim
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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]
Expand Down

0 comments on commit 846fab9

Please sign in to comment.