Stow: allow recursing unstow as well

This commit is contained in:
Tim 2025-04-11 15:13:22 +02:00
parent 33ba450383
commit 2d0c74b25e

View File

@ -64,11 +64,15 @@ readonly class stow
foreach ($sourceFiles as $sourceFile) { foreach ($sourceFiles as $sourceFile) {
$targetFile = path($targetPath, $sourceFile); $targetFile = path($targetPath, $sourceFile);
$stowFile = path($sourcePath, $sourceFile); $stowFile = path($sourcePath, $sourceFile);
$isStowed = is_link($targetFile) && readlink($targetFile) === $stowFile;
if ($this->action === 'stow') { if ($this->action === 'stow') {
if (is_link($targetFile)) { if (is_link($targetFile)) {
$targetLink = readlink($targetFile);
if ($targetLink === $stowFile) {
line("File $targetFile is already stowed from $stowFile"); line("File $targetFile is already stowed from $stowFile");
} else {
line("File $targetFile is linked from $targetLink");
}
continue; continue;
} }
if (is_dir($targetFile)) { if (is_dir($targetFile)) {
@ -97,14 +101,24 @@ readonly class stow
continue; continue;
} }
} }
line("Stow $stowFile to $targetFile"); line("Stow $targetFile from $stowFile");
symlink($stowFile, $targetFile); symlink($stowFile, $targetFile);
} elseif ($this->action === 'unstow') { } elseif ($this->action === 'unstow') {
if (!$isStowed) { if (is_link($targetFile) && readlink($targetFile) === $stowFile) {
line("File $targetFile is not stowed from $stowFile"); line("Unstow $targetFile from $stowFile");
unlink($targetFile);
continue; continue;
} }
unlink($targetFile); if (is_dir($targetFile)) {
if (is_dir($stowFile)) {
line("File $targetFile and $stowFile are folders, recurse into existing folders");
$this->run($stowFile, $targetFile);
continue;
} else {
line("File $targetFile is a folder, but stow file $stowFile is not a folder");
}
}
line("File $targetFile is not stowed from $stowFile");
} }
} }
} }