Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sponsor command #1258

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/nimble.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.

import os, tables, strtabs, json, algorithm, sets, uri, sugar, sequtils, osproc,
import os, tables, strtabs, json, math, browsers, algorithm, sets, uri, sugar, sequtils, osproc,
strformat

import std/options as std_opt
Expand Down Expand Up @@ -2087,6 +2087,54 @@ proc sync(options: Options) =
if errors.len > 0:
raise validationErrors(errors)

proc sponsor(options: Options) =
## Allows the user to sponsor a package's developer, if they have the fields set-up in their package manifest.
## This is done in a case-sensitive manner and the search terminates upon a single match.
let pkgList = getPackageList(options)

if options.action.optionalPackage.len < 1:
var accepting: uint = 0

for pkg in pkgList:
if pkg.donations.len > 0:
inc(accepting)
displayInfo(pkg.name)

for donation in pkg.donations:
displayInfo($donation)

displayHint("To sponsor this library's developer, run `nimble sponsor " & pkg.name & '`')
echo('\n')

displayInfo(
"$1 $2 $3 accepting donations, amounting to $4% of all packages." % [
$accepting,
(if accepting == 1: "package" else: "packages"),
(if accepting == 1: "is" else: "are"),
$round(accepting.int / pkgList.len)
]
)
return

for pkg in pkgList:
if pkg.name == options.action.optionalPackage:
displayInfo("Found package: " & pkg.name)

if pkg.donations.len < 1:
displayError("This package's maintainer has not set up any donation links in the package manifest.")
displayError("You can contact them directly to sponsor them in some other way instead.")
return

for donationUrl in pkg.donations:
let url = $donationUrl

displayInfo(url)
openDefaultBrowser(url)

return

displayError("No such package by the name of $1 was found." % [options.action.optionalPackage])

proc append(existingContent: var string; newContent: string) =
## Appends `newContent` to the `existingContent` on a new line by inserting it
## if the new line doesn't already exist.
Expand Down Expand Up @@ -2335,6 +2383,8 @@ proc doAction(options: var Options) =
assert false
of actionAdd:
addPackages(options.action.packages, options)
of actionSponsor:
sponsor(options)
of actionCustom:
var optsCopy = options
optsCopy.task = options.action.command.normalize
Expand Down
8 changes: 7 additions & 1 deletion src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type
actionInstall, actionSearch, actionList, actionBuild, actionPath,
actionUninstall, actionCompile, actionDoc, actionCustom, actionTasks,
actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup,
actionClean, actionDeps, actionShellEnv, actionShell, actionAdd
actionClean, actionDeps, actionShellEnv, actionShell, actionAdd, actionSponsor

DevelopActionType* = enum
datAdd, datRemoveByPath, datRemoveByName, datInclude, datExclude
Expand Down Expand Up @@ -108,6 +108,8 @@ type
custRunFlags*: seq[string]
of actionDeps:
format*: string
of actionSponsor:
optionalPackage*: string
of actionShellEnv, actionShell:
discard

Expand Down Expand Up @@ -330,6 +332,8 @@ proc parseActionType*(action: string): ActionType =
result = actionShell
of "add":
result = actionAdd
of "sponsor":
result = actionSponsor
else:
result = actionCustom

Expand Down Expand Up @@ -510,6 +514,8 @@ proc parseArgument*(key: string, result: var Options) =
result.action.file = key
of actionRun:
result.setRunOptions(key, key, true)
of actionSponsor:
result.action.optionalPackage = key
of actionCustom:
result.action.arguments.add(key)
else:
Expand Down
14 changes: 12 additions & 2 deletions src/nimblepkg/packageinfo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Stdlib imports
import system except TResult
import hashes, json, strutils, os, sets, tables, times, httpclient, strformat
import hashes, json, strutils, os, sets, tables, uri, times, httpclient, strformat
from net import SslError

# Local imports
Expand Down Expand Up @@ -98,6 +98,13 @@ proc fromJson(obj: JSonNode): Package =
result.tags.add(t.str)
result.description = obj.optionalField("description")
result.web = obj.optionalField("web")

if "donations" in obj:
for d in obj.getOrDefault("donations"):
result.donations &= parseUri(d.getStr())
else:
result.donations = @[]

{.warning[ProveInit]: on.}

proc needsRefresh*(options: Options): bool =
Expand Down Expand Up @@ -367,7 +374,6 @@ proc findAllPkgs*(pkglist: seq[PackageInfo], dep: PkgTuple): seq[PackageInfo] =
if withinRange(pkg, dep.ver):
result.add pkg


proc getRealDir*(pkgInfo: PackageInfo): string =
## Returns the directory containing the package source files.
if pkgInfo.srcDir != "" and (not pkgInfo.isInstalled or pkgInfo.isLink):
Expand Down Expand Up @@ -395,6 +401,10 @@ proc echoPackage*(pkg: Package) =
echo(" license: " & pkg.license)
if pkg.web.len > 0:
echo(" website: " & pkg.web)
if pkg.donations.len > 0:
echo(" donations:")
for i, link in pkg.donations:
echo " " & $(i + 1) & ": " & $link

proc getDownloadDirName*(pkg: Package, verRange: VersionRange): string =
result = pkg.name
Expand Down
7 changes: 5 additions & 2 deletions src/nimblepkg/packageinfotypes.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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
Expand Down Expand Up @@ -71,7 +71,9 @@ type
isLink*: bool
paths*: seq[string]
entryPoints*: seq[string] #useful for tools like the lsp.


DonationLink* = URI

Package* = object ## Definition of package from packages.json.
# Required fields in a package.
name*: string
Expand All @@ -84,6 +86,7 @@ type
version*: Version
dvcsTag*: string
web*: string # Info url for humans.
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