Skip to content

Commit

Permalink
make directory recursive (#829)
Browse files Browse the repository at this point in the history
* make directory recursive

Signed-off-by: michael <[email protected]>

* add utils test case

Signed-off-by: michael <[email protected]>

---------

Signed-off-by: michael <[email protected]>
  • Loading branch information
mikeah2011 authored Mar 26, 2024
1 parent 84e49b5 commit 7955c80
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Tools/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Knuckles\Scribe\Exceptions\CouldntFindFactory;
use Knuckles\Scribe\Exceptions\CouldntGetRouteDetails;
Expand Down Expand Up @@ -191,6 +192,11 @@ public static function copyDirectory(string $src, string $dest): void
}
}

public static function makeDirectoryRecursive(string $dir): void
{
File::isDirectory($dir) || File::makeDirectory($dir, 0777, true, true);
}

public static function deleteFilesMatching(string $dir, callable $condition): void
{
if (class_exists(LocalFilesystemAdapter::class)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Knuckles\Scribe\Writing;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
use Knuckles\Scribe\Tools\DocumentationConfig;
use Knuckles\Scribe\Tools\Globals;
Expand Down Expand Up @@ -96,6 +95,7 @@ protected function writeOpenAPISpec(array $parsedRoutes): void

$spec = $this->generateOpenAPISpec($parsedRoutes);
if ($this->isStatic) {
Utils::makeDirectoryRecursive($this->staticTypeOutputPath);
$specPath = "{$this->staticTypeOutputPath}/openapi.yaml";
file_put_contents($specPath, $spec);
} else {
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Knuckles\Scribe\Tests\Unit;

use Knuckles\Scribe\Tests\BaseLaravelTest;
use Knuckles\Scribe\Tools\Utils;

class UtilsTest extends BaseLaravelTest
{
/** @test */
public function make_directory_recursive()
{
$dir = __DIR__ . '/test_dir';
Utils::makeDirectoryRecursive($dir);
$this->assertDirectoryExists($dir); // Directory exists

if (rmdir($dir)) { // Remove the directory
dump("Directory deleted successfully: $dir");
} else { // If deletion fails, you can handle the error as needed
dump("Failed to delete directory: $dir");
}
}
}

0 comments on commit 7955c80

Please sign in to comment.