|
4 | 4 |
|
5 | 5 | use Composer\Composer; |
6 | 6 | use Composer\EventDispatcher\EventSubscriberInterface; |
7 | | -use Composer\Installer\PackageEvents; |
8 | 7 | use Composer\IO\IOInterface; |
| 8 | +use Composer\Installer\PackageEvent; |
| 9 | +use Composer\Installer\PackageEvents; |
9 | 10 | use Composer\Plugin\PluginInterface; |
| 11 | +use Symfony\Component\Process\Process; |
| 12 | + |
| 13 | +use function copy; |
| 14 | +use function is_dir; |
| 15 | +use function is_file; |
| 16 | +use function mkdir; |
| 17 | +use function realpath; |
| 18 | +use function sprintf; |
| 19 | +use function strtoupper; |
| 20 | +use function substr; |
10 | 21 |
|
11 | 22 | class Plugin implements PluginInterface, EventSubscriberInterface |
12 | 23 | { |
| 24 | + private const PACKAGE_NAME = 'sass-embedded-php'; |
| 25 | + |
| 26 | + private string $packagePath; |
| 27 | + |
| 28 | + private string $binDir; |
| 29 | + |
13 | 30 | public function activate(Composer $composer, IOInterface $io): void |
14 | 31 | { |
15 | | - $io->write('This is the way.'); |
| 32 | + $config = $composer->getConfig(); |
| 33 | + |
| 34 | + $this->packagePath = realpath(__DIR__ . '/../'); |
| 35 | + $this->binDir = (string) $config->get('bin-dir'); |
16 | 36 | } |
17 | 37 |
|
18 | 38 | public function deactivate(Composer $composer, IOInterface $io): void |
19 | 39 | { |
20 | | - $io->write('May the Force be with you.'); |
21 | 40 | } |
22 | 41 |
|
23 | 42 | public function uninstall(Composer $composer, IOInterface $io): void |
24 | 43 | { |
25 | | - $io->write('I\'ll see you again. I promise ©️'); |
26 | 44 | } |
27 | 45 |
|
| 46 | + /* @uses onPostPackageUpdate */ |
28 | 47 | public static function getSubscribedEvents(): array |
29 | 48 | { |
30 | 49 | return [ |
31 | | - PackageEvents::POST_PACKAGE_INSTALL => 'onPostPackageInstall', |
| 50 | + PackageEvents::POST_PACKAGE_INSTALL => 'onPostPackageUpdate', |
| 51 | + PackageEvents::POST_PACKAGE_UPDATE => 'onPostPackageUpdate', |
32 | 52 | ]; |
33 | 53 | } |
34 | 54 |
|
35 | | - public function onPostPackageInstall(): void |
| 55 | + public function onPostPackageUpdate(PackageEvent $event): void |
| 56 | + { |
| 57 | + static $alreadyRun = false; |
| 58 | + |
| 59 | + if ($alreadyRun) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + $alreadyRun = true; |
| 64 | + |
| 65 | + $this->installNpm($event->getIO()); |
| 66 | + $this->copyBinary($event->getIO()); |
| 67 | + } |
| 68 | + |
| 69 | + private function installNpm(IOInterface $io): void |
36 | 70 | { |
37 | | - Installer::postInstall(); |
| 71 | + if ($this->packagePath === '') { |
| 72 | + $io->write(sprintf( |
| 73 | + '<warning>[%s]</warning> package directory not found, skipping npm install.', |
| 74 | + self::PACKAGE_NAME |
| 75 | + )); |
| 76 | + |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + $packageJson = $this->packagePath . '/package.json'; |
| 81 | + if (! is_file($packageJson)) { |
| 82 | + $io->write(sprintf( |
| 83 | + '<warning>[%s]</warning> package.json not found in %s, skipping npm install.', |
| 84 | + self::PACKAGE_NAME, |
| 85 | + $this->packagePath |
| 86 | + )); |
| 87 | + |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + $npmBin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'npm.cmd' : 'npm'; |
| 92 | + |
| 93 | + $checkNpm = new Process([$npmBin, '--version']); |
| 94 | + $checkNpm->run(); |
| 95 | + if (! $checkNpm->isSuccessful()) { |
| 96 | + $io->write(sprintf( |
| 97 | + '<warning>[%s]</warning> npm not found in PATH, skipping npm install.', |
| 98 | + self::PACKAGE_NAME |
| 99 | + )); |
| 100 | + |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + $nodeModules = $this->packagePath . '/node_modules/sass-embedded'; |
| 105 | + if (is_dir($nodeModules)) { |
| 106 | + $io->write(sprintf( |
| 107 | + '<info>[%s]</info> node_modules/sass-embedded already exists, skipping npm install.', |
| 108 | + self::PACKAGE_NAME |
| 109 | + )); |
| 110 | + |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + $process = new Process([$npmBin, 'install'], $this->packagePath); |
| 115 | + $process->setTimeout(null); |
| 116 | + $process->run(function ($type, $buffer): void { echo $buffer; }); |
| 117 | + |
| 118 | + if ($process->isSuccessful()) { |
| 119 | + $io->write(sprintf( |
| 120 | + '<info>[%s]</info> npm install completed successfully.', |
| 121 | + self::PACKAGE_NAME |
| 122 | + )); |
| 123 | + |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + $io->write(sprintf('<error>[%s]</error> npm install failed:', self::PACKAGE_NAME)); |
| 128 | + $io->write($process->getErrorOutput()); |
| 129 | + } |
| 130 | + |
| 131 | + private function copyBinary(IOInterface $io): void |
| 132 | + { |
| 133 | + $source = $this->packagePath . '/bin/bridge.js'; |
| 134 | + $target = $this->binDir . '/bridge.js'; |
| 135 | + |
| 136 | + if (! is_file($source)) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + if (! is_dir($this->binDir)) { |
| 141 | + @mkdir($this->binDir, 0777, true); |
| 142 | + } |
| 143 | + |
| 144 | + if (@copy($source, $target)) { |
| 145 | + $io->write(sprintf('<info>[%s]</info> Binary overwritten: %s', self::PACKAGE_NAME, $target)); |
| 146 | + } else { |
| 147 | + $io->writeError(sprintf('<error>[%s]</error> Failed to copy binary', self::PACKAGE_NAME)); |
| 148 | + } |
38 | 149 | } |
39 | 150 | } |
0 commit comments