Skip to content

Commit 27766b4

Browse files
Merge pull request #156 from phel-lang/update-phel-version-script
Create script to read current Phel version
2 parents c5c6dc2 + 2a2de6a commit 27766b4

File tree

9 files changed

+193
-3
lines changed

9 files changed

+193
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhelWeb\VersionUpdater;
6+
7+
use Gacela\Framework\AbstractProvider;
8+
use Gacela\Framework\Container\Container;
9+
use Phel\Console\ConsoleFacade;
10+
use Phel\Shared\Facade\ConsoleFacadeInterface;
11+
12+
/**
13+
* @method Factory getFactory()
14+
*/
15+
final class DependencyProvider extends AbstractProvider
16+
{
17+
public const string FACADE_PHEL_CONSOLE = 'FACADE_PHEL_CONSOLE';
18+
19+
public function provideModuleDependencies(Container $container): void
20+
{
21+
$container->set(self::FACADE_PHEL_CONSOLE, function (Container $container) {
22+
return $container->getLocator()->get(ConsoleFacade::class);
23+
});
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhelWeb\VersionUpdater;
6+
7+
use Gacela\Framework\AbstractFacade;
8+
9+
/**
10+
* @method Factory getFactory()
11+
*/
12+
final class Facade extends AbstractFacade
13+
{
14+
public function updateTomlFile(): void
15+
{
16+
$this->getFactory()
17+
->createPhelVersionUpdater()
18+
->update();
19+
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhelWeb\VersionUpdater;
6+
7+
use Gacela\Framework\AbstractFactory;
8+
use Gacela\Framework\Config\Config;
9+
use Phel\Shared\Facade\ConsoleFacadeInterface;
10+
use PhelWeb\VersionUpdater\Infrastructure\PhelVersionUpdater;
11+
12+
/**
13+
* @method Config getConfig()
14+
*/
15+
final class Factory extends AbstractFactory
16+
{
17+
public function createPhelVersionUpdater(): PhelVersionUpdater
18+
{
19+
return new PhelVersionUpdater(
20+
$this->getPhelConsoleFacade(),
21+
$this->getConfigFileLocation(),
22+
);
23+
}
24+
25+
private function getPhelConsoleFacade(): ConsoleFacadeInterface
26+
{
27+
return $this->getProvidedDependency(DependencyProvider::FACADE_PHEL_CONSOLE);
28+
}
29+
30+
private function getConfigFileLocation(): string
31+
{
32+
return $this->getConfig()->getAppRootDir() . '/../config.toml';
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhelWeb\VersionUpdater\Infrastructure;
6+
7+
use Phel\Shared\Facade\ConsoleFacadeInterface;
8+
9+
final readonly class PhelVersionUpdater
10+
{
11+
private const string REGEX_PHEL_VERSION_FINDER = '/^phel_version\s*=\s*"[^"]*"/m';
12+
13+
public function __construct(
14+
private ConsoleFacadeInterface $consoleFacade,
15+
private string $configFile,
16+
) {
17+
}
18+
19+
public function update(): void
20+
{
21+
$configContent = file_get_contents($this->configFile);
22+
23+
$phelVersion = $this->consoleFacade->getVersion();
24+
$updatedContent = preg_replace(
25+
self::REGEX_PHEL_VERSION_FINDER,
26+
'phel_version = "' . $phelVersion . '"',
27+
$configContent
28+
);
29+
30+
if ($updatedContent !== $configContent) {
31+
file_put_contents($this->configFile, $updatedContent);
32+
}
33+
}
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhelWebTests\VersionUpdater\Integration;
6+
7+
use Phel\Shared\Facade\ConsoleFacadeInterface;
8+
use PhelWeb\VersionUpdater\Infrastructure\PhelVersionUpdater;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class PhelVersionUpdaterTest extends TestCase
12+
{
13+
/** @var false|resource */
14+
private $file;
15+
16+
protected function setUp(): void
17+
{
18+
$this->file = tmpfile();
19+
fwrite($this->file, 'phel_version = "v.0.9"');
20+
}
21+
22+
protected function tearDown(): void
23+
{
24+
fclose($this->file);
25+
}
26+
27+
public function test_update_phel_version(): void
28+
{
29+
$consoleFacade = $this->createAnonConsoleFacade();
30+
31+
$path = stream_get_meta_data($this->file)['uri'];
32+
$phelVersionUpdater = new PhelVersionUpdater($consoleFacade, $path);
33+
$phelVersionUpdater->update();
34+
35+
$content = file_get_contents($path);
36+
37+
$expected = 'phel_version = "v1.0"';
38+
39+
self::assertSame($expected, $content);
40+
}
41+
42+
private function createAnonConsoleFacade(): ConsoleFacadeInterface
43+
{
44+
return new class implements ConsoleFacadeInterface {
45+
46+
public function getVersion(): string
47+
{
48+
return 'v1.0';
49+
}
50+
51+
public function runConsole(): void
52+
{
53+
// ignore
54+
}
55+
};
56+
}
57+
}

build/update-phel-version.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
use Gacela\Framework\Gacela;
8+
use Phel\Phel;
9+
use PhelWeb\VersionUpdater\Facade as VersionUpdaterFacade;
10+
11+
Gacela::bootstrap(__DIR__, Phel::configFn());
12+
13+
$facade = new VersionUpdaterFacade();
14+
$facade->updateTomlFile();

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
"php build/api-page.php",
4242
"php build/api-search.php"
4343
],
44-
"test": "./vendor/bin/phpunit"
44+
"test": "./vendor/bin/phpunit",
45+
"post-install-cmd": [
46+
"php build/update-phel-version.php"
47+
],
48+
"post-update-cmd": [
49+
"php build/update-phel-version.php"
50+
]
4551
}
4652
}

config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ extra_syntaxes_and_themes = ["syntaxes"]
2828
# Put all your custom variables here
2929

3030
repo_content_url = "https://github.com/phel-lang/phel-lang.org/blob/master/content/"
31-
phel_version = "0.23.1"
31+
phel_version = "v0.23.1"
3232

templates/footer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h3>Community</h3>
5252
<div class="footer-badges">
5353
<span class="footer-badge">PHP 8.3+ Required</span>
5454
<span class="footer-badge-separator"></span>
55-
<span class="footer-badge footer-badge-version">v{{ config.extra.phel_version }}</span>
55+
<span class="footer-badge footer-badge-version">{{ config.extra.phel_version }}</span>
5656
</div>
5757
</div>
5858
</div>

0 commit comments

Comments
 (0)