|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +$root = dirname(__DIR__, 2); |
| 6 | +$pluginFile = $root . '/plugin.yml'; |
| 7 | +$packageFile = $root . '/package.yml'; |
| 8 | + |
| 9 | +foreach ([$pluginFile, $packageFile] as $requiredFile) { |
| 10 | + if (!is_file($requiredFile)) { |
| 11 | + fwrite(STDERR, basename($requiredFile) . " was not found.\n"); |
| 12 | + exit(1); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +$pluginYml = (string) file_get_contents($pluginFile); |
| 17 | +$packageYml = (string) file_get_contents($packageFile); |
| 18 | +$name = readYamlScalar($pluginYml, 'name'); |
| 19 | +$version = readYamlScalar($pluginYml, 'version'); |
| 20 | +$packageId = readYamlScalar($packageYml, 'id'); |
| 21 | +$packageName = readYamlScalar($packageYml, 'name'); |
| 22 | +$packageVersion = readYamlScalar($packageYml, 'version'); |
| 23 | + |
| 24 | +if ($name !== $packageName) { |
| 25 | + fwrite(STDERR, "package.yml name {$packageName} does not match plugin.yml name {$name}.\n"); |
| 26 | + exit(1); |
| 27 | +} |
| 28 | +if ($version !== $packageVersion) { |
| 29 | + fwrite(STDERR, "package.yml version {$packageVersion} does not match plugin.yml version {$version}.\n"); |
| 30 | + exit(1); |
| 31 | +} |
| 32 | + |
| 33 | +$safeName = sanitizeAssetPart($name); |
| 34 | +$safeVersion = sanitizeAssetPart($version); |
| 35 | +$dist = $root . '/dist'; |
| 36 | +if (!is_dir($dist) && !mkdir($dist, 0775, true) && !is_dir($dist)) { |
| 37 | + fwrite(STDERR, "Cannot create dist directory.\n"); |
| 38 | + exit(1); |
| 39 | +} |
| 40 | + |
| 41 | +$asset = $safeName . '-' . $safeVersion . '.easylib.zip'; |
| 42 | +$zipPath = $dist . '/' . $asset; |
| 43 | +if (is_file($zipPath) && !unlink($zipPath)) { |
| 44 | + fwrite(STDERR, "Cannot remove previous package: {$zipPath}\n"); |
| 45 | + exit(1); |
| 46 | +} |
| 47 | + |
| 48 | +$zip = new PharData($zipPath); |
| 49 | +foreach ([ |
| 50 | + 'package.yml', |
| 51 | + 'src', |
| 52 | + 'resources', |
| 53 | + 'README.md', |
| 54 | + 'LICENSE', |
| 55 | +] as $entry) { |
| 56 | + addPath($zip, $root, $root . '/' . $entry); |
| 57 | +} |
| 58 | + |
| 59 | +$manifestAsset = $dist . '/package.yml'; |
| 60 | +if (!copy($packageFile, $manifestAsset)) { |
| 61 | + fwrite(STDERR, "Cannot copy package.yml into dist.\n"); |
| 62 | + exit(1); |
| 63 | +} |
| 64 | + |
| 65 | +$checksums = []; |
| 66 | +foreach (glob($dist . '/*') ?: [] as $file) { |
| 67 | + if (!is_file($file) || basename($file) === 'checksums.txt' || basename($file) === 'release.env') { |
| 68 | + continue; |
| 69 | + } |
| 70 | + $hash = hash_file('sha256', $file); |
| 71 | + if (!is_string($hash)) { |
| 72 | + fwrite(STDERR, "Cannot calculate checksum for {$file}.\n"); |
| 73 | + exit(1); |
| 74 | + } |
| 75 | + $checksums[] = $hash . ' ' . basename($file); |
| 76 | +} |
| 77 | +sort($checksums); |
| 78 | +file_put_contents($dist . '/checksums.txt', implode(PHP_EOL, $checksums) . PHP_EOL); |
| 79 | + |
| 80 | +$phar = $dist . '/' . $safeName . '-' . $safeVersion . '.phar'; |
| 81 | +$releaseEnv = [ |
| 82 | + 'NAME=' . $safeName, |
| 83 | + 'VERSION=' . $safeVersion, |
| 84 | + 'TAG=v' . $safeVersion, |
| 85 | + 'PHAR=' . (is_file($phar) ? 'dist/' . basename($phar) : ''), |
| 86 | + 'EASYLIB=dist/' . $asset, |
| 87 | + 'PACKAGE_MANIFEST=dist/package.yml', |
| 88 | + 'PACKAGE_ID=' . sanitizeAssetPart($packageId), |
| 89 | +]; |
| 90 | +file_put_contents($dist . '/release.env', implode("\n", $releaseEnv) . "\n"); |
| 91 | + |
| 92 | +echo "Built {$asset}\n"; |
| 93 | +echo "Package manifest dist/package.yml\n"; |
| 94 | + |
| 95 | +function readYamlScalar(string $yaml, string $key): string { |
| 96 | + if (preg_match('/^\s*' . preg_quote($key, '/') . '\s*:\s*["\']?([^"\'#\r\n]+)["\']?/m', $yaml, $matches) !== 1) { |
| 97 | + fwrite(STDERR, "YAML is missing {$key}.\n"); |
| 98 | + exit(1); |
| 99 | + } |
| 100 | + |
| 101 | + return trim($matches[1]); |
| 102 | +} |
| 103 | + |
| 104 | +function sanitizeAssetPart(string $value): string { |
| 105 | + $value = trim($value); |
| 106 | + if ($value === '' || preg_match('/^[A-Za-z0-9_.-]+$/', $value) !== 1) { |
| 107 | + fwrite(STDERR, "Invalid release asset value: {$value}\n"); |
| 108 | + exit(1); |
| 109 | + } |
| 110 | + |
| 111 | + return $value; |
| 112 | +} |
| 113 | + |
| 114 | +function addPath(PharData $zip, string $root, string $path): void { |
| 115 | + if (!file_exists($path)) { |
| 116 | + return; |
| 117 | + } |
| 118 | + if (is_file($path)) { |
| 119 | + $zip->addFile($path, relativePath($root, $path)); |
| 120 | + return; |
| 121 | + } |
| 122 | + if (!is_dir($path)) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + $iterator = new RecursiveIteratorIterator( |
| 127 | + new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
| 128 | + ); |
| 129 | + foreach ($iterator as $file) { |
| 130 | + if ($file instanceof SplFileInfo && $file->isFile()) { |
| 131 | + $zip->addFile($file->getPathname(), relativePath($root, $file->getPathname())); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +function relativePath(string $root, string $path): string { |
| 137 | + return str_replace('\\', '/', substr($path, strlen($root) + 1)); |
| 138 | +} |
0 commit comments