|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Flowpack\Prunner\Composer; |
| 5 | + |
| 6 | +use Composer\Script\Event; |
| 7 | +use Neos\Utility\Files; |
| 8 | +use PharData; |
| 9 | + |
| 10 | +class InstallerScripts |
| 11 | +{ |
| 12 | + |
| 13 | + private const PRUNNER_DISPATCH_SCRIPT = <<<'EOD' |
| 14 | +#!/bin/sh |
| 15 | +
|
| 16 | +SCRIPTS_DIR=$(dirname "$(realpath $0)") |
| 17 | +
|
| 18 | +OS_TYPE=$(uname -s) |
| 19 | +ARCH_TYPE=$(uname -m) |
| 20 | +
|
| 21 | +# Make a very simple check if we have a bundled binary for the right OS / architecture |
| 22 | +BIN_TARGET="${SCRIPTS_DIR}/${OS_TYPE}_${ARCH_TYPE}/prunner" |
| 23 | +
|
| 24 | +if [ -f "$BIN_TARGET" ]; then |
| 25 | + $BIN_TARGET $@ |
| 26 | +else |
| 27 | + echo "Unsupported OS or architecture" |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | +EOD; |
| 31 | + |
| 32 | + |
| 33 | + public static function postUpdateAndInstall(Event $event) |
| 34 | + { |
| 35 | + $platform = php_uname('s'); // stuff like Darwin etc |
| 36 | + $architecture = php_uname('m'); // x86_64 |
| 37 | + |
| 38 | + $version = '0.0.1'; |
| 39 | + |
| 40 | + $baseDirectory = 'prunner'; |
| 41 | + $platformSpecificTargetDirectory = $baseDirectory . '/' . $platform . '_' . $architecture; |
| 42 | + |
| 43 | + if (file_exists($platformSpecificTargetDirectory . '/version') && trim(file_get_contents($platformSpecificTargetDirectory . '/version')) !== $version) { |
| 44 | + echo '> Version of prunner inside ' . $platformSpecificTargetDirectory . ' is ' . trim(file_get_contents($platformSpecificTargetDirectory . '/version')) . ', but we expect ' . $version . ".\n"; |
| 45 | + echo '> Removing prunner, and re-downloading.' . "\n"; |
| 46 | + Files::removeDirectoryRecursively($platformSpecificTargetDirectory); |
| 47 | + } |
| 48 | + if (!file_exists($platformSpecificTargetDirectory . '/prunner')) { |
| 49 | + echo '> Downloading prunner from https://github.com/Flowpack/prunner' . "\n"; |
| 50 | + echo '> Version: ' . $version . "\n"; |
| 51 | + echo '> Platform: ' . $platform . "\n"; |
| 52 | + echo '> Architecture: ' . $architecture . "\n"; |
| 53 | + $downloadLink = sprintf('https://github.com/Flowpack/prunner/releases/download/v%s/prunner_%s_%s_%s.tar.gz', $version, $version, $platform, $architecture); |
| 54 | + $downloadedFileContents = file_get_contents($downloadLink); |
| 55 | + echo '> Download complete.' . "\n"; |
| 56 | + |
| 57 | + file_put_contents('Data/Temporary/prunner.tar.gz', $downloadedFileContents); |
| 58 | + unlink('Data/Temporary/prunner.tar'); |
| 59 | + |
| 60 | + // decompress from gz |
| 61 | + $p = new PharData('Data/Temporary/prunner.tar.gz'); |
| 62 | + $p->decompress(); |
| 63 | + $phar = new PharData('Data/Temporary/prunner.tar'); |
| 64 | + |
| 65 | + if (!is_dir(dirname($platformSpecificTargetDirectory))) { |
| 66 | + mkdir(dirname($platformSpecificTargetDirectory)); |
| 67 | + } |
| 68 | + |
| 69 | + $phar->extractTo($platformSpecificTargetDirectory); |
| 70 | + file_put_contents($platformSpecificTargetDirectory . '/version', $version); |
| 71 | + |
| 72 | + echo '> Prunner extracted to ' . $platformSpecificTargetDirectory . "\n"; |
| 73 | + } |
| 74 | + |
| 75 | + file_put_contents($baseDirectory . '/prunner', self::PRUNNER_DISPATCH_SCRIPT); |
| 76 | + chmod($baseDirectory . '/prunner', 0755); |
| 77 | + } |
| 78 | +} |
0 commit comments