From 2d0c74b25ecb3cd482781d0f6dfcdd60a72517ba Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 11 Apr 2025 15:13:22 +0200 Subject: [PATCH] Stow: allow recursing unstow as well --- bin/bin/stow | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/bin/bin/stow b/bin/bin/stow index f3ac7f3..20dd1e0 100755 --- a/bin/bin/stow +++ b/bin/bin/stow @@ -64,11 +64,15 @@ readonly class stow foreach ($sourceFiles as $sourceFile) { $targetFile = path($targetPath, $sourceFile); $stowFile = path($sourcePath, $sourceFile); - $isStowed = is_link($targetFile) && readlink($targetFile) === $stowFile; if ($this->action === 'stow') { if (is_link($targetFile)) { - line("File $targetFile is already stowed from $stowFile"); + $targetLink = readlink($targetFile); + if ($targetLink === $stowFile) { + line("File $targetFile is already stowed from $stowFile"); + } else { + line("File $targetFile is linked from $targetLink"); + } continue; } if (is_dir($targetFile)) { @@ -97,14 +101,24 @@ readonly class stow continue; } } - line("Stow $stowFile to $targetFile"); + line("Stow $targetFile from $stowFile"); symlink($stowFile, $targetFile); } elseif ($this->action === 'unstow') { - if (!$isStowed) { - line("File $targetFile is not stowed from $stowFile"); + if (is_link($targetFile) && readlink($targetFile) === $stowFile) { + line("Unstow $targetFile from $stowFile"); + unlink($targetFile); 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"); } } }