Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Dec 12, 2023
1 parent 462030b commit 53e57ac
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 62 deletions.
8 changes: 4 additions & 4 deletions src/Commands/GenerateDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class GenerateDocumentation extends Command
{--force : Discard any changes you've made to the YAML or Markdown files}
{--no-extraction : Skip extraction of route and API info and just transform the YAML and Markdown files into HTML}
{--no-upgrade-check : Skip checking for config file upgrades. Won't make things faster, but can be helpful if the command is buggy}
{--config=scribe : choose which config file to use}
{--cache-dir= : choose which cache directory to use}
{--config=scribe : Choose which config file to use}
{--scribe-dir= : Specify the directory where Scribe stores its intermediate output and cache. Defaults to `.<config_file>`}
";

protected $description = 'Generate API documentation from your Laravel/Dingo routes.';
Expand Down Expand Up @@ -106,9 +106,9 @@ public function bootstrap(): void
}

$this->paths = new PathConfig(configName: $configName);
if ($this->hasOption('cache-dir') && !empty($this->option('cache-dir'))) {
if ($this->hasOption('scribe-dir') && !empty($this->option('scribe-dir'))) {
$this->paths = new PathConfig(
configName: $configName, cacheDir: $this->option('cache-dir')
configName: $configName, scribeDir: $this->option('scribe-dir')
);
}

Expand Down
12 changes: 7 additions & 5 deletions src/Configuration/PathConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class PathConfig
{
public function __construct(
public string $configName = 'scribe',
protected ?string $cacheDir = null
// FOr lack of a better name, we'll call this `scribeDir`.
// It's sort of the cache dir, where Scribe stores its intermediate outputs.
protected ?string $scribeDir = null
)
{
if (is_null($this->cacheDir)) {
$this->cacheDir = ".{$this->configName}";
if (is_null($this->scribeDir)) {
$this->scribeDir = ".{$this->configName}";
}
}

Expand All @@ -37,9 +39,9 @@ public function configFileName(): string
public function intermediateOutputPath(string $resolvePath = null, string $separator = '/'): string
{
if (is_null($resolvePath)) {
return $this->cacheDir;
return $this->scribeDir;
}

return "{$this->cacheDir}{$separator}{$resolvePath}";
return "{$this->scribeDir}{$separator}{$resolvePath}";
}
}
76 changes: 24 additions & 52 deletions tests/GenerateDocumentation/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,81 +140,53 @@ public function generates_laravel_type_output()
/** @test */
public function supports_multi_docs_in_laravel_type_output()
{
$this->supports_base_test(["--config" => "scribe_admin"], '.scribe_admin');
$this->generate_with_paths(configName: "scribe_admin");
}

/** @test */
public function supports_separate_hidden_cache_directory()
public function supports_custom_scribe_directory()
{
$this->supports_base_test([
"--config" => "scribe_admin",
"--cache-dir" => ".scribe_admin_dir"
], '.scribe_admin_dir');
$this->generate_with_paths(configName: "scribe_admin", intermediateOutputDirectory: '5.5/Apple/26');
}

/** @test */
public function supports_separate_non_hidden_cache_directory()
{
$this->supports_base_test([
"--config" => "scribe_admin",
"--cache-dir" => "scribe_admin_dir"
], 'scribe_admin_dir');
}

/** @test */
public function supports_unrelated_temp_directory()
{
$this->supports_base_test([
"--config" => "bananas_are_good",
"--cache-dir" => "5.5/Apple/26"
], '5.5/Apple/26');
}

/**
* Base test for
* - supports_multi_docs_in_laravel_type_output
* - supports_separate_hidden_cache_directory
* - supports_separate_non_hidden_cache_directory
* - supports_unrelated_temp_directory
*
* @param $generate
* @param $assertDir
* @return void
*/
private function supports_base_test($generate, $assertDir)
private function generate_with_paths($configName, $intermediateOutputDirectory = null)
{
RouteFacade::post('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
config([$generate['--config'] => config('scribe')]);
config([$configName => config('scribe')]);
$title = "The Real Admin API";
config([$generate['--config'] . '.title' => $title]);
config([$generate['--config'] . '.type' => 'laravel']);
config([$generate['--config'] . '.postman.enabled' => true]);
config([$generate['--config'] . '.openapi.enabled' => true]);

$output = $this->generate($generate);
config(["{$configName}.title" => $title]);
config(["{$configName}.type" => 'laravel']);
config(["{$configName}.postman.enabled" => true]);
config(["{$configName}.openapi.enabled" => true]);

$pathOptions = ["--config" => $configName];
if ($intermediateOutputDirectory) {
$pathOptions["--scribe-dir"] = $intermediateOutputDirectory;
}
$output = $this->generate($pathOptions);
$this->assertStringContainsString(
"Wrote Blade docs to: vendor/orchestra/testbench-core/laravel/resources/views/" . $generate['--config'], $output
"Wrote Blade docs to: vendor/orchestra/testbench-core/laravel/resources/views/{$configName}", $output
);
$this->assertStringContainsString(
"Wrote Laravel assets to: vendor/orchestra/testbench-core/laravel/public/vendor/" . $generate['--config'], $output
"Wrote Laravel assets to: vendor/orchestra/testbench-core/laravel/public/vendor/{$configName}", $output
);
$this->assertStringContainsString(
"Wrote Postman collection to: vendor/orchestra/testbench-core/laravel/storage/app/" . $generate['--config'] . "/collection.json", $output
"Wrote Postman collection to: vendor/orchestra/testbench-core/laravel/storage/app/{$configName}/collection.json", $output
);
$this->assertStringContainsString(
"Wrote OpenAPI specification to: vendor/orchestra/testbench-core/laravel/storage/app/" . $generate['--config'] . "/openapi.yaml", $output
"Wrote OpenAPI specification to: vendor/orchestra/testbench-core/laravel/storage/app/{$configName}/openapi.yaml", $output
);

$paths = collect([
Storage::disk('local')->path( $generate['--config'] . '/collection.json'),
Storage::disk('local')->path($generate['--config'] . '/openapi.yaml'),
View::getFinder()->find($generate['--config'] . '/index'),
Storage::disk('local')->path("{$configName}/collection.json"),
Storage::disk('local')->path("{$configName}/openapi.yaml"),
View::getFinder()->find("{$configName}/index"),
]);
$paths->each(fn($path) => $this->assertFileContainsString($path, $title));
$paths->each(fn($path) => unlink($path));

$this->assertDirectoryExists($assertDir);
Utils::deleteDirectoryAndContents($assertDir);
$this->assertDirectoryExists($intermediateOutputDirectory ?: ".{$configName}");
Utils::deleteDirectoryAndContents($intermediateOutputDirectory ?: ".{$configName}");
}

/** @test */
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/PathConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function resolves_cache_path_with_subdirectories()
/** @test */
public function supports_custom_cache_path()
{
$pathConfig = new PathConfig('scribe/bob', cacheDir: 'scribe_cache');
$pathConfig = new PathConfig('scribe/bob', scribeDir: 'scribe_cache');
$this->assertEquals('scribe_cache', $pathConfig->intermediateOutputPath());
$this->assertEquals('scribe_cache/tim', $pathConfig->intermediateOutputPath('tim'));
$this->assertEquals('scribe/bob', $pathConfig->outputPath());
Expand Down

0 comments on commit 53e57ac

Please sign in to comment.