From 33ba450383e7e8579d2fa368ee58be8fba0c5b8d Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 11 Apr 2025 14:51:15 +0200 Subject: [PATCH] Stow: allow recursing into folder, tree folding --- bin/bin/stow | 92 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/bin/bin/stow b/bin/bin/stow index d2c6e68..f3ac7f3 100755 --- a/bin/bin/stow +++ b/bin/bin/stow @@ -16,7 +16,7 @@ $action = 'stow'; $backup = $force = false; array_shift($argv); // we don't care about the script name -if(count($argv) === 0) { +if (count($argv) === 0) { line("Usage: stow [-u] [-b] [-f] "); exit(1); } @@ -49,46 +49,66 @@ if (!is_dir($stowPath)) { exit(1); } -$files = array_diff(scandir($stowPath), ['..', '.']); +readonly class stow +{ + public function __construct( + private string $action = 'stow', + private bool $backup = false, + private bool $force = false + ) {} -foreach ($files as $file) { - $targetFile = path($targetPath, $file); - $stowFile = path($stowPath, $file); - $isStowed = is_link($targetFile) && readlink($targetFile) === $stowFile; + function run(string $sourcePath, string $targetPath): void + { + $sourceFiles = array_diff(scandir($sourcePath), ['..', '.']); - if ($action === 'stow') { - if (is_link($targetFile)) { - line("File $targetFile is already stowed from $stowFile"); - continue; - } - if(is_dir($targetFile)) { - line("File $targetFile is a folder, todo: recurse through existing folders"); - continue; - } - if (is_file($targetFile)) { - if ($backup) { - $backFile = $targetFile . '.bak'; - if (is_file($backFile)) { - line("Backup file $backFile already exists"); + 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"); continue; } - rename($targetFile, $backFile); - line("Backup file $targetFile to $backFile"); - } elseif ($force) { - line("Todo: delete file $targetFile"); - continue; - } else { - line("File $targetFile already exists"); - continue; + if (is_dir($targetFile)) { + if (is_dir($stowFile)) { + line("File $targetFile and $stowFile are folders, recurse into existing folders"); + $this->run($stowFile, $targetFile); + } else { + line("File $targetFile is a folder, but stow file $stowFile is not a folder"); + } + continue; + } + if (is_file($targetFile)) { + if ($this->backup) { + $backFile = $targetFile . '.bak'; + if (is_file($backFile)) { + line("Backup file $backFile already exists"); + continue; + } + rename($targetFile, $backFile); + line("Backup file $targetFile to $backFile"); + } elseif ($this->force) { + line("Todo: delete file $targetFile"); + continue; + } else { + line("File $targetFile already exists"); + continue; + } + } + line("Stow $stowFile to $targetFile"); + symlink($stowFile, $targetFile); + } elseif ($this->action === 'unstow') { + if (!$isStowed) { + line("File $targetFile is not stowed from $stowFile"); + continue; + } + unlink($targetFile); } } - line("Stow $stowFile to $targetFile"); - symlink($stowFile, $targetFile); - } elseif ($action === 'unstow') { - if (!$isStowed) { - line("File $targetFile is not stowed from $stowFile"); - continue; - } - unlink($targetFile); } } + +$stow = new stow($action, $backup, $force); +$stow->run($stowPath, $targetPath); \ No newline at end of file