From 03f8f802938f7b726aa02c6a89130a933e1244a1 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 17 Oct 2023 23:16:25 -0400 Subject: [PATCH] - Added symlink install --- src/Action/Install.php | 2 +- src/Action/Symlink.php | 54 +++++++++++++++++++++++++++++++++++++++--- src/Asset.php | 3 +-- src/Config.php | 10 ++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 src/Config.php diff --git a/src/Action/Install.php b/src/Action/Install.php index 837d8a9..4b10594 100644 --- a/src/Action/Install.php +++ b/src/Action/Install.php @@ -22,7 +22,7 @@ class Install { $output = []; foreach($this->actions as $action) { - $output[$action::class] = $action->run($fileFetcher); + $output[$action::class] = $action->run(); } return $output; diff --git a/src/Action/Symlink.php b/src/Action/Symlink.php index 1be30ca..cc36a32 100644 --- a/src/Action/Symlink.php +++ b/src/Action/Symlink.php @@ -2,18 +2,66 @@ namespace Picea\Asset\Action; +use Picea\Asset\Config; use Picea\Asset\FileFetcher; class Symlink implements InstallActionInterface { + public array $packageLinks = []; + public function __construct( - protected FileFetcher $fileFetcher + protected FileFetcher $fileFetcher, + protected Config $config, ) { } - public function run() : mixed + public function run(bool $dryRun = false) : mixed { - dump($this->fileFetcher->getFileList()); + if (empty($this->config->destination)) { + throw new \InvalidArgumentException("A destination must be provided into Config object."); + } + + foreach($this->fileFetcher->getFileList() as $path => $file) { + $this->mapPackagesLinks($path, $file); + } + + if ( ! $dryRun ) { + foreach($this->packageLinks as $link => $source) { + $destination = $this->config->destination . DIRECTORY_SEPARATOR . $link; + + # Clear previously linked path + if (is_link($destination)) { + unlink($destination); + } + + if ( ! @symlink($source, $destination) ) { + throw new \RuntimeException(sprintf("Could not create symlink for %s -> %s", $source, $destination)); + } + } + } return null; } + + protected function mapPackagesLinks(string $path, array $splFiles) : void + { + $links = []; + $len = strlen($path); + + foreach($splFiles as $file) { + $name = $file->getPathName(); + $relativePath = substr($name, $len); + + $link = $this->extractLink($relativePath); + + $links[$link] ??= $path . $link; + } + + $this->packageLinks = $this->packageLinks + $links; + } + + protected function extractLink(string $name) { + $pos = strpos($name, DIRECTORY_SEPARATOR); + + return $pos !== false ? substr($name, 0, $pos) : $name; + } } \ No newline at end of file diff --git a/src/Asset.php b/src/Asset.php index 8b2efbe..4ac43f1 100644 --- a/src/Asset.php +++ b/src/Asset.php @@ -10,6 +10,7 @@ class Asset { use EventTrait; public function __construct( + public Config $config, public Action\Install $install, public FileFetcher $fileFetcher, ) { } @@ -24,8 +25,6 @@ class Asset { public function launchInstall() : void { $result = $this->install->launch($this->fileFetcher); - - #dump($result); } } diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..03d0bbf --- /dev/null +++ b/src/Config.php @@ -0,0 +1,10 @@ +