From 7ed07c0189ef9cc3b48dc654e8202f955f05f7c9 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Fri, 3 Jul 2020 11:39:08 +0200
Subject: [PATCH] Provide both commands separately and trigger post package
 install script from composer.json

---
 composer.json                                 |  5 ++++
 src/CommandProvider.php                       |  3 ++-
 src/Handler.php                               |  4 +--
 src/Plugin.php                                | 12 ++++-----
 src/PrepareComposerCommand.php                | 27 +++++++++++++++++++
 ...sCommand.php => PrepareProjectCommand.php} |  6 ++---
 6 files changed, 44 insertions(+), 13 deletions(-)
 create mode 100644 src/PrepareComposerCommand.php
 rename src/{DrupalSpoonsCommand.php => PrepareProjectCommand.php} (73%)

diff --git a/composer.json b/composer.json
index a5e2797..7d7e72e 100644
--- a/composer.json
+++ b/composer.json
@@ -30,5 +30,10 @@
     },
     "extra": {
         "class": "LakeDrops\\DrupalSpoons\\Plugin"
+    },
+    "scripts": {
+        "post-package-install": [
+            "LakeDrops\\DrupalSpoons\\Plugin::postPackageInstall"
+        ]
     }
 }
diff --git a/src/CommandProvider.php b/src/CommandProvider.php
index bb8fda8..a6d42b4 100644
--- a/src/CommandProvider.php
+++ b/src/CommandProvider.php
@@ -14,7 +14,8 @@ class CommandProvider implements CommandProviderCapability {
    */
   public function getCommands(): array {
     return [
-      new DrupalSpoonsCommand(),
+      new PrepareComposerCommand(),
+      new PrepareProjectCommand(),
     ];
   }
 }
diff --git a/src/Handler.php b/src/Handler.php
index 06a6f2a..e49c1e9 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -50,7 +50,7 @@ class Handler {
     $projectRoot = getcwd();
 
     // Append DrupalSpoon related components to composer.json.
-    $jsonFile = new JsonFile($projectRoot . '/composer.json.disabled');
+    $jsonFile = new JsonFile($projectRoot . '/composer.disabled.json');
     $content = [];
     if ($jsonFile->exists()) {
       $content = $jsonFile->read();
@@ -58,7 +58,7 @@ class Handler {
     $content = array_merge_recursive($content, $options);
     $jsonFile->write($content);
     $fs->remove($projectRoot . '/composer.json');
-    $fs->rename($projectRoot . '/composer.json.disabled', $projectRoot . '/composer.json');
+    $fs->rename($projectRoot . '/composer.disabled.json', $projectRoot . '/composer.json');
   }
 
   /**
diff --git a/src/Plugin.php b/src/Plugin.php
index 24979bf..32fd108 100644
--- a/src/Plugin.php
+++ b/src/Plugin.php
@@ -4,6 +4,7 @@ namespace LakeDrops\DrupalSpoons;
 
 use Composer\Composer;
 use Composer\EventDispatcher\EventSubscriberInterface;
+use Composer\Installer\PackageEvent;
 use Composer\IO\IOInterface;
 use Composer\Plugin\Capable;
 use Composer\Plugin\PluginInterface;
@@ -43,19 +44,16 @@ class Plugin implements PluginInterface, EventSubscriberInterface, Capable {
    */
   public static function getSubscribedEvents(): array {
     return [
-      ScriptEvents::POST_INSTALL_CMD => 'configureComposerJson',
       ScriptEvents::POST_UPDATE_CMD => 'configureProject',
     ];
   }
 
   /**
-   * Post install command event callback.
-   *
-   * @param \Composer\Script\Event $event
-   *   The event that triggered the plugin.
+   * @param \Composer\Installer\PackageEvent $event
    */
-  public function configureComposerJson(Event $event) {
-    $this->handler->configureComposerJson();
+  public static function postPackageInstall(PackageEvent $event) {
+    $handler = new Handler($event->getComposer(), $event->getIO());
+    $handler->configureComposerJson();
   }
 
   /**
diff --git a/src/PrepareComposerCommand.php b/src/PrepareComposerCommand.php
new file mode 100644
index 0000000..c8594ae
--- /dev/null
+++ b/src/PrepareComposerCommand.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace LakeDrops\DrupalSpoons;
+
+use Composer\Command\BaseCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class PrepareComposerCommand extends BaseCommand {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function configure() {
+    $this->setName('drupalspoons:prepare');
+    $this->setDescription('Prepare composer.json for DrupalSpoons.');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function execute(InputInterface $input, OutputInterface $output) {
+    $handler = new Handler($this->getComposer(), $this->getIO());
+    $handler->configureComposerJson();
+  }
+
+}
diff --git a/src/DrupalSpoonsCommand.php b/src/PrepareProjectCommand.php
similarity index 73%
rename from src/DrupalSpoonsCommand.php
rename to src/PrepareProjectCommand.php
index 0d54b68..222e152 100644
--- a/src/DrupalSpoonsCommand.php
+++ b/src/PrepareProjectCommand.php
@@ -6,14 +6,14 @@ use Composer\Command\BaseCommand;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 
-class DrupalSpoonsCommand extends BaseCommand {
+class PrepareProjectCommand extends BaseCommand {
 
   /**
    * {@inheritdoc}
    */
   protected function configure() {
-    $this->setName('lakedrops:drupalspoons');
-    $this->setDescription('Prepare Drupal project for CI and/or local tests.');
+    $this->setName('drupalspoons:configure');
+    $this->setDescription('Configure project for DrupalSpoons.');
   }
 
   /**
-- 
GitLab