117 lines
5.1 KiB
PHP
117 lines
5.1 KiB
PHP
<?php
|
|
|
|
// Build only: no application bootstrap, database connection, installation or activation.
|
|
use App\Services\StandaloneArtifact;
|
|
use App\Services\StandaloneRuntime;
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
$options = getopt('', ['financial-source:', 'output:']);
|
|
$source = isset($options['financial-source']) && is_string($options['financial-source']) ? realpath($options['financial-source']) : false;
|
|
$output = isset($options['output']) && is_string($options['output']) ? realpath($options['output']) : false;
|
|
if ($source === false || $output === false || ! is_dir($source) || ! is_dir($output) || is_link($options['output'])) {
|
|
throw new LogicException('Explicit existing canonical source and output directories required.');
|
|
}
|
|
$sources = [];
|
|
foreach (StandaloneArtifact::PACKAGES as $package) {
|
|
$directory = $package === 'purchasing' ? dirname(__DIR__).'/packages/purchasing' : $source.'/'.$package;
|
|
foreach (['bootstrap.php', 'README.md', 'module.json'] as $file) {
|
|
if (is_file($directory.'/'.$file)) {
|
|
$sources[$package.'/'.$file] = $directory.'/'.$file;
|
|
}
|
|
}
|
|
foreach (['src', 'ui'] as $subdirectory) {
|
|
if (! is_dir($directory.'/'.$subdirectory)) {
|
|
continue;
|
|
}
|
|
foreach (new DirectoryIterator($directory.'/'.$subdirectory) as $file) {
|
|
if ($file->isDot()) {
|
|
continue;
|
|
}
|
|
if (! $file->isFile() || $file->isLink() || ! preg_match('/^[A-Za-z0-9_-]+\.php$/D', $file->getFilename())) {
|
|
throw new LogicException('Unexpected canonical runtime file.');
|
|
}
|
|
$sources[$package.'/'.$subdirectory.'/'.$file->getFilename()] = $file->getPathname();
|
|
}
|
|
}
|
|
}
|
|
ksort($sources, SORT_STRING);
|
|
$content = [];
|
|
$files = [];
|
|
foreach ($sources as $name => $file) {
|
|
if (is_link($file) || ! is_file($file)) {
|
|
throw new LogicException('Canonical source must contain regular files.');
|
|
}
|
|
$bytes = file_get_contents($file);
|
|
if (! is_string($bytes)) {
|
|
throw new LogicException('Unable to read canonical runtime source.');
|
|
}
|
|
$content[$name] = $bytes;
|
|
$files[] = ['file' => $name, 'sha256' => hash('sha256', $bytes)];
|
|
}
|
|
$manifest = json_encode(['contract' => StandaloneArtifact::CONTRACT, 'product' => 'compras', 'version' => 1,
|
|
'capabilities' => StandaloneRuntime::FEATURES, 'files' => $files], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)."\n";
|
|
$pin = hash('sha256', $manifest);
|
|
$target = $output.'/compras-standalone-'.substr($pin, 0, 16);
|
|
if (! file_exists($target)) {
|
|
if (! mkdir($target, 0700)) {
|
|
throw new LogicException('Unable to create candidate directory.');
|
|
}
|
|
foreach ($content as $name => $bytes) {
|
|
$destination = $target.'/'.$name;
|
|
if (! is_dir(dirname($destination)) && ! mkdir(dirname($destination), 0700, true)) {
|
|
throw new LogicException('Unable to create candidate package directory.');
|
|
}
|
|
$handle = fopen($destination, 'x');
|
|
if ($handle === false || fwrite($handle, $bytes) !== strlen($bytes)) {
|
|
throw new LogicException('Unable to write candidate package.');
|
|
}
|
|
fclose($handle);
|
|
}
|
|
$handle = fopen($target.'/manifest.json', 'x');
|
|
if ($handle === false || fwrite($handle, $manifest) !== strlen($manifest)) {
|
|
throw new LogicException('Unable to write candidate manifest.');
|
|
}
|
|
fclose($handle);
|
|
}
|
|
(new StandaloneArtifact)->verify($target, $pin);
|
|
$archive = $target.'.zip';
|
|
if (is_link($archive)) {
|
|
throw new LogicException('Candidate archive links refused.');
|
|
}
|
|
if (! file_exists($archive)) {
|
|
$zip = new ZipArchive;
|
|
if ($zip->open($archive, ZipArchive::CREATE | ZipArchive::EXCL) !== true) {
|
|
throw new LogicException('Unable to create candidate archive.');
|
|
}
|
|
foreach (array_keys($content) as $name) {
|
|
if (! $zip->addFile($target.'/'.$name, $name)) {
|
|
throw new LogicException('Unable to archive candidate package file.');
|
|
}
|
|
}
|
|
$zip->addFile($target.'/manifest.json', 'manifest.json');
|
|
if (! $zip->close()) {
|
|
throw new LogicException('Unable to finish candidate archive.');
|
|
}
|
|
}
|
|
$zip = new ZipArchive;
|
|
if ($zip->open($archive) !== true || $zip->numFiles !== count($files) + 1) {
|
|
throw new LogicException('Candidate archive inventory mismatch.');
|
|
}
|
|
$expected = ['manifest.json' => $manifest, ...$content];
|
|
$seen = [];
|
|
for ($i = 0; $i < $zip->numFiles; $i++) {
|
|
$name = $zip->getNameIndex($i);
|
|
if (! is_string($name) || isset($seen[$name]) || ! isset($expected[$name]) || $zip->getFromIndex($i) !== $expected[$name]) {
|
|
throw new LogicException('Candidate archive bytes mismatch.');
|
|
}
|
|
$seen[$name] = true;
|
|
}
|
|
$zip->close();
|
|
$archiveHash = hash_file('sha256', $archive);
|
|
if (! is_string($archiveHash)) {
|
|
throw new LogicException('Unable to hash candidate archive.');
|
|
}
|
|
echo json_encode(['root' => $target, 'manifest_sha256' => $pin, 'files' => count($files),
|
|
'archive' => $archive, 'archive_sha256' => $archiveHash,
|
|
'state' => 'candidate_requires_operational_validation', 'installed_or_activated' => false], JSON_THROW_ON_ERROR).PHP_EOL;
|