Browse Source

Implement sorting of groups

shalvah 3 years ago
parent
commit
573f2ba74a
2 changed files with 28 additions and 9 deletions
  1. 16 4
      camel/Camel.php
  2. 12 5
      src/Commands/GenerateDocumentation.php

+ 16 - 4
camel/Camel.php

@@ -15,6 +15,12 @@ use Symfony\Component\Yaml\Yaml;
 
 class Camel
 {
+    /**
+     * Mapping of group names to their generated file names. Helps us respect user reordering.
+     * @var array<string, string>
+     */
+    public static array $groupFileNames = [];
+
     /**
      * Load endpoints from the Camel files into groups (arrays).
      *
@@ -41,18 +47,18 @@ class Camel
      *
      * @return array[]
      */
-    public static function loadEndpointsToFlatPrimitivesArray(string $folder): array
+    public static function loadEndpointsToFlatPrimitivesArray(string $folder, bool $isFromCache = false): array
     {
         $endpoints = [];
         self::loadEndpointsFromCamelFiles($folder, function ($group) use (&$endpoints) {
             foreach ($group['endpoints'] as $endpoint) {
                 $endpoints[] = $endpoint;
             }
-        });
+        }, !$isFromCache);
         return $endpoints;
     }
 
-    public static function loadEndpointsFromCamelFiles(string $folder, callable $callback)
+    public static function loadEndpointsFromCamelFiles(string $folder, callable $callback, bool $storeGroupFilePaths = true)
     {
         $adapter = new Local(getcwd());
         $fs = new Filesystem($adapter);
@@ -65,6 +71,10 @@ class Camel
                 && !Str::startsWith($object['basename'], 'custom.')
             ) {
                 $group = Yaml::parseFile($object['path']);
+                if ($storeGroupFilePaths) {
+                    $filePathParts = explode('/', $object['path']);
+                    self::$groupFileNames[$group['name']] = end($filePathParts);
+                }
                 $callback($group);
             }
         }
@@ -122,14 +132,16 @@ class Camel
 
     public static function prepareGroupedEndpointsForOutput(array $groupedEndpoints): array
     {
-        return array_map(function (array $group) {
+        $groups =  array_map(function (array $group) {
             return [
                 'name' => $group['name'],
                 'description' => $group['description'],
+                'fileName' => self::$groupFileNames[$group['name']] ?? null,
                 'endpoints' => array_map(function (array $endpoint) {
                     return OutputEndpointData::fromExtractedEndpointArray($endpoint);
                 }, $group['endpoints']),
             ];
         }, $groupedEndpoints);
+        return Arr::sort($groups, 'fileName');
     }
 }

+ 12 - 5
src/Commands/GenerateDocumentation.php

@@ -247,15 +247,22 @@ class GenerateDocumentation extends Command
             mkdir(static::$cacheDir, 0777, true);
         }
 
-        $i = 0;
+        $fileNameIndex = 0;
         foreach ($grouped as $group) {
             $yaml = Yaml::dump(
                 $group, 10, 2,
                 Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK
             );
-            file_put_contents(static::$camelDir . "/$i.yaml", $yaml);
-            file_put_contents(static::$cacheDir . "/$i.yaml", "## Autogenerated by Scribe. DO NOT MODIFY.\n\n" . $yaml);
-            $i++;
+            if (count(Camel::$groupFileNames) == count($grouped)
+                && isset(Camel::$groupFileNames[$group['name']])) {
+                $fileName = Camel::$groupFileNames[$group['name']];
+            } else {
+                $fileName = "$fileNameIndex.yaml";
+                $fileNameIndex++;
+            }
+
+            file_put_contents(static::$camelDir . "/$fileName", $yaml);
+            file_put_contents(static::$cacheDir . "/$fileName", "## Autogenerated by Scribe. DO NOT MODIFY.\n\n" . $yaml);
         }
     }
 
@@ -288,7 +295,7 @@ class GenerateDocumentation extends Command
 
         if ($preserveUserChanges && is_dir(static::$camelDir) && is_dir(static::$cacheDir)) {
             $latestEndpointsData = Camel::loadEndpointsToFlatPrimitivesArray(static::$camelDir);
-            $cachedEndpoints = Camel::loadEndpointsToFlatPrimitivesArray(static::$cacheDir);
+            $cachedEndpoints = Camel::loadEndpointsToFlatPrimitivesArray(static::$cacheDir, true);
         }
 
         $routes = $routeMatcher->getRoutes($this->docConfig->get('routes'), $this->docConfig->get('router'));