- Added symlink install
This commit is contained in:
parent
cce9d9865b
commit
03f8f80293
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Picea\Asset;
|
||||
|
||||
class Config
|
||||
{
|
||||
public function __construct(
|
||||
public null|string $destination = null,
|
||||
) {}
|
||||
}
|
Loading…
Reference in New Issue