refactor: Grande migration de fichiers...
- Les fichiers de compilations sont dans compile/ - Le Phar ne présente que les fichiers de public/ - Autochargement de PclZip si besoin, avec Composer - Correction http_no_cache qui ne créait pas le bon format de headers pour Response - Correction CSS du pied de page si pas de version (cas des erreurs)pull/27/head
parent
83f467b61f
commit
be6ad0e4ba
@ -1,145 +1,30 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
namespace Spip\Loader\Compiler;
|
||||
|
||||
use CallbackFilterIterator;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use FilesystemIterator;
|
||||
use Iterator;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
use Seld\PharUtils\Timestamps;
|
||||
use Spip\Loader\Compiler\Build;
|
||||
use League\CLImate\CLImate;
|
||||
|
||||
error_reporting(E_ALL);
|
||||
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
|
||||
class Git {
|
||||
public function getVersion(): string {
|
||||
return trim(exec('git describe --tags --abbrev=0'));
|
||||
}
|
||||
|
||||
public function getVersionDate(): DateTimeImmutable {
|
||||
$date = trim(exec('git log -n1 --pretty=%ci HEAD'));
|
||||
$date = new DateTimeImmutable($date ?: null);
|
||||
$date->setTimezone(new DateTimeZone('UTC'));
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Site {
|
||||
public function __construct(
|
||||
private string $siteDirectory,
|
||||
private string $assetsDirectory,
|
||||
) {}
|
||||
public function prepare(): void {
|
||||
if (!is_dir($this->siteDirectory)) {
|
||||
mkdir($this->siteDirectory);
|
||||
}
|
||||
|
||||
foreach ([
|
||||
'spip_loader.php',
|
||||
'index.html',
|
||||
'logo-spip.png',
|
||||
'titre_logo_installer.svg',
|
||||
'favicon_rose.ico',
|
||||
'spip_loader.phar',
|
||||
'.htaccess',
|
||||
] as $clean_file) {
|
||||
if (file_exists($this->siteDirectory . '/' . $clean_file)) {
|
||||
unlink($this->siteDirectory . '/' . $clean_file);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function putVersion(string $version) {
|
||||
file_put_contents($this->siteDirectory . '/version', $version);
|
||||
}
|
||||
|
||||
public function copyAssets() {
|
||||
$assets = new FilesystemIterator(
|
||||
$this->assetsDirectory,
|
||||
FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS
|
||||
);
|
||||
$assets = array_keys(iterator_to_array($assets));
|
||||
foreach ($assets as $asset) {
|
||||
copy($asset, $this->siteDirectory . '/' . basename($asset));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Phar {
|
||||
|
||||
public function __construct(
|
||||
private string $siteDirectory,
|
||||
private string $sourceDirectory,
|
||||
private string $filename = 'spip_loader.phar'
|
||||
) {}
|
||||
|
||||
public function putVersionInSourceDirectory(string $version) {
|
||||
file_put_contents($this->sourceDirectory . '/version.php', '<?php return \'' . $version . '\';');
|
||||
}
|
||||
|
||||
public function getPhpFiles(): Iterator {
|
||||
$iterator = new RecursiveDirectoryIterator(
|
||||
$this->sourceDirectory,
|
||||
RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS
|
||||
);
|
||||
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
|
||||
$iterator = new CallbackFilterIterator($iterator, function($file) {
|
||||
return $file->isFile() && (
|
||||
$file->getExtension() === 'php'
|
||||
or $file->getExtension() === 'php8' # 1.19.0 de symfony/polyfill-mbstring/Resources/mb_convert_variables.php8
|
||||
);
|
||||
});
|
||||
return $iterator;
|
||||
}
|
||||
|
||||
public function getAssetsFiles(): Iterator {
|
||||
return new FilesystemIterator(
|
||||
$this->sourceDirectory . '/assets',
|
||||
FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS
|
||||
);
|
||||
}
|
||||
|
||||
public function build(DateTimeImmutable $versionDate) {
|
||||
$file = $this->siteDirectory . '/' . $this->filename;
|
||||
$p = new \Phar($file, 0, $this->filename);
|
||||
$p->setSignatureAlgorithm(\Phar::SHA512);
|
||||
$p->startBuffering();
|
||||
$p->setStub(file_get_contents('stub.php'));
|
||||
foreach ($this->getPhpFiles() as $phpFile) {
|
||||
$_file = str_replace($this->sourceDirectory . '/', '', $phpFile);
|
||||
$p[$_file] = file_get_contents($phpFile);
|
||||
$p[$_file]->compress(\Phar::GZ);
|
||||
}
|
||||
$p->buildFromIterator($this->getAssetsFiles(), $this->sourceDirectory);
|
||||
$p->stopBuffering();
|
||||
#$p->compressFiles(\Phar::GZ); # casse les assets
|
||||
|
||||
$util = new Timestamps($file);
|
||||
$util->updateTimestamps($versionDate);
|
||||
$util->save($file, \Phar::SHA512);
|
||||
}
|
||||
|
||||
public function renameTo(string $filename) {
|
||||
rename($this->siteDirectory . '/spip_loader.phar', $this->siteDirectory . '/' . $filename);
|
||||
}
|
||||
$build = new Build(
|
||||
sourceDirectory: __DIR__ . '/loader',
|
||||
buildDirectory: __DIR__ . '/build',
|
||||
pharFilename: 'spip_loader.php'
|
||||
);
|
||||
|
||||
$build->prepare();
|
||||
$build->run();
|
||||
|
||||
$climate = new CLImate;
|
||||
$climate->out('');
|
||||
$climate->underline()->out('Results');
|
||||
$climate->out('');
|
||||
foreach($build->getStats() as $item => $value) {
|
||||
$climate->infoInline("$item: ");
|
||||
$climate->out($value);
|
||||
}
|
||||
|
||||
$git = new Git();
|
||||
|
||||
$site = new Site(__DIR__ . '/public_html', __DIR__ . '/assets');
|
||||
$site->prepare();
|
||||
$site->putVersion($git->getVersion());
|
||||
$site->copyAssets();
|
||||
|
||||
$phar = new Phar(__DIR__ . '/public_html', __DIR__ . '/loader');
|
||||
$phar->putVersionInSourceDirectory($git->getVersion());
|
||||
$phar->build($git->getVersionDate());
|
||||
$phar->renameTo('spip_loader.php');
|
||||
$climate->out('');
|
||||
|
||||
exit(0);
|
||||
|
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Spip\Loader\Compiler;
|
||||
use SebastianBergmann\Timer\Duration;
|
||||
use SebastianBergmann\Timer\ResourceUsageFormatter;
|
||||
use SebastianBergmann\Timer\Timer;
|
||||
|
||||
class Build {
|
||||
private array $stats = [];
|
||||
|
||||
public function __construct(
|
||||
private string $sourceDirectory,
|
||||
private string $buildDirectory,
|
||||
private string $pharFilename,
|
||||
private Timer $timer = new Timer(),
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
public function prepare(): void {
|
||||
if (!is_dir($this->buildDirectory)) {
|
||||
mkdir($this->buildDirectory);
|
||||
}
|
||||
|
||||
foreach ([
|
||||
'spip_loader.php',
|
||||
'version',
|
||||
] as $clean_file) {
|
||||
if (file_exists($this->buildDirectory . '/' . $clean_file)) {
|
||||
unlink($this->buildDirectory . '/' . $clean_file);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function run() {
|
||||
$this->start();
|
||||
$git = new Git(dirname(__DIR__));
|
||||
$phar = new PharArchive(
|
||||
sourceDirectory: $this->sourceDirectory,
|
||||
buildDirectory: $this->buildDirectory,
|
||||
);
|
||||
$n = $phar->build(
|
||||
$git->getVersion(),
|
||||
$git->getVersionDate()
|
||||
);
|
||||
$this->stats['Nb files'] = $n;
|
||||
$phar->renameTo($this->pharFilename);
|
||||
file_put_contents($this->buildDirectory . '/version', $git->getVersion());
|
||||
$this->stop();
|
||||
}
|
||||
|
||||
public function getStats(): array {
|
||||
return $this->stats;
|
||||
}
|
||||
|
||||
private function start() {
|
||||
$this->stats = [
|
||||
'Version' => 'No version file',
|
||||
'Time' => '',
|
||||
'Memory' => '',
|
||||
'Filesize' => 'No archive file',
|
||||
'Nb files' => '',
|
||||
];
|
||||
$this->timer->start();
|
||||
}
|
||||
|
||||
private function stop() {
|
||||
$this->saveStats($this->timer->stop());
|
||||
}
|
||||
|
||||
private function saveStats(Duration $duration) {
|
||||
[$time, $memory] = $this->parseResourceUsage($duration);
|
||||
$this->stats['Time'] = $time;
|
||||
$this->stats['Memory'] = $memory;
|
||||
$pharPath = $this->buildDirectory . '/' . $this->pharFilename;
|
||||
if (file_exists($pharPath)) {
|
||||
$this->stats['Filesize'] = $this->MakeReadable(filesize($pharPath));
|
||||
}
|
||||
if (file_exists($this->buildDirectory . '/version')) {
|
||||
$this->stats['Version'] = file_get_contents($this->buildDirectory . '/version');
|
||||
}
|
||||
}
|
||||
|
||||
private function parseResourceUsage(Duration $duration): array {
|
||||
$usage = (new ResourceUsageFormatter)->resourceUsage($duration);
|
||||
[$time, $memory] = explode(', ', $usage, 2);
|
||||
return [
|
||||
explode(': ', $time)[1],
|
||||
explode(': ', $memory)[1],
|
||||
];
|
||||
}
|
||||
|
||||
private function MakeReadable($bytes) {
|
||||
$i = floor(log($bytes, 1024));
|
||||
return round($bytes / pow(1024, $i), [0,0,2,2,3][$i]).['B','kiB','MiB','GiB','TiB'][$i];
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Spip\Loader\Compiler;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
|
||||
class Git {
|
||||
public function __construct(private string $directory = __DIR__) {}
|
||||
|
||||
public function setDirectory(string $directory) {
|
||||
$this->directory = $directory;
|
||||
}
|
||||
|
||||
public function run(string $command): string {
|
||||
$git = 'git -C ' . $this->directory;
|
||||
return trim(exec($git . ' ' . $command));
|
||||
}
|
||||
|
||||
public function getVersion(): string {
|
||||
return $this->run('describe --tags --abbrev=0');
|
||||
}
|
||||
|
||||
public function getVersionDate(): DateTimeImmutable {
|
||||
$date = $this->run('log -n1 --pretty=%ci HEAD');
|
||||
$date = new DateTimeImmutable($date ?: null);
|
||||
$date->setTimezone(new DateTimeZone('UTC'));
|
||||
return $date;
|
||||
}
|
||||
}
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 4.8 KiB After Width: | Height: | Size: 4.8 KiB |
Loading…
Reference in New Issue