Skip to content

Commit

Permalink
Add ability to specify static output path
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed May 15, 2020
1 parent a38018a commit ce5b8b8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
11 changes: 11 additions & 0 deletions config/scribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
5 changes: 5 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 8 additions & 5 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}
Expand Down Expand Up @@ -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");

Expand All @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions tests/GenerateDocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit ce5b8b8

Please sign in to comment.