diff --git a/composer.json b/composer.json index beed1b38c1793a6919b9f7539721ded71f7d7cb6..1d834c3463e6054a3927cc6b5e10689ab480e49e 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ }, "require": { "composer-plugin-api": "^1.0.0", - "lakedrops/composer-json-utils": "^1.0.0" + "lakedrops/composer-json-utils": "^1.3.0" }, "require-dev": { "composer/composer": "^1.5.0", @@ -47,12 +47,6 @@ "extra": { "class": "LakeDrops\\Ahoy\\Plugin", "lakedrops": { - "scripts": { - "ahoy": { - "callback": "LakeDrops\\Ahoy\\Plugin::scripts", - "description": "Scan LakeDrops plugins for ahoy scripts." - } - }, "ahoy": { "me": { "usage": "Ahoy plugin commands", diff --git a/src/CommandProvider.php b/src/CommandProvider.php new file mode 100644 index 0000000000000000000000000000000000000000..3c5309d0e94e5466a947e50d136e04181f0bb4dd --- /dev/null +++ b/src/CommandProvider.php @@ -0,0 +1,20 @@ +<?php + +namespace LakeDrops\Ahoy; + +use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability; +use Composer\Command\BaseCommand; + +class CommandProvider implements CommandProviderCapability { + + /** + * Retrieves an array of commands + * + * @return BaseCommand[] + */ + public function getCommands(): array { + return [ + new UpdateCommand(), + ]; + } +} diff --git a/src/Handler.php b/src/Handler.php index c590a8d7da823740b99de12594a54415dcb0a2da..b00ca9da2bf4eaa260650159bd7f1f7d1271e5a2 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -2,9 +2,7 @@ namespace LakeDrops\Ahoy; -use Composer\Composer; -use Composer\IO\IOInterface; -use Composer\Script\Event; +use LakeDrops\Component\Composer\BaseHandler; use LakeDrops\Component\Composer\Utils; use Symfony\Component\Yaml\Yaml; @@ -13,58 +11,15 @@ use Symfony\Component\Yaml\Yaml; * * @package LakeDrops\Drupal8Scaffold */ -class Handler { - - /** - * The composer object running this session. - * - * @var \Composer\Composer - */ - protected $composer; - - /** - * The input-output object for the composer session. - * - * @var \Composer\IO\IOInterface - */ - protected $io; - - /** - * Handler constructor. - * - * @param \Composer\Composer $composer - * The composer object. - * @param \Composer\IO\IOInterface $io - * The input-output object. - */ - public function __construct(Composer $composer, IOInterface $io) { - $this->composer = $composer; - $this->io = $io; - } - - /** - * Retrieve a package from the current composer process. - * - * @param string $name - * Name of the package to get from the current composer installation. - * - * @return \Composer\Package\PackageInterface - * The package. - */ - protected function getPackage($name) { - return $this->composer->getRepositoryManager()->getLocalRepository()->findPackage($name, '*'); - } +class Handler extends BaseHandler { /** * Update ahoy scripts of all LakeDrops plugins. - * - * @param \Composer\Script\Event $event - * The event that triggered the plugin. */ - public function updateScripts(Event $event) { + public function updateScripts() { // We only do the fancy stuff for developers. - if (!$event->isDevMode()) { + if (!$this->isDevMode()) { return; } @@ -94,14 +49,4 @@ class Handler { $this->git('ignore ' . '.ahoy.yml'); } - /** - * Wrapper for git command in the root directory. - * - * @param string $command - * Git command name, arguments and/or options. - */ - protected function git($command) { - passthru(escapeshellcmd('git -c "user.email=d8-project@lakedrops.com" ' . $command)); - } - } diff --git a/src/Plugin.php b/src/Plugin.php index 80f71b4c34155144fee41d8dd9b67d5fe082565c..3b1c1fb20c91f4d8277025723f3e6c2f6e1e9e61 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -2,30 +2,29 @@ namespace LakeDrops\Ahoy; -use Composer\Composer; -use Composer\EventDispatcher\EventSubscriberInterface; -use Composer\IO\IOInterface; -use Composer\Plugin\PluginInterface; use Composer\Script\Event; use Composer\Script\ScriptEvents; +use LakeDrops\Component\Composer\BasePlugin; /** * Composer plugin for handling Ahoy scripts. */ -class Plugin implements PluginInterface, EventSubscriberInterface { +class Plugin extends BasePlugin { /** - * The handler object for events. - * - * @var \LakeDrops\ComposerScripts\Handler + * {@inheritdoc} */ - protected $handler; + public function getHandlerClass() { + return Handler::class; + } /** * {@inheritdoc} */ - public function activate(Composer $composer, IOInterface $io) { - $this->handler = new Handler($composer, $io); + public function getCapabilities(): array { + return array( + \Composer\Plugin\Capability\CommandProvider::class => CommandProvider::class, + ); } /** @@ -46,18 +45,11 @@ class Plugin implements PluginInterface, EventSubscriberInterface { * The event that triggered the plugin. */ public function updateScripts(Event $event) { - $this->handler->updateScripts($event); - } - - /** - * Callback to scan plugins for ahoy scripts. - * - * @param \Composer\Script\Event $event - * The event that triggered the plugin. - */ - public static function scripts(Event $event) { - $handler = new Handler($event->getComposer(), $event->getIO()); - $handler->updateScripts($event); + /** @var Handler $handler */ + $handler = $this->handler; + $handler + ->setEvent($event) + ->updateScripts(); } } diff --git a/src/UpdateCommand.php b/src/UpdateCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..8b763605913e3f3adbf4250bb6d631910c3d7e30 --- /dev/null +++ b/src/UpdateCommand.php @@ -0,0 +1,36 @@ +<?php + +namespace LakeDrops\Ahoy; + +use LakeDrops\Component\Composer\BaseCommand; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class UpdateCommand extends BaseCommand { + + /** + * {@inheritdoc} + */ + protected function configure() { + $this->setName('lakedrops:ahoy'); + $this->setDescription('Scan LakeDrops plugins for ahoy scripts.'); + } + + /** + * {@inheritdoc} + */ + public function getHandlerClass() { + return Handler::class; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) { + parent::execute($input, $output); + /** @var Handler $handler */ + $handler = $this->handler; + $handler->updateScripts(); + } + +}