From b2044ff3124a03b0ec8ac47b164ccfb2bacd8d35 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sun, 4 Feb 2024 13:56:09 +0100 Subject: [PATCH] fix(em): breaks when composer deps get updated --- .../src/Command/GlobalUpdateHandler.php | 3 +- .../src/Command/MajorUpdateHandler.php | 2 +- .../src/Command/MinorUpdateHandler.php | 3 +- .../src/Command/RemoveExtensionHandler.php | 3 +- .../src/Command/RequireExtensionHandler.php | 3 +- .../src/Command/UpdateExtensionHandler.php | 3 +- .../src/Composer/ComposerAdapter.php | 33 +++++++++++++++++-- .../src/ExtensionManagerServiceProvider.php | 2 ++ 8 files changed, 44 insertions(+), 8 deletions(-) diff --git a/extensions/package-manager/src/Command/GlobalUpdateHandler.php b/extensions/package-manager/src/Command/GlobalUpdateHandler.php index dfc527d224..0de12ad107 100644 --- a/extensions/package-manager/src/Command/GlobalUpdateHandler.php +++ b/extensions/package-manager/src/Command/GlobalUpdateHandler.php @@ -64,7 +64,8 @@ public function handle(GlobalUpdate $command) $output = $this->composer->run( new ArrayInput($input), - $command->task ?? null + $command->task ?? null, + true ); if ($output->getExitCode() !== 0) { diff --git a/extensions/package-manager/src/Command/MajorUpdateHandler.php b/extensions/package-manager/src/Command/MajorUpdateHandler.php index 4fa22039ae..fdf54354af 100644 --- a/extensions/package-manager/src/Command/MajorUpdateHandler.php +++ b/extensions/package-manager/src/Command/MajorUpdateHandler.php @@ -115,7 +115,7 @@ protected function runCommand(MajorUpdate $command, string $majorVersion): void $input['--dry-run'] = true; } - $output = $this->composer->run(new ArrayInput($input), $command->task ?? null); + $output = $this->composer->run(new ArrayInput($input), $command->task ?? null, true); if ($output->getExitCode() !== 0) { throw new MajorUpdateFailedException('*', $output->getContents(), $majorVersion); diff --git a/extensions/package-manager/src/Command/MinorUpdateHandler.php b/extensions/package-manager/src/Command/MinorUpdateHandler.php index 06bff34bd0..097f35b68e 100755 --- a/extensions/package-manager/src/Command/MinorUpdateHandler.php +++ b/extensions/package-manager/src/Command/MinorUpdateHandler.php @@ -60,7 +60,8 @@ public function handle(MinorUpdate $command) $output = $this->composer->run( new StringInput('update --prefer-dist --no-dev -a --with-all-dependencies'), - $command->task ?? null + $command->task ?? null, + true ); if ($output->getExitCode() !== 0) { diff --git a/extensions/package-manager/src/Command/RemoveExtensionHandler.php b/extensions/package-manager/src/Command/RemoveExtensionHandler.php index bc7f930c77..d45045b400 100755 --- a/extensions/package-manager/src/Command/RemoveExtensionHandler.php +++ b/extensions/package-manager/src/Command/RemoveExtensionHandler.php @@ -76,7 +76,8 @@ public function handle(RemoveExtension $command) $output = $this->composer->run( new StringInput("remove $extension->name"), - $command->task ?? null + $command->task ?? null, + true ); if ($output->getExitCode() !== 0) { diff --git a/extensions/package-manager/src/Command/RequireExtensionHandler.php b/extensions/package-manager/src/Command/RequireExtensionHandler.php index 6c0aacda78..227310417a 100755 --- a/extensions/package-manager/src/Command/RequireExtensionHandler.php +++ b/extensions/package-manager/src/Command/RequireExtensionHandler.php @@ -75,7 +75,8 @@ public function handle(RequireExtension $command) $output = $this->composer->run( new StringInput("require $packageName -W"), - $command->task ?? null + $command->task ?? null, + true ); if ($output->getExitCode() !== 0) { diff --git a/extensions/package-manager/src/Command/UpdateExtensionHandler.php b/extensions/package-manager/src/Command/UpdateExtensionHandler.php index ae11a5341b..61d5b8a8a1 100755 --- a/extensions/package-manager/src/Command/UpdateExtensionHandler.php +++ b/extensions/package-manager/src/Command/UpdateExtensionHandler.php @@ -92,7 +92,8 @@ public function handle(UpdateExtension $command) $output = $this->composer->run( new StringInput($input), - $command->task ?? null + $command->task ?? null, + true ); if ($output->getExitCode() !== 0) { diff --git a/extensions/package-manager/src/Composer/ComposerAdapter.php b/extensions/package-manager/src/Composer/ComposerAdapter.php index 4b307ef86e..fee4918f7a 100644 --- a/extensions/package-manager/src/Composer/ComposerAdapter.php +++ b/extensions/package-manager/src/Composer/ComposerAdapter.php @@ -15,6 +15,7 @@ use Flarum\ExtensionManager\OutputLogger; use Flarum\ExtensionManager\Support\Util; use Flarum\ExtensionManager\Task\Task; +use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\BufferedOutput; @@ -43,14 +44,20 @@ class ComposerAdapter */ private $output = null; - public function __construct(Application $application, OutputLogger $logger, Paths $paths) + /** + * @var Filesystem + */ + private $filesystem; + + public function __construct(Application $application, OutputLogger $logger, Paths $paths, Filesystem $filesystem) { $this->application = $application; $this->logger = $logger; $this->paths = $paths; + $this->filesystem = $filesystem; } - public function run(InputInterface $input, ?Task $task = null): ComposerOutput + public function run(InputInterface $input, ?Task $task = null, bool $safeMode = false): ComposerOutput { $this->application->resetComposer(); @@ -59,7 +66,29 @@ public function run(InputInterface $input, ?Task $task = null): ComposerOutput // This hack is necessary so that relative path repositories are resolved properly. $currDir = getcwd(); chdir($this->paths->base); + + if ($safeMode) { + $temporaryVendorDir = $this->paths->base . DIRECTORY_SEPARATOR . 'temp-vendor'; + if (! $this->filesystem->isDirectory($temporaryVendorDir)) { + $this->filesystem->makeDirectory($temporaryVendorDir); + } + Config::$defaultConfig['vendor-dir'] = $temporaryVendorDir; + } + $exitCode = $this->application->run($input, $this->output); + + if ($safeMode) { + // Move the temporary vendor directory to the real vendor directory. + if ($this->filesystem->isDirectory($temporaryVendorDir) && count($this->filesystem->allFiles($temporaryVendorDir))) { + $vendorDir = $this->paths->vendor; + if (file_exists($vendorDir)) { + $this->filesystem->deleteDirectory($vendorDir); + } + $this->filesystem->moveDirectory($temporaryVendorDir, $vendorDir); + } + Config::$defaultConfig['vendor-dir'] = $this->paths->vendor; + } + chdir($currDir); $command = Util::readableConsoleInput($input); diff --git a/extensions/package-manager/src/ExtensionManagerServiceProvider.php b/extensions/package-manager/src/ExtensionManagerServiceProvider.php index 87f2c5175e..6af99178c5 100755 --- a/extensions/package-manager/src/ExtensionManagerServiceProvider.php +++ b/extensions/package-manager/src/ExtensionManagerServiceProvider.php @@ -24,6 +24,7 @@ use Flarum\ExtensionManager\Listener\ReCheckForUpdates; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Filesystem\Filesystem; use Monolog\Formatter\LineFormatter; use Monolog\Handler\RotatingFileHandler; use Monolog\Logger; @@ -56,6 +57,7 @@ public function register() $composer, $container->make(OutputLogger::class), $container->make(Paths::class), + $container->make(Filesystem::class) ); });