|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +$root = dirname(__DIR__, 2); |
| 6 | +$pluginFile = $root . '/plugin.yml'; |
| 7 | +if (!is_file($pluginFile)) { |
| 8 | + fwrite(STDERR, "plugin.yml was not found.\n"); |
| 9 | + exit(1); |
| 10 | +} |
| 11 | + |
| 12 | +$pluginYml = file_get_contents($pluginFile); |
| 13 | +if ($pluginYml === false) { |
| 14 | + fwrite(STDERR, "Cannot read plugin.yml.\n"); |
| 15 | + exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +$name = readPluginValue($pluginYml, 'name'); |
| 19 | +$version = readPluginValue($pluginYml, 'version'); |
| 20 | +$safeName = sanitizeAssetPart($name); |
| 21 | +$safeVersion = sanitizeAssetPart($version); |
| 22 | +$dist = $root . '/dist'; |
| 23 | +if (!is_dir($dist) && !mkdir($dist, 0775, true) && !is_dir($dist)) { |
| 24 | + fwrite(STDERR, "Cannot create dist directory.\n"); |
| 25 | + exit(1); |
| 26 | +} |
| 27 | + |
| 28 | +$pharPath = $dist . '/' . $safeName . '-' . $safeVersion . '.phar'; |
| 29 | +if (is_file($pharPath) && !unlink($pharPath)) { |
| 30 | + fwrite(STDERR, "Cannot remove previous PHAR: {$pharPath}\n"); |
| 31 | + exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +$phar = new Phar($pharPath); |
| 35 | +$phar->startBuffering(); |
| 36 | +foreach ([ |
| 37 | + 'plugin.yml', |
| 38 | + 'src', |
| 39 | + 'resources', |
| 40 | + 'icon.png', |
| 41 | + 'icon.gif', |
| 42 | + 'LICENSE', |
| 43 | +] as $entry) { |
| 44 | + addPath($phar, $root, $root . '/' . $entry); |
| 45 | +} |
| 46 | +$phar->setStub("<?php __HALT_COMPILER();\n"); |
| 47 | +$phar->setSignatureAlgorithm(Phar::SHA256); |
| 48 | +$phar->stopBuffering(); |
| 49 | + |
| 50 | +$hash = hash_file('sha256', $pharPath); |
| 51 | +if (!is_string($hash)) { |
| 52 | + fwrite(STDERR, "Cannot calculate PHAR checksum.\n"); |
| 53 | + exit(1); |
| 54 | +} |
| 55 | + |
| 56 | +$asset = basename($pharPath); |
| 57 | +file_put_contents($dist . '/checksums.txt', $hash . ' ' . $asset . PHP_EOL); |
| 58 | +file_put_contents( |
| 59 | + $dist . '/release.env', |
| 60 | + implode("\n", [ |
| 61 | + 'NAME=' . $safeName, |
| 62 | + 'VERSION=' . $safeVersion, |
| 63 | + 'TAG=v' . $safeVersion, |
| 64 | + 'PHAR=dist/' . $asset, |
| 65 | + ]) . "\n" |
| 66 | +); |
| 67 | + |
| 68 | +echo "Built {$asset}\n"; |
| 69 | +echo "SHA-256 {$hash}\n"; |
| 70 | + |
| 71 | +function readPluginValue(string $pluginYml, string $key): string { |
| 72 | + if (preg_match('/^\s*' . preg_quote($key, '/') . '\s*:\s*["\']?([^"\'#\r\n]+)["\']?/m', $pluginYml, $matches) !== 1) { |
| 73 | + fwrite(STDERR, "plugin.yml is missing {$key}.\n"); |
| 74 | + exit(1); |
| 75 | + } |
| 76 | + |
| 77 | + return trim($matches[1]); |
| 78 | +} |
| 79 | + |
| 80 | +function sanitizeAssetPart(string $value): string { |
| 81 | + $value = trim($value); |
| 82 | + if ($value === '' || preg_match('/^[A-Za-z0-9_.-]+$/', $value) !== 1) { |
| 83 | + fwrite(STDERR, "Invalid release asset value: {$value}\n"); |
| 84 | + exit(1); |
| 85 | + } |
| 86 | + |
| 87 | + return $value; |
| 88 | +} |
| 89 | + |
| 90 | +function addPath(Phar $phar, string $root, string $path): void { |
| 91 | + if (!file_exists($path)) { |
| 92 | + return; |
| 93 | + } |
| 94 | + if (is_file($path)) { |
| 95 | + $phar->addFile($path, relativePath($root, $path)); |
| 96 | + return; |
| 97 | + } |
| 98 | + if (!is_dir($path)) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + $iterator = new RecursiveIteratorIterator( |
| 103 | + new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
| 104 | + ); |
| 105 | + foreach ($iterator as $file) { |
| 106 | + if ($file instanceof SplFileInfo && $file->isFile()) { |
| 107 | + $phar->addFile($file->getPathname(), relativePath($root, $file->getPathname())); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function relativePath(string $root, string $path): string { |
| 113 | + return str_replace('\\', '/', substr($path, strlen($root) + 1)); |
| 114 | +} |
0 commit comments