Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c37577d

Browse files
authored
Merge pull request #986 from phel-lang/fix/981-phel-phar-doc
Fix doc command on phar
2 parents a0c5238 + 52e1e44 commit c37577d

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44

55
## Unreleased
66

7+
- Fix `doc` command on phar command
78
- Add `catch` and `finally` functions to the API docs
89
- Add `--format` json option to `phel doc` command
910

src/php/Api/Infrastructure/PhelFnLoader.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Phel\Api\Infrastructure;
66

7+
use Phar;
78
use Phel;
89
use Phel\Api\Domain\PhelFnLoaderInterface;
910
use Phel\Compiler\Infrastructure\GlobalEnvironmentSingleton;
@@ -440,7 +441,22 @@ public function loadAllPhelFunctions(array $namespaces): void
440441
}
441442

442443
$docPhelContent = str_replace('%REQUIRES%', $requireNamespaces, $template);
443-
$phelFile = __DIR__ . '/phel/doc.phel';
444+
445+
// When running in a phar, we can't write to __DIR__ due to phar.readonly
446+
// Use a temporary directory in the current working directory instead
447+
if (Phar::running() !== '') {
448+
$tempDir = getcwd() . '/.phel_temp_' . uniqid();
449+
mkdir($tempDir, 0755, true);
450+
$phelFile = $tempDir . '/doc.phel';
451+
} else {
452+
$phelDir = __DIR__ . '/phel';
453+
if (!is_dir($phelDir)) {
454+
mkdir($phelDir, 0755, true);
455+
}
456+
457+
$phelFile = $phelDir . '/doc.phel';
458+
}
459+
444460
file_put_contents($phelFile, $docPhelContent);
445461

446462
$namespace = $this->runFacade
@@ -462,6 +478,11 @@ public function loadAllPhelFunctions(array $namespaces): void
462478
}
463479

464480
unlink($phelFile);
481+
482+
// Clean up temporary directory if running in phar
483+
if (Phar::running() !== '' && dirname($phelFile) !== __DIR__) {
484+
rmdir(dirname($phelFile));
485+
}
465486
}
466487

467488
/**

src/php/Build/Application/NamespaceExtractor.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use RecursiveDirectoryIterator;
2222
use RecursiveIteratorIterator;
2323
use RegexIterator;
24+
use UnexpectedValueException;
2425

2526
final readonly class NamespaceExtractor implements NamespaceExtractorInterface
2627
{
@@ -137,12 +138,19 @@ private function findAllNs(string $directory): array
137138
return [];
138139
}
139140

140-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($realpath));
141-
$phelIterator = new RegexIterator($iterator, '/^.+\.phel$/i', RegexIterator::GET_MATCH);
141+
try {
142+
$directoryIterator = new RecursiveDirectoryIterator($realpath);
143+
$iterator = new RecursiveIteratorIterator($directoryIterator);
144+
$phelIterator = new RegexIterator($iterator, '/^.+\.phel$/i', RegexIterator::GET_MATCH);
142145

143-
$result = [];
144-
foreach ($phelIterator as $file) {
145-
$result[] = $this->getNamespaceFromFile($file[0]);
146+
$result = [];
147+
foreach ($phelIterator as $file) {
148+
$result[] = $this->getNamespaceFromFile($file[0]);
149+
}
150+
} catch (UnexpectedValueException) {
151+
// Skip directories that cannot be read (e.g., permission denied)
152+
// This can happen with system-protected directories in temp paths
153+
return [];
146154
}
147155

148156
return $result;

0 commit comments

Comments
 (0)