Parcourir la source

Add ability to specify static output path

shalvah il y a 5 ans
Parent
commit
ce5b8b8fa9
4 fichiers modifiés avec 37 ajouts et 5 suppressions
  1. 11 0
      config/scribe.php
  2. 5 0
      docs/config.md
  3. 8 5
      src/Writing/Writer.php
  4. 13 0
      tests/GenerateDocumentationTest.php

+ 11 - 0
config/scribe.php

@@ -8,6 +8,17 @@ return [
      */
     'type' => 'static',
 
+    /*
+     * Settings for `static` type output.
+     */
+    'static' => [
+        /*
+         * HTML documentation, assets and Postman collection will be generated to this folder.
+         * Source Markdown will still be in resources/docs.
+         */
+        'output_path' => 'public/docs',
+    ],
+
     /*
      * Settings for `laravel` type output.
      */

+ 5 - 0
docs/config.md

@@ -15,6 +15,11 @@ This is the type of documentation output to generate.
 .. Note:: In both instances, the source markdown file will be generated in `resources/docs`.
 ```
 
+### `static`
+Settings for the `static` type output.
+
+- `output_path`: Output folder. The HTML documentation, assets and Postman collection will be generated to this folder. Source Markdown will still be in resources/docs. Default: `public/docs`.
+
 ### `laravel`
 Settings for the `laravel` type output.
 

+ 8 - 5
src/Writing/Writer.php

@@ -65,15 +65,17 @@ class Writer
 
     public function __construct(DocumentationConfig $config = null, bool $shouldOverwrite = false)
     {
-        // If no config is injected, pull from global
+        // If no config is injected, pull from global. Makes testing easier.
         $this->config = $config ?: new DocumentationConfig(config('scribe'));
         $this->baseUrl = $this->config->get('base_url') ?? config('app.url');
         $this->shouldOverwrite = $shouldOverwrite;
         $this->shouldGeneratePostmanCollection = $this->config->get('postman.enabled', false);
         $this->pastel = new Pastel();
+
         $this->isStatic = $this->config->get('type') === 'static';
         $this->sourceOutputPath = 'resources/docs';
-        $this->outputPath = $this->isStatic ? 'public/docs' : 'resources/views/scribe';
+        $this->outputPath = $this->isStatic ? rtrim($this->config->get('static.output_path', 'public/docs'), '/') : 'resources/views/scribe';
+
         $this->fileModificationTimesFile = $this->sourceOutputPath . '/.filemtimes';
         $this->lastTimesWeModifiedTheseFiles = [];
     }
@@ -187,10 +189,11 @@ class Writer
             mkdir($this->outputPath);
         }
 
-        rename("public/docs/index.html", "$this->outputPath/index.blade.php");
+        rename("{$this->outputPath}/index.html", "$this->outputPath/index.blade.php");
         // Move assets from public/docs to public/vendor/scribe
+        // We need to do this delete first, otherwise move won't work if folder exists
         Utils::deleteDirectoryAndContents("public/vendor/scribe/", getcwd());
-        rename("public/docs/", "public/vendor/scribe/");
+        rename("{$this->outputPath}/", "public/vendor/scribe/");
 
         $contents = file_get_contents("$this->outputPath/index.blade.php");
 
@@ -206,7 +209,7 @@ class Writer
     {
         ConsoleOutputUtils::info('Generating API HTML code');
 
-        $this->pastel->generate($this->sourceOutputPath . '/index.md', 'public/docs');
+        $this->pastel->generate($this->sourceOutputPath . '/index.md', $this->outputPath);
 
         if (!$this->isStatic) {
             $this->performFinalTasksForLaravelType();

+ 13 - 0
tests/GenerateDocumentationTest.php

@@ -393,6 +393,19 @@ class GenerateDocumentationTest extends TestCase
         $this->assertFileExists(__DIR__ . '/../resources/docs/groups/10-group-10.md');
     }
 
+    /** @test */
+    public function can_customise_static_output_path()
+    {
+        RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
+
+        config(['scribe.routes.0.prefixes' => ['*']]);
+        config(['scribe.static.output_path' => 'static/docs']);
+        $this->artisan('scribe:generate');
+
+        $this->assertFileExists(realpath(__DIR__ . '/../static/docs/index.html'));
+        Utils::deleteDirectoryAndContents('/static/docs');
+    }
+
     /** @test */
     public function will_not_overwrite_manually_modified_markdown_files_unless_force_flag_is_set()
     {