diff --git a/src/Plugin.php b/src/Plugin.php index c672279..620b468 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -8,23 +8,20 @@ use Composer\Composer; use Composer\IO\IOInterface; +use Composer\Plugin\Capability\CommandProvider; +use Composer\Plugin\Capable; use Composer\Plugin\PluginInterface; +use craft\composer\commands\Provider; /** * Plugin is the Composer plugin that registers the Craft CMS composer installer. * * @author Pixel & Tonic, Inc. */ -class Plugin implements PluginInterface +class Plugin implements PluginInterface, Capable { - /** - * @var Installer - */ - private $installer; - - /** - * @inheritdoc - */ + private Installer $installer; + public function activate(Composer $composer, IOInterface $io) { // Register the plugin installer @@ -37,18 +34,23 @@ public function activate(Composer $composer, IOInterface $io) } } - /** - * @inheritdoc - */ public function deactivate(Composer $composer, IOInterface $io) { $composer->getInstallationManager()->removeInstaller($this->installer); } - /** - * @inheritdoc - */ public function uninstall(Composer $composer, IOInterface $io) + {} + + public function getCapabilities() + { + return [ + CommandProvider::class => Provider::class, + ]; + } + + public function getInstaller(): Installer { + return $this->installer; } } diff --git a/src/commands/Provider.php b/src/commands/Provider.php new file mode 100644 index 0000000..1553d85 --- /dev/null +++ b/src/commands/Provider.php @@ -0,0 +1,24 @@ +io = $arguments['io']; + $this->plugin = $arguments['plugin']; + } + + public function getCommands() + { + return [new Regenerate($this->plugin)]; + } +} diff --git a/src/commands/Regenerate.php b/src/commands/Regenerate.php new file mode 100644 index 0000000..8f98916 --- /dev/null +++ b/src/commands/Regenerate.php @@ -0,0 +1,57 @@ +setName('craft-plugin-regenerate'); + $this->setDescription(sprintf('Rebuild the %s file from the installed packages.', Installer::PLUGINS_FILE)); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $output->writeln(sprintf('Regenerating %s...', Installer::PLUGINS_FILE)); + + /** @var PackageInterface[] $pluginPackages */ + $pluginPackages = array_map(function ($name) { + return $this->getComposer() + ->getRepositoryManager() + ->getLocalRepository() + ->findPackage($name, new MatchAllConstraint); + }, InstalledVersions::getInstalledPackagesByType('craft-plugin')); + + if (empty($pluginPackages)) { + $output->writeln('No plugins are installed.'); + + return 0; + } + + $output->writeln(sprintf('Found %d plugin package(s):', count($pluginPackages))); + + foreach ($pluginPackages as $package) { + $output->writeln(sprintf(' - Re-adding %s', $package->getName())); + $this->plugin->getInstaller()->addPlugin($package); + } + + $output->writeln('Done!'); + + return 0; + } +}