Commit a32fbe96 authored by Emma's avatar Emma 🏳🌈

ditch VersionPass in favour of env variables

parent cd24aaf0
Pipeline #60753196 passed with stages
in 10 minutes and 52 seconds
......@@ -108,7 +108,9 @@ RATELIMIT_WHITELIST=
APP_ENABLE_WEBHOOKS=0
### Misc config files
### Misc configuration
APP_FONTS=%kernel.project_dir%/assets/fonts.json
APP_THEMES=%kernel.project_dir%/assets/themes.json
APP_BRANCH=$(git rev-parse --abbrev-ref HEAD)
APP_VERSION=$(git describe --tags)
......@@ -86,6 +86,10 @@ services:
App\Security\Voter\TokenVoter:
$forumCreationInterval: "%user_forum_creation_interval%"
App\Twig\AppExtension:
$branch: "%env(APP_BRANCH)%"
$version: "%env(APP_VERSION)%"
App\Utils\IpRateLimitFactory:
$cache: "@cache.ratelimit"
$ipWhitelist: "%ratelimit_ip_whitelist%"
......
<?php
namespace App\DependencyInjection\Compiler;
use App\Twig\AppExtension;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Process\Process;
/**
* Compiler pass that makes the current version number of the software available
* to templates.
*/
final class VersionPass implements CompilerPassInterface {
const COMPOSER_JSON = __DIR__.'/../../../composer.json';
public function process(ContainerBuilder $container) {
if (!$container->has(AppExtension::class)) {
return;
}
$branchName = $this->getGitBranchName();
$branchAlias = $this->getComposerBranchAlias($branchName);
$tagName = $this->getGitTagName();
$extensionDefinition = $container->getDefinition(AppExtension::class);
$extensionDefinition->setArgument('$branch', $branchAlias ?? $branchName);
$extensionDefinition->setArgument('$version', $tagName);
}
private function getGitBranchName(): ?string {
return $this->getVersionFromCommand(['git', 'rev-parse', '--abbrev-ref', 'HEAD']);
}
private function getGitTagName(): ?string {
return $this->getVersionFromCommand(['git', 'describe', '--tags']);
}
private function getComposerBranchAlias(?string $branch): ?string {
if ($branch !== null) {
$content = json_decode(file_get_contents(self::COMPOSER_JSON));
$key = preg_match('/^\d+(\.\d+){1,2}$/', $branch) ? $branch : "dev-$branch";
if (isset($content->extra->{'branch-alias'}->{$key})) {
return $content->extra->{'branch-alias'}->{$key};
}
}
return null;
}
private function getVersionFromCommand(array $commandLine): ?string {
$process = new Process($commandLine);
$process->run();
$output = trim($process->getOutput());
if ($process->isSuccessful() && strlen($output) > 0) {
return preg_split('/\r?\n/', $output, 2)[0];
}
return null;
}
}
......@@ -2,7 +2,6 @@
namespace App;
use App\DependencyInjection\Compiler\VersionPass;
use FOS\HttpCache\SymfonyCache\HttpCacheAware;
use FOS\HttpCache\SymfonyCache\HttpCacheProvider;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
......@@ -62,8 +61,4 @@ class Kernel extends BaseKernel implements HttpCacheProvider {
}
$routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
}
protected function build(ContainerBuilder $container) {
$container->addCompilerPass(new VersionPass());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment