From 8513b73650568005d774f99ad5d82f170ab00589 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Mon, 10 Dec 2018 19:59:57 +0100
Subject: [PATCH] Move scripts to command provider

---
 composer.json           |  8 +-----
 src/CommandProvider.php | 20 +++++++++++++
 src/Handler.php         | 63 +++--------------------------------------
 src/Plugin.php          | 38 ++++++++++---------------
 src/UpdateCommand.php   | 36 +++++++++++++++++++++++
 5 files changed, 76 insertions(+), 89 deletions(-)
 create mode 100644 src/CommandProvider.php
 create mode 100644 src/UpdateCommand.php

diff --git a/composer.json b/composer.json
index beed1b3..1d834c3 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 0000000..3c5309d
--- /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 c590a8d..b00ca9d 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 80f71b4..3b1c1fb 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 0000000..8b76360
--- /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();
+  }
+
+}
-- 
GitLab