Skip to content

Commit

Permalink
Add support for premium extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rob006 committed May 25, 2020
1 parent 6c34b9b commit 47bfb46
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 168 deletions.
15 changes: 9 additions & 6 deletions commands/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use app\components\ConsoleController;
use app\components\extensions\ConfigGenerator;
use app\models\RegularExtension;
use app\models\Repository;
use app\models\Translations;
use Yii;
Expand Down Expand Up @@ -66,12 +67,13 @@ public function actionUpdate(array $projects = [], string $configFile = '@app/tr
Yii::warning("Unable to update {$component->getId()} extension.", __METHOD__);
continue;
}

$configGenerator->updateExtension($extension);
$this->commitRepository(
$translations->getRepository(),
"Update config for {$component->getId()}.\n\n{$extension->getTranslationTagsUrl()}"
);
if ($extension instanceof RegularExtension) {
$configGenerator->updateExtension($extension);
$this->commitRepository(
$translations->getRepository(),
"Update config for {$component->getId()}.\n\n{$extension->getTranslationTagsUrl()}"
);
}
}
}
}
Expand All @@ -86,6 +88,7 @@ private function getTranslations(string $configFile): Translations {
null,
require Yii::getAlias($configFile)
);
Yii::$app->extensionsRepository->setPremiumExtensions($translations->getPremiumExtensionsConfig());
if ($this->update) {
$output = $translations->getRepository()->update();
if ($this->verbose) {
Expand Down
5 changes: 5 additions & 0 deletions commands/ExtensionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use app\components\ConsoleController;
use app\components\extensions\PullRequestGenerator;
use app\models\ForkRepository;
use app\models\RegularExtension;
use app\models\Translations;
use Yii;
use const APP_ROOT;
Expand Down Expand Up @@ -79,6 +80,9 @@ public function actionDetectNew(int $limit = 2, string $configFile = '@app/trans
if (!$extension->hasTranslationSource()) {
unset($extensions[$index]);
}
if (!$extension instanceof RegularExtension) {
unset($extensions[$index]);
}
}

$repository = new ForkRepository(
Expand All @@ -99,6 +103,7 @@ private function getTranslations(string $configFile): Translations {
null,
require Yii::getAlias($configFile)
);
Yii::$app->extensionsRepository->setPremiumExtensions($translations->getPremiumExtensionsConfig());
if ($this->update) {
$output = $translations->getRepository()->update();
if ($this->verbose) {
Expand Down
1 change: 1 addition & 0 deletions commands/JanitorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ private function getTranslations(string $configFile): Translations {
null,
require Yii::getAlias($configFile)
);
Yii::$app->extensionsRepository->setPremiumExtensions($translations->getPremiumExtensionsConfig());
$translations->getRepository()->update();

return $translations;
Expand Down
1 change: 1 addition & 0 deletions commands/ReadmeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private function getTranslations(string $configFile): Translations {
null,
require Yii::getAlias($configFile)
);
Yii::$app->extensionsRepository->setPremiumExtensions($translations->getPremiumExtensionsConfig());
if ($this->update) {
$output = $translations->getRepository()->update();
if ($this->verbose) {
Expand Down
1 change: 1 addition & 0 deletions commands/ReleaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private function getTranslations(string $configFile): Translations {
null,
require Yii::getAlias($configFile)
);
Yii::$app->extensionsRepository->setPremiumExtensions($translations->getPremiumExtensionsConfig());
if ($this->update) {
$output = $translations->getRepository()->update();
if ($this->verbose) {
Expand Down
4 changes: 3 additions & 1 deletion commands/StatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace app\commands;

use app\components\extensions\LanguageStatsGenerator;
use app\models\RegularExtension;
use app\models\Repository;
use app\models\Translations;
use Yii;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function actionUpdate(array $languages = [], string $configFile = '@app/t
foreach ($translations->getProjects() as $project) {
foreach ($project->getExtensionsComponents() as $component) {
$extension = Yii::$app->extensionsRepository->getExtension($component->getId());
if ($extension !== null) {
if ($extension instanceof RegularExtension) {
$generator->addExtension($extension, !$component->isValidForLanguage($language));
}
}
Expand Down Expand Up @@ -97,6 +98,7 @@ private function getTranslations(string $configFile): Translations {
null,
require Yii::getAlias($configFile)
);
Yii::$app->extensionsRepository->setPremiumExtensions($translations->getPremiumExtensionsConfig());
if ($this->update) {
$output = $translations->getRepository()->update();
if ($this->verbose) {
Expand Down
1 change: 1 addition & 0 deletions commands/TranslationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private function getTranslations(string $configFile): Translations {
null,
require Yii::getAlias($configFile)
);
Yii::$app->extensionsRepository->setPremiumExtensions($translations->getPremiumExtensionsConfig());
if ($this->update) {
$output = $translations->getRepository()->update();
if ($this->verbose) {
Expand Down
25 changes: 14 additions & 11 deletions components/extensions/ConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace app\components\extensions;

use app\models\Extension;
use app\models\RegularExtension;
use Dont\DontCall;
use Dont\DontCallStatic;
use Dont\DontGet;
Expand Down Expand Up @@ -108,18 +109,20 @@ private function generateConfig(Extension $extension, $extensionConfig): string
$extensionConfig = [];
}

$extensionConfig['tag'] = $extension->getStableTranslationSourceUrl();
foreach ($extensionConfig as $key => $url) {
if (strncmp($key, 'tag:', 4) === 0) {
$extensionConfig[$key] = $extension->getStableTranslationSourceUrl([substr($key, 4)]);
if ($extension instanceof RegularExtension) {
$extensionConfig['tag'] = $extension->getStableTranslationSourceUrl();
foreach ($extensionConfig as $key => $url) {
if (strncmp($key, 'tag:', 4) === 0) {
$extensionConfig[$key] = $extension->getStableTranslationSourceUrl([substr($key, 4)]);
}
}
}
if (isset($extensionConfig['branch'])) {
$extensionConfig['branch'] = $extension->getTranslationSourceUrl();
}
foreach ($extensionConfig as $key => $url) {
if (strncmp($key, 'branch:', 7) === 0) {
$extensionConfig[$key] = $extension->getTranslationSourceUrl(substr($key, 4));
if (isset($extensionConfig['branch'])) {
$extensionConfig['branch'] = $extension->getTranslationSourceUrl();
}
foreach ($extensionConfig as $key => $url) {
if (strncmp($key, 'branch:', 7) === 0) {
$extensionConfig[$key] = $extension->getTranslationSourceUrl(substr($key, 4));
}
}
}

Expand Down
29 changes: 26 additions & 3 deletions components/extensions/ExtensionsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use app\components\extensions\exceptions\UnprocessableExtensionInterface;
use app\models\Extension;
use app\models\packagist\SearchResult;
use app\models\PremiumExtension;
use app\models\RegularExtension;
use Composer\Semver\Semver;
use Composer\Semver\VersionParser;
use mindplay\readable;
Expand Down Expand Up @@ -50,6 +52,7 @@ final class ExtensionsRepository extends Component {

private $_extensions;
private $_client;
private $premiumExtensions = [];

/**
* @param bool $useCache
Expand Down Expand Up @@ -94,6 +97,26 @@ public function getValidExtensions(array $supportedVersions, bool $useCache = tr
return $extensions;
}

// @todo I hope this is temporary solution - premium extensions config should be fetched directly from
// https://extiverse.com/, but I don't know any public and stable API for this.
public function setPremiumExtensions(array $extensionsConfig): void {
$this->_extensions = null;
$this->premiumExtensions = [];
foreach ($extensionsConfig as $id => $extension) {
if (!$extension instanceof PremiumExtension) {
$extension = new PremiumExtension(
$id,
$extension['name'],
$extension['vendor'],
$extension['packageName'],
$extension['repositoryUrl']
);

$this->premiumExtensions[$id] = $extension;
}
}
}

private function getClient(): HttpClientInterface {
if ($this->_client === null) {
$this->_client = HttpClient::create();
Expand All @@ -108,10 +131,10 @@ private function fetchExtensions(): array {
'per_page' => 100,
]);

$extensions = [];
$extensions = $this->premiumExtensions;
foreach ($results as $result) {
assert($result instanceof SearchResult);
$extension = Extension::createFromPackagistSearchResult($result);
$extension = RegularExtension::createFromPackagistSearchResult($result);
if (
!isset($extensions[$extension->getId()])
// handle ID conflicts
Expand All @@ -124,7 +147,7 @@ private function fetchExtensions(): array {
return $extensions;
}

private function compareExtensions(Extension $a, Extension $b): int {
private function compareExtensions(RegularExtension $a, RegularExtension $b): int {
if ($b->isAbandoned()) {
return 1;
}
Expand Down
10 changes: 5 additions & 5 deletions components/extensions/LanguageStatsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace app\components\extensions;

use app\models\Extension;
use app\models\RegularExtension;
use app\models\Project;
use Dont\DontCall;
use Dont\DontCallStatic;
Expand All @@ -43,7 +43,7 @@ final class LanguageStatsGenerator {
use DontGet;
use DontSet;

/** @var Extension[] */
/** @var RegularExtension[] */
private $extensions = [];
/** @var bool */
private $disabledExtensions = [];
Expand Down Expand Up @@ -72,15 +72,15 @@ public function __construct(string $language, array $projects, string $sortingCr
}
}

public function addExtension(Extension $extension, bool $isDisabled): void {
public function addExtension(RegularExtension $extension, bool $isDisabled): void {
$this->extensions[] = $extension;
$this->stats[$extension->getId()] = $this->getStats($extension->getPackageName());
$this->disabledExtensions[$extension->getId()] = $isDisabled;
}

public function generate(): string {
$extensions = $this->extensions;
usort($extensions, function (Extension $a, Extension $b) {
usort($extensions, function (RegularExtension $a, RegularExtension $b) {
$result = $this->stats[$b->getId()][$this->sortingCriteria] <=> $this->stats[$a->getId()][$this->sortingCriteria];
if ($result === 0) {
$result = $this->stats[$b->getId()]['total'] <=> $this->stats[$a->getId()]['total'];
Expand Down Expand Up @@ -163,7 +163,7 @@ private function image(string $src, ?string $alt = null): string {
]);
}

private function stats(Extension $extension, string $statsType) : string {
private function stats(RegularExtension $extension, string $statsType) : string {
$badge = $this->statsChangeBadge($extension->getPackageName(), $statsType, $this->stats[$extension->getId()][$statsType]);
$statsUrl = "https://packagist.org/packages/{$extension->getPackageName()}/stats";

Expand Down
12 changes: 6 additions & 6 deletions components/extensions/PullRequestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace app\components\extensions;

use app\components\GithubApi;
use app\models\Extension;
use app\models\RegularExtension;
use app\models\ForkRepository;
use Dont\DontCall;
use Dont\DontCallStatic;
Expand Down Expand Up @@ -46,7 +46,7 @@ public function __construct(ForkRepository $repository, ?GithubApi $githubApi =
}

/**
* @param Extension[] $extensions
* @param RegularExtension[] $extensions
* @param int $limit
*/
public function generateForNewExtensions(array $extensions, int $limit): void {
Expand Down Expand Up @@ -77,7 +77,7 @@ public function generateForNewExtensions(array $extensions, int $limit): void {
}
}

private function updateBranch(string $branchName, Extension $extension): bool {
private function updateBranch(string $branchName, RegularExtension $extension): bool {
$this->repository->checkoutBranch($branchName);
$this->addExtensionToConfig($extension);
$this->repository->commit("Update config for {$extension->getPackageName()}.", $commited);
Expand All @@ -86,13 +86,13 @@ private function updateBranch(string $branchName, Extension $extension): bool {
return $commited;
}

private function addExtensionToConfig(Extension $extension): void {
private function addExtensionToConfig(RegularExtension $extension): void {
$filePath = $this->repository->getPath() . "/config/{$extension->getProjectId()}-project.php";
$generator = new ConfigGenerator($filePath);
$generator->updateExtension($extension);
}

private function openPullRequestForNewExtension(string $branchName, Extension $extension): void {
private function openPullRequestForNewExtension(string $branchName, RegularExtension $extension): void {
$this->githubApi->openPullRequest(
Yii::$app->params['translationsRepository'],
Yii::$app->params['translationsForkRepository'],
Expand All @@ -115,7 +115,7 @@ private function updatePullRequestForNewExtension(string $branchName): void {
);
}

private function generatePullRequestBadges(Extension $extension): string {
private function generatePullRequestBadges(RegularExtension $extension): string {
$name = $extension->getPackageName();

$output = <<<MD
Expand Down
Loading

0 comments on commit 47bfb46

Please sign in to comment.