From 6559506fe3cd0da1c7039d0213db50ef88b4ff57 Mon Sep 17 00:00:00 2001 From: Jack Wilkinson Date: Tue, 5 Sep 2023 14:50:18 +0100 Subject: [PATCH] Added support for installing and removing composer packages to plugin commands --- modules/system/console/PluginInstall.php | 70 ++++++++++++++++++++---- modules/system/console/PluginRemove.php | 12 ++++ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/modules/system/console/PluginInstall.php b/modules/system/console/PluginInstall.php index 113fe4026..791a02928 100644 --- a/modules/system/console/PluginInstall.php +++ b/modules/system/console/PluginInstall.php @@ -1,8 +1,11 @@ argument('plugin'); - $manager = UpdateManager::instance()->setNotesOutput($this->output); + $manager = null; - $pluginDetails = $manager->requestPluginDetails($pluginName); + if (strpos($pluginName, '/')) { + $code = $this->composerInstall($pluginName); + if (!$code) { + return; + } + } elseif (strpos($pluginName, '.')) { + $manager = UpdateManager::instance()->setNotesOutput($this->output); + $code = $this->winterInstall($pluginName, $manager); + } - $code = array_get($pluginDetails, 'code'); - $hash = array_get($pluginDetails, 'hash'); - - $this->output->writeln(sprintf('Downloading plugin: %s', $code)); - $manager->downloadPlugin($code, $hash, true); - - $this->output->writeln(sprintf('Unpacking plugin: %s', $code)); - $manager->extractPlugin($code, $hash); + if (!$manager) { + $manager = UpdateManager::instance()->setNotesOutput($this->output); + } /* * Make sure plugin is registered @@ -64,4 +70,46 @@ class PluginInstall extends Command $this->output->writeln(sprintf('Migrating plugin...', $code)); $manager->updatePlugin($code); } + + public function winterInstall(string $pluginName, UpdateManager $manager): string + { + $pluginDetails = $manager->requestPluginDetails($pluginName); + + $code = array_get($pluginDetails, 'code'); + $hash = array_get($pluginDetails, 'hash'); + + $this->output->writeln(sprintf('Downloading plugin: %s', $code)); + $manager->downloadPlugin($code, $hash, true); + + $this->output->writeln(sprintf('Unpacking plugin: %s', $code)); + $manager->extractPlugin($code, $hash); + + return $code; + } + + public function composerInstall(string $pluginName): ?string + { + try { + $result = Composer::search($pluginName, 'winter-plugin')->getResults(); + + if (count($result) > 1) { + throw new SystemException('More than 1 plugin returned via composer search'); + } + + if (count($result) === 0) { + throw new SystemException('Plugin could not be found'); + } + + Composer::require($pluginName); + } catch (\Throwable $e) { + $this->error($e->getMessage()); + return null; + } + + PluginManager::forgetInstance(); + UpdateManager::forgetInstance(); + VersionManager::forgetInstance(); + + return PluginManager::instance()->findByComposerPackage($pluginName)->getPluginIdentifier(); + } } diff --git a/modules/system/console/PluginRemove.php b/modules/system/console/PluginRemove.php index d442fb151..55cb5d03b 100644 --- a/modules/system/console/PluginRemove.php +++ b/modules/system/console/PluginRemove.php @@ -1,6 +1,7 @@ rollbackPlugin($pluginName); } + $plugin = $pluginManager->findByIdentifier($pluginName); + + /** + * Uninstall composer package + */ + if ($package = $plugin->getComposerPackageName()) { + $this->output->writeln(sprintf('Removing composer package: %s', $package)); + $this->output->write(Composer::remove($package) . PHP_EOL); + return 0; + } + /* * Delete from file system */