- Added autoclean of dead symlinks

This commit is contained in:
Dave M. 2023-10-18 19:49:24 +00:00
parent 8396dc6d74
commit e36756b951
1 changed files with 11 additions and 1 deletions

View File

@ -14,7 +14,7 @@ class Symlink implements InstallActionInterface
protected Config $config,
) { }
public function run(bool $dryRun = false) : mixed
public function run(bool $dryRun = false, bool $clean = true) : mixed
{
if (empty($this->config->destination)) {
throw new \InvalidArgumentException("A destination must be provided into Config object.");
@ -24,6 +24,16 @@ class Symlink implements InstallActionInterface
$this->mapPackagesLinks($path, $file);
}
if ( $clean ) {
foreach(glob($this->config->destination . DIRECTORY_SEPARATOR . "*") as $file) {
if ( is_link($file) ) {
if (! file_exists(readlink($file)) ) {
unlink($file);
}
}
}
}
if ( ! $dryRun ) {
foreach($this->packageLinks as $link => $source) {
$destination = $this->config->destination . DIRECTORY_SEPARATOR . $link;