Skip to content

Commit

Permalink
feat(documentator): Processor wildcard tasks support.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Jul 22, 2024
1 parent a320c33 commit 124891b
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/documentator/src/Processor/Contracts/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

interface Task {
/**
* Returns the file extensions which task is processing.
* Returns the file extensions which task is processing. The `*` can be used
* to process any file.
*
* @return non-empty-list<string>
*/
Expand Down
11 changes: 7 additions & 4 deletions packages/documentator/src/Processor/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\FileSystem;
use Throwable;

use function array_merge;
use function array_values;
use function microtime;
use function preg_match;
Expand Down Expand Up @@ -96,7 +97,7 @@ private function runFile(File $file): float {
}

// Process
$tasks = $this->tasks[$file->getExtension()] ?? [];
$tasks = array_merge($this->tasks[$file->getExtension()] ?? [], $this->tasks['*'] ?? []);
$start = microtime(true);
$paused = 0;
$this->stack[$path] = $file;
Expand Down Expand Up @@ -208,10 +209,12 @@ private function dispatch(Dependency|File $file, Result $result, float $duration

private function isSkipped(File $file): bool {
// Tasks?
$tasks = $this->tasks[$file->getExtension()] ?? [];
if (!isset($this->tasks['*'])) {
$tasks = $this->tasks[$file->getExtension()] ?? [];

if (!$tasks) {
return true;
if (!$tasks) {
return true;
}
}

// Outside?
Expand Down
4 changes: 3 additions & 1 deletion packages/documentator/src/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public function task(Task $task): static {
*/
public function run(string $path, array|string|null $exclude = null, ?Closure $listener = null): float {
$start = microtime(true);
$extensions = array_map(static fn ($e) => "*.{$e}", array_keys($this->tasks));
$extensions = !isset($this->tasks['*'])
? array_map(static fn ($e) => "*.{$e}", array_keys($this->tasks))
: null;
$exclude = array_map(Glob::toRegex(...), (array) $exclude);
$root = new Directory($path, true);
$fs = new FileSystem();
Expand Down
122 changes: 122 additions & 0 deletions packages/documentator/src/Processor/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,128 @@ static function (string $path, Result $result) use (&$count, &$events): void {
);
}

public function testRunWildcard(): void {
$taskA = new class() implements Task {
/**
* @var array<array-key, string>
*/
public array $processed = [];

/**
* @inheritDoc
*/
#[Override]
public function getExtensions(): array {
return ['html'];
}

/**
* @return Generator<mixed, Dependency<*>, mixed, bool>
*/
#[Override]
public function __invoke(Directory $root, File $file): Generator {
$dependencies = match ($file->getName()) {
'b.html' => [
'../../../../README.md',
'../a/excluded.txt',
],
default => [
// empty
],
};

foreach ($dependencies as $dependency) {
yield new FileReference($dependency);
}

$this->processed[] = $file->getRelativePath($root);

return true;
}
};
$taskB = new class() implements Task {
/**
* @var array<array-key, string>
*/
public array $processed = [];

/**
* @inheritDoc
*/
#[Override]
public function getExtensions(): array {
return ['*'];
}

#[Override]
public function __invoke(Directory $root, File $file): bool {
$this->processed[] = $file->getRelativePath($root);

return true;
}
};

$root = Path::normalize(self::getTestData()->path(''));
$count = 0;
$events = [];

(new Processor())
->task($taskA)
->task($taskB)
->run(
$root,
['excluded.txt', '**/**/excluded.txt'],
static function (string $path, Result $result) use (&$count, &$events): void {
$events[$path] = $result;
$count++;
},
);

self::assertEquals(
[
'b/a/ba.txt' => Result::Success,
'c.txt' => Result::Success,
'b/b/bb.txt' => Result::Success,
'a/a.txt' => Result::Success,
'a/a/aa.txt' => Result::Success,
'a/b/ab.txt' => Result::Success,
'b/b.txt' => Result::Success,
'c.htm' => Result::Success,
'c.html' => Result::Success,
'a/excluded.txt' => Result::Skipped,
'../../../README.md' => Result::Skipped,
'a/a.html' => Result::Success,
'b/b.html' => Result::Success,
],
$events,
);
self::assertCount($count, $events);
self::assertEquals(
[
'a/a.html',
'b/b.html',
'c.html',
],
$taskA->processed,
);
self::assertEquals(
[
'a/a.html',
'a/a.txt',
'a/a/aa.txt',
'a/b/ab.txt',
'b/a/ba.txt',
'b/b.html',
'b/b.txt',
'b/b/bb.txt',
'c.htm',
'c.html',
'c.txt',
],
$taskB->processed,
);
}

public function testRunFileNotFound(): void {
$task = new class() implements Task {
/**
Expand Down

0 comments on commit 124891b

Please sign in to comment.