Skip to content

Commit

Permalink
feature #73 Add "composer fund" command (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------

Add "composer fund" command

This PR is related to composer/packagist#1050

We discussed with @naderman about the topic: new pages are coming to packagist with similar call-to-actions. Composer will soon also allow defining funding links in our `composer.json` files. A native `composer fund` command should be able to display the funding links too.

Meanwhile, this command in `symfony/thanks` provides a `composer fund` command that relies on [GitHub's funding files](https://help.github.com/en/github/building-a-strong-community/displaying-a-sponsor-button-in-your-repository).

It is designed to give the idea a try in the community while the more generic solutions are being baked.

First, run `composer global require symfony/thanks ^1.2` to install or update this command on your computer.

Then run `composer fund` and see this:

![image](https://user-images.githubusercontent.com/243674/69824452-146fcc00-120c-11ea-8f40-7a48144868ea.png)

And run `composer thanks` and see this:
![image](https://user-images.githubusercontent.com/243674/69824471-26516f00-120c-11ea-86f7-eeb670c695a0.png)

Enjoy, maintainers will thank you!

Commits
-------

918cb6f Add "composer fund" command
  • Loading branch information
nicolas-grekas committed Nov 28, 2019
2 parents 4d97ae7 + 918cb6f commit 36ace22
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
92 changes: 92 additions & 0 deletions src/Command/FundCommand.php
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Thanks\Command;

use Composer\Command\BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Thanks\GitHubClient;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class FundCommand extends BaseCommand
{
private $star = '★ ';
private $love = '💖 ';
private $cash = '💵 ';

protected function configure()
{
if ('Hyper' === getenv('TERM_PROGRAM')) {
$this->star = '⭐ ';
} elseif ('\\' === \DIRECTORY_SEPARATOR) {
$this->star = '*';
$this->love = '<3';
$this->cash = '$$$';
}

$this->setName('fund')
->setDescription(sprintf('Discover the funding links that fellow PHP package maintainers publish %s.', $this->cash))
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$composer = $this->getComposer();
$gitHub = new GitHubClient($composer, $this->getIO());

$repos = $gitHub->getRepositories($failures);
$fundings = [];
$notStarred = 0;

foreach ($repos as $alias => $repo) {
$notStarred += (int) !$repo['viewerHasStarred'];

foreach ($repo['fundingLinks'] as $link) {
[$owner, $package] = explode('/', $repo['package'], 2);
$fundings[$owner][$link['url']][] = $package;
}
}

if ($fundings) {
$prev = null;

$output->writeln('The following packages were found in your dependencies and publish sponsoring links on their GitHub page:');

foreach ($fundings as $owner => $links) {
$output->writeln(sprintf("\n<comment>%s</comment>", $owner));
foreach ($links as $url => $packages) {
$line = sprintf(" <info>%s/%s</>", $owner, implode(', ', $packages));

if ($prev !== $line) {
$output->writeln($line);
$prev = $line;
}
$output->writeln(sprintf(" %s %s", $this->cash, $url));
}
}

$output->writeln("\nPlease consider following these links and sponsoring the work of package authors!");
$output->writeln(sprintf("\nThanks you! %s", $this->love));
} else {
$output->writeln("No funding links were found in your package dependencies. That doesn't mean they don't need your support!");
}

if ($notStarred) {
$output->writeln(sprintf("\nRun <comment>composer thanks</> to send a %s to <comment>%d</comment> GitHub repositor%s of your fellow package maintainers.", $this->star, $notStarred, 1 < $notStarred ? 'ies' : 'y'));
}

return 0;
}
}
8 changes: 5 additions & 3 deletions src/Command/ThanksCommand.php
Expand Up @@ -24,15 +24,16 @@ class ThanksCommand extends BaseCommand
{
private $star = '★ ';
private $love = '💖 ';
private $cash = '💵 ';

protected function configure()
{
if ('Hyper' === getenv('TERM_PROGRAM')) {
$this->star = '⭐ ';
$this->love = '💖 ';
} elseif ('\\' === \DIRECTORY_SEPARATOR) {
$this->star = '*';
$this->love = '<3';
$this->cash = '$$$';
}

$this->setName('thanks')
Expand Down Expand Up @@ -85,8 +86,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$output->writeln(sprintf("\nThanks to you! %s", $this->love));
$output->writeln('Please consider contributing back in any way if you can!');
$output->writeln("\nPlease consider contributing back in any way if you can!");
$output->writeln(sprintf("\nRun <comment>composer fund</> to discover how you can sponsor your fellow PHP package maintainers %s", $this->cash));
$output->writeln(sprintf("\nThanks you! %s", $this->love));

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/GitHubClient.php
Expand Up @@ -144,13 +144,13 @@ public function getRepositories(array &$failures = null)
ksort($urls);

$i = 0;
$template = '_%d: repository(owner:"%s",name:"%s"){id,viewerHasStarred}'."\n";
$template = '_%d: repository(owner:"%s",name:"%s"){id,viewerHasStarred,fundingLinks{platform,url}}'."\n";
$graphql = '';

foreach ($urls as $package => $url) {
if (preg_match('#^https://github.com/([^/]++)/(.*?)(?:\.git)?$#i', $url, $url)) {
$graphql .= sprintf($template, ++$i, $url[1], $url[2]);
$aliases['_'.$i] = [$package, $url[0]];
$aliases['_'.$i] = [$package, sprintf('https://github.com/%s/%s', $url[1], $url[2])];
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Thanks.php
Expand Up @@ -56,6 +56,7 @@ public function activate(Composer $composer, IOInterface $io)
}

$app->add(new Command\ThanksCommand());
$app->add(new Command\FundCommand());
break;
}
}
Expand Down

0 comments on commit 36ace22

Please sign in to comment.