From 5f3088fada1fd5f765f976e2d07ff75d7d9eb451 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen.haas@lakedrops.com>
Date: Sun, 27 Dec 2020 12:39:54 +0100
Subject: [PATCH] docker/l3d#58 Move project settings out of composer.json

---
 .ahoy.yml             |  6 ++++++
 composer.json         | 12 ++----------
 src/Handler.php       | 38 ++++++++++++++++++++++++--------------
 src/Plugin.php        |  4 ++--
 src/UpdateCommand.php |  7 ++++---
 5 files changed, 38 insertions(+), 29 deletions(-)
 create mode 100644 .ahoy.yml

diff --git a/.ahoy.yml b/.ahoy.yml
new file mode 100644
index 0000000..146b5b8
--- /dev/null
+++ b/.ahoy.yml
@@ -0,0 +1,6 @@
+ahoyapi: v2
+commands:
+  me:
+    cmd:
+      - ahoy.yml
+    usage: Ahoy plugin commands
diff --git a/composer.json b/composer.json
index 500a905..019caa9 100644
--- a/composer.json
+++ b/composer.json
@@ -32,7 +32,7 @@
     "require": {
         "php": ">=7.2",
         "composer-plugin-api": "^1||^2",
-        "lakedrops/composer-json-utils": "^1.4.0",
+        "lakedrops/composer-json-utils": "^1.7||dev-master",
         "symfony/yaml": "^3.4||^4.4||^5.0"
     },
     "require-dev": {
@@ -47,14 +47,6 @@
         }
     },
     "extra": {
-        "class": "LakeDrops\\Ahoy\\Plugin",
-        "lakedrops": {
-            "ahoy": {
-                "me": {
-                    "usage": "Ahoy plugin commands",
-                    "imports": ["ahoy.yml"]
-                }
-            }
-        }
+        "class": "LakeDrops\\Ahoy\\Plugin"
     }
 }
diff --git a/src/Handler.php b/src/Handler.php
index 08d05ee..80112d8 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -13,34 +13,44 @@ use Symfony\Component\Yaml\Yaml;
  */
 class Handler extends BaseHandler {
 
+  /**
+   * {@inheritdoc}
+   */
+  public function configId(): string {
+    return 'ahoy';
+  }
+
   /**
    * Update ahoy scripts of all LakeDrops plugins.
    */
   public function updateScripts() {
+    $this->init();
 
     // We only do the fancy stuff for developers.
     if (!$this->isDevMode()) {
       return;
     }
 
-    $ahoy = [
-      'ahoyapi' => 'v2',
-      'commands' => [],
-    ];
-
-    $settings = new Utils($this->composer);
-    foreach ($settings->getSubSubSection('extra', 'lakedrops', 'ahoy') as $command => $commands) {
-      $ahoy['commands'][$command] = $commands;
+    if (file_exists('.ahoy.yml')) {
+      $ahoy = Yaml::parseFile('.ahoy.yml');
+    }
+    else {
+      $ahoy = [
+        'ahoyapi' => 'v2',
+        'commands' => [],
+      ];
     }
 
     $installationManager = $this->composer->getInstallationManager();
-    foreach ($this->composer->getRepositoryManager()->getLocalRepository()->search('lakedrops/*') as $pkg) {
+    foreach ($this->composer->getRepositoryManager()->getLocalRepository()->search('*') as $pkg) {
       $pluginRoot = $installationManager->getInstallPath($this->getPackage($pkg['name']));
-      $pluginSettings = new Utils($this->composer, $pluginRoot);
-      foreach ($pluginSettings->getSubSubSection('extra', 'lakedrops', 'ahoy') as $command => $commands) {
-        $ahoy['commands'][$command] = $commands;
-        foreach ($commands['imports'] as $key => $import) {
-          $ahoy['commands'][$command]['imports'][$key] = './vendor/' . $pkg['name'] . '/' . $import;
+      if (file_exists($pluginRoot . '/.ahoy.yml')) {
+        $pluginAhoy = Yaml::parseFile($pluginRoot . '/.ahoy.yml');
+        foreach ($pluginAhoy['commands'] as $command => $commands) {
+          $ahoy['commands'][$command] = $commands;
+          foreach ($commands['imports'] as $key => $import) {
+            $ahoy['commands'][$command]['imports'][$key] = './vendor/' . $pkg['name'] . '/' . $import;
+          }
         }
       }
     }
diff --git a/src/Plugin.php b/src/Plugin.php
index 20d88cd..004bd21 100644
--- a/src/Plugin.php
+++ b/src/Plugin.php
@@ -14,7 +14,7 @@ class Plugin extends BasePlugin {
   /**
    * {@inheritdoc}
    */
-  public function getHandlerClass() {
+  public function getHandlerClass(): string {
     return Handler::class;
   }
 
@@ -44,7 +44,7 @@ class Plugin extends BasePlugin {
    * @param \Composer\Script\Event $event
    *   The event that triggered the plugin.
    */
-  public function updateScripts(Event $event) {
+  public function updateScripts(Event $event): void {
     /** @var Handler $handler */
     $handler = $this->handler;
     $handler
diff --git a/src/UpdateCommand.php b/src/UpdateCommand.php
index dfcb1c4..cf1b07c 100644
--- a/src/UpdateCommand.php
+++ b/src/UpdateCommand.php
@@ -11,7 +11,7 @@ class UpdateCommand extends BaseCommand {
   /**
    * {@inheritdoc}
    */
-  protected function configure() {
+  protected function configure(): void {
     parent::configure();
     $this
       ->setName('lakedrops:ahoy')
@@ -21,18 +21,19 @@ class UpdateCommand extends BaseCommand {
   /**
    * {@inheritdoc}
    */
-  public function getHandlerClass() {
+  public function getHandlerClass(): string {
     return Handler::class;
   }
 
   /**
    * {@inheritdoc}
    */
-  protected function execute(InputInterface $input, OutputInterface $output) {
+  protected function execute(InputInterface $input, OutputInterface $output): int {
     parent::execute($input, $output);
     /** @var Handler $handler */
     $handler = $this->handler;
     $handler->updateScripts();
+    return 0;
   }
 
 }
-- 
GitLab