|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +$binDir = __DIR__; |
| 6 | +$projectRoot = dirname($binDir); |
| 7 | + |
| 8 | +$linkPath = $binDir . DIRECTORY_SEPARATOR . 'console'; |
| 9 | + |
| 10 | +$targetRelativeFromBin = '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'console'; |
| 11 | + |
| 12 | +function fail(string $message, int $code = 1): void { |
| 13 | + fwrite(STDERR, "[create-console-symlink] {$message}\n"); |
| 14 | + exit($code); |
| 15 | +} |
| 16 | + |
| 17 | +function info(string $message): void { |
| 18 | + fwrite(STDOUT, "[create-console-symlink] {$message}\n"); |
| 19 | +} |
| 20 | + |
| 21 | +$targetAbsolute = $projectRoot . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'console'; |
| 22 | + |
| 23 | +if (is_link($linkPath) || file_exists($linkPath)) { |
| 24 | + if (!@unlink($linkPath)) { |
| 25 | + fail("Unable to remove existing bin/console (check permissions)."); |
| 26 | + } |
| 27 | + info("Removed existing: bin/console"); |
| 28 | +} |
| 29 | + |
| 30 | +$isWindows = (PHP_OS_FAMILY === 'Windows'); |
| 31 | + |
| 32 | +if ($isWindows) { |
| 33 | + $batPath = $linkPath . '.bat'; |
| 34 | + $bat = "@echo off\r\n" |
| 35 | + . "php \"%~dp0..\\vendor\\bin\\console\" %*\r\n"; |
| 36 | + |
| 37 | + if (file_put_contents($batPath, $bat) === false) { |
| 38 | + fail("Unable to write: bin/console.bat"); |
| 39 | + } |
| 40 | + info("Created launcher: bin/console.bat"); |
| 41 | + exit(0); |
| 42 | +} |
| 43 | + |
| 44 | +if (!file_exists($targetAbsolute)) { |
| 45 | + info("Warning: target not found yet: vendor/bin/console (will still create symlink)."); |
| 46 | +} |
| 47 | + |
| 48 | +if (!symlink($targetRelativeFromBin, $linkPath)) { |
| 49 | + fail("Unable to create symlink bin/console -> {$targetRelativeFromBin}"); |
| 50 | +} |
| 51 | + |
| 52 | +@chmod($linkPath, 0755); |
| 53 | +info("Created symlink: bin/console -> {$targetRelativeFromBin}"); |
0 commit comments