Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processor fixes/improvements #222

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions packages/documentator/src/Processor/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,23 @@ private function file(File $file): void {
return;
}

// Event
$pathname = $this->fs->getPathname($file);

$this->dispatcher->notify(new FileStarted($pathname));

// Circular?
if (isset($this->stack[$path])) {
$this->dispatcher->notify(new FileFinished(FileFinishedResult::Failed));

throw new DependencyCircularDependency($file, array_values($this->stack));
// The $file cannot be changed if it is placed outside the output
// directory, so we can return it safely in this case.
if ($this->fs->output->isInside($file->getPath())) {
$this->dispatcher->notify(new FileStarted($this->fs->getPathname($file)));
$this->dispatcher->notify(new FileFinished(FileFinishedResult::Failed));

throw new DependencyCircularDependency($file, array_values($this->stack));
} else {
return;
}
}

// Event
$this->dispatcher->notify(new FileStarted($this->fs->getPathname($file)));

// Skipped?
if ($this->isSkipped($file)) {
$this->dispatcher->notify(new FileFinished(FileFinishedResult::Skipped));
Expand Down
14 changes: 10 additions & 4 deletions packages/documentator/src/Processor/FileSystem/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ public function getPathname(Directory|DirectoryPath|File|FilePath $path): string
$suffix = $path instanceof Directory || $path instanceof DirectoryPath ? '/' : '';
$path = $path instanceof Entry ? $path->getPath() : $path;
$name = match (true) {
$this->input->isEqual($this->output) => Mark::Inout->value.' '.$this->output->getRelativePath($path).$suffix,
$this->input->isInside($path) => Mark::Input->value.' '.$this->input->getRelativePath($path).$suffix,
$this->output->isInside($path) => Mark::Output->value.' '.$this->output->getRelativePath($path).$suffix,
default => Mark::External->value.' '.$path.$suffix,
$this->input->isEqual($this->output),
=> Mark::Inout->value.' '.$this->output->getRelativePath($path).$suffix,
$this->output->isInside($path),
$this->output->isEqual($path),
=> Mark::Output->value.' '.$this->output->getRelativePath($path).$suffix,
$this->input->isInside($path),
$this->input->isEqual($path),
=> Mark::Input->value.' '.$this->input->getRelativePath($path).$suffix,
default
=> Mark::External->value.' '.$path.$suffix,
};

return $name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,12 @@ public function testGetPathname(): void {
$bFileSystemAFile = $bFileSystem->getFile($aPath->getFilePath('a.txt'));
$bFileSystemADirectory = $bFileSystem->getDirectory($aPath->getDirectoryPath('a'));

self::assertSame('↔ /', $bFileSystem->getPathname($bFileSystem->input));
self::assertSame('↔ /', $bFileSystem->getPathname($bFileSystem->output));
self::assertSame('↔ a.txt', $bFileSystem->getPathname($bFileSystemAFile));
self::assertSame('↔ a.txt', $bFileSystem->getPathname($bFileSystemAFile->getPath()));
self::assertSame('→ /', $aFileSystem->getPathname($aFileSystem->input));
self::assertSame('← /', $aFileSystem->getPathname($aFileSystem->output));
self::assertSame('→ a.txt', $aFileSystem->getPathname($aFileSystemAFile));
self::assertSame('→ a.txt', $aFileSystem->getPathname($aFileSystemAFile->getPath()));
self::assertSame('← b.txt', $aFileSystem->getPathname($aFileSystemBFile));
Expand Down
29 changes: 12 additions & 17 deletions packages/documentator/src/Processor/Listeners/Console/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use function implode;
use function ksort;
use function mb_strlen;
use function mb_substr;
use function memory_get_peak_usage;
use function memory_get_usage;
use function microtime;
Expand Down Expand Up @@ -110,15 +111,12 @@ protected function processingFinished(ProcessingFinished $event): void {
$time = microtime(true) - $this->start;
$peak = memory_get_peak_usage(true);
$memory = $peak > $this->peakMemory ? $peak - $this->startMemory : 0;
$message = "Files: {$this->formatter->integer($this->filesProcessed)}"
.", Updated: {$this->message($this->formatter->integer($this->filesUpdated), FileSystemModifiedType::Updated)}"
.", Created: {$this->message($this->formatter->integer($this->filesCreated), FileSystemModifiedType::Created)}"
$message = $this->message('✓', ProcessingFinishedResult::Success)
." Files: {$this->formatter->integer($this->filesProcessed)}"
.", [{$this->message('U', FileSystemModifiedType::Updated)}]pdated: {$this->message($this->formatter->integer($this->filesUpdated), FileSystemModifiedType::Updated)}"
.", [{$this->message('C', FileSystemModifiedType::Created)}]reated: {$this->message($this->formatter->integer($this->filesCreated), FileSystemModifiedType::Created)}"
.", Memory: {$this->formatter->filesize($memory)}";

if ($this->filesProcessed > 0) {
$this->output->writeln('');
}

$this->line(0, $message, $time, $event->result, []);

// Reset
Expand Down Expand Up @@ -257,18 +255,18 @@ protected function line(
ProcessingFinishedResult|FileFinishedResult|TaskFinishedResult|DependencyResolvedResult $result,
array $changes,
): void {
if ($changes === [] && (!$this->isLevelVisible($level) || !$this->isResultVisible($result))) {
if ((!$this->isLevelVisible($level) || !$this->isResultVisible($result))) {
return;
}

$flag = null;
$flags = $this->flags($changes, $path, $flag);
$template = [
'prefix' => str_repeat(' ', $level),
'prefix' => mb_substr(str_repeat('<fg=gray>·</> ', $level), 0, -1),
'message' => $this->message($path, $flag),
'spacer' => '',
'flags' => $flags,
'duration' => $time !== null ? "<fg=gray>{$this->formatter->duration($time)}</>" : '',
'flags' => str_repeat('<fg=gray>.</>', 3 - count($flags)).implode('', $flags),
'suffix' => $this->message($this->result($result), $result),
];
$length = array_map($this->length(...), $template);
Expand All @@ -288,8 +286,10 @@ protected function line(

/**
* @param list<Change> $changes
*
* @return list<string>
*/
protected function flags(array $changes, string $path, FileSystemModifiedType|Flag|null &$flag): string {
protected function flags(array $changes, string $path, FileSystemModifiedType|Flag|null &$flag): array {
// Collect
$flag = null;
$flags = [];
Expand All @@ -305,16 +305,11 @@ protected function flags(array $changes, string $path, FileSystemModifiedType|Fl
}
}

// Only one flag for $path?
if ($flag !== null && $flag !== Flag::Mixed && count($flags) === 1) {
return '';
}

// Sort
ksort($flags);

// Return
return implode('', array_keys($flags));
return array_keys($flags);
}

protected function length(string $message): int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ protected function getTerminalWidth(): int {
}

/**
* @param array{FileSystemModifiedType|Flag|null, string} $expected
* @param list<Change> $changes
* @param array{FileSystemModifiedType|Flag|null, list<string>} $expected
* @param list<Change> $changes
*/
#[DataProvider('dataProviderFlags')]
public function testFlags(array $expected, array $changes, string $path): void {
Expand All @@ -90,7 +90,7 @@ public function __construct() {
* @inheritDoc
*/
#[Override]
public function flags(array $changes, string $path, FileSystemModifiedType|Flag|null &$flag): string {
public function flags(array $changes, string $path, FileSystemModifiedType|Flag|null &$flag): array {
return parent::flags($changes, $path, $flag);
}
};
Expand Down Expand Up @@ -254,12 +254,12 @@ public static function dataProviderInvoke(): array {
}

/**
* @return array<string, array{array{FileSystemModifiedType|Flag|null, string}, list<Change>, string}>
* @return array<string, array{array{FileSystemModifiedType|Flag|null, list<string>}, list<Change>, string}>
*/
public static function dataProviderFlags(): array {
return [
'Path doesn\'t match' => [
[null, '<fg=green>C</><fg=yellow>U</>'],
[null, ['<fg=green>C</>', '<fg=yellow>U</>']],
[
new Change('a', FileSystemModifiedType::Created),
new Change('a', FileSystemModifiedType::Created),
Expand All @@ -270,7 +270,7 @@ public static function dataProviderFlags(): array {
'd',
],
'Path match to one' => [
[FileSystemModifiedType::Updated, '<fg=green>C</><fg=yellow>U</>'],
[FileSystemModifiedType::Updated, ['<fg=green>C</>', '<fg=yellow>U</>']],
[
new Change('a', FileSystemModifiedType::Created),
new Change('b', FileSystemModifiedType::Updated),
Expand All @@ -279,7 +279,7 @@ public static function dataProviderFlags(): array {
'b',
],
'Path match to multiple (same)' => [
[FileSystemModifiedType::Created, '<fg=green>C</><fg=yellow>U</>'],
[FileSystemModifiedType::Created, ['<fg=green>C</>', '<fg=yellow>U</>']],
[
new Change('a', FileSystemModifiedType::Created),
new Change('a', FileSystemModifiedType::Created),
Expand All @@ -288,7 +288,7 @@ public static function dataProviderFlags(): array {
'a',
],
'Path match to multiple (mixed)' => [
[Flag::Mixed, '<fg=green>C</><fg=yellow>U</>'],
[Flag::Mixed, ['<fg=green>C</>', '<fg=yellow>U</>']],
[
new Change('a', FileSystemModifiedType::Created),
new Change('a', FileSystemModifiedType::Updated),
Expand All @@ -297,15 +297,15 @@ public static function dataProviderFlags(): array {
'a',
],
'Path only (same)' => [
[FileSystemModifiedType::Created, ''],
[FileSystemModifiedType::Created, ['<fg=green>C</>']],
[
new Change('a', FileSystemModifiedType::Created),
new Change('a', FileSystemModifiedType::Created),
],
'a',
],
'Path only (mixed)' => [
[Flag::Mixed, '<fg=green>C</><fg=yellow>U</>'],
[Flag::Mixed, ['<fg=green>C</>', '<fg=yellow>U</>']],
[
new Change('a', FileSystemModifiedType::Created),
new Change('a', FileSystemModifiedType::Updated),
Expand Down
Loading
Loading