From ce5b8b8fa9fc443f743119aadf4d73b6053d3aac Mon Sep 17 00:00:00 2001 From: shalvah Date: Fri, 15 May 2020 17:54:30 +0100 Subject: [PATCH] Add ability to specify static output path --- config/scribe.php | 11 +++++++++++ docs/config.md | 5 +++++ src/Writing/Writer.php | 13 ++++++++----- tests/GenerateDocumentationTest.php | 13 +++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/config/scribe.php b/config/scribe.php index 508a624f..ca0cc386 100644 --- a/config/scribe.php +++ b/config/scribe.php @@ -8,6 +8,17 @@ */ '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. */ diff --git a/docs/config.md b/docs/config.md index 5fa27fef..57e90b75 100644 --- a/docs/config.md +++ b/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. diff --git a/src/Writing/Writer.php b/src/Writing/Writer.php index 03b4c040..fbab3579 100644 --- a/src/Writing/Writer.php +++ b/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 @@ protected function performFinalTasksForLaravelType(): void 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 @@ public function writeHtmlDocs(): void { 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(); diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index a82a6775..ec96fa24 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -393,6 +393,19 @@ public function sorts_group_naturally() $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() {