Skip to content

Commit

Permalink
Symlinks (#55)
Browse files Browse the repository at this point in the history
* build: upgrade dependencies

* build: upgrade dependencies to new versions

* test: satisfy mess detector

* ci: upgrade ci

* ci: remove scrutinizer

* test: create testbed for new functionality of #51

* feature: sync sym links
closes #51
  • Loading branch information
g105b authored Jul 6, 2023
1 parent 0313521 commit 5764001
Show file tree
Hide file tree
Showing 6 changed files with 453 additions and 292 deletions.
1 change: 0 additions & 1 deletion bin/sync
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env php
<?php

use Gt\Cli\Argument\CommandArgumentList;
use Gt\Sync\Command\SyncCommand;
use Gt\Cli\Application;
Expand Down
61 changes: 50 additions & 11 deletions src/Command/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Gt\Cli\Parameter\NamedParameter;
use Gt\Cli\Parameter\Parameter;
use Gt\Sync\DirectorySync;
use Gt\Sync\SymlinkSync;

class SyncCommand extends Command {
public function run(ArgumentValueList $arguments = null):void {
Expand All @@ -19,17 +20,11 @@ public function run(ArgumentValueList $arguments = null):void {
$pattern = "**/*";
}

$sync = new DirectorySync($source, $destination, $pattern);
$sync->exec();

if(!$arguments->contains("silent")) {
$this->write("Copied ");
$this->write((string)count($sync->getCopiedFilesList()));
$this->write(", skipped ");
$this->write((string)count($sync->getSkippedFilesList()));
$this->write(", deleted ");
$this->write((string)count($sync->getDeletedFilesList()));
$this->writeLine(".");
if($arguments->contains("symlink")) {
$this->performSymlinkSync($arguments, $source, $destination);
}
else {
$this->performDirectorySync($arguments, $source, $destination, $pattern);
}
}

Expand Down Expand Up @@ -67,6 +62,11 @@ public function getOptionalParameterList():array {
"pattern",
"p"
),
new Parameter(
false,
"symlink",
"l",
),
new Parameter(
false,
"silent",
Expand All @@ -79,4 +79,43 @@ public function getOptionalParameterList():array {
)
];
}

private function performDirectorySync(
ArgumentValueList $arguments,
string $source,
string $destination,
string $pattern,
):void {
$sync = new DirectorySync($source, $destination, $pattern);
$sync->exec();

if(!$arguments->contains("silent")) {
$this->write("Copied ");
$this->write((string)count($sync->getCopiedFilesList()));
$this->write(", skipped ");
$this->write((string)count($sync->getSkippedFilesList()));
$this->write(", deleted ");
$this->write((string)count($sync->getDeletedFilesList()));
$this->writeLine(".");
}
}

private function performSymlinkSync(
ArgumentValueList $arguments,
string $source,
string $destination,
):void {
$sync = new SymlinkSync($source, $destination);
$sync->exec();

if(!$arguments->contains("silent")) {
$this->write("Linked: directories ");
$this->write((string)count($sync->getLinkedDirectoriesList()));
$this->write(", files ");
$this->write((string)count($sync->getLinkedFilesList()));
$this->write(", failed ");
$this->write((string)count($sync->getFailedList()));
$this->writeLine(".");
}
}
}
69 changes: 69 additions & 0 deletions src/SymlinkSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
namespace Gt\Sync;

class SymlinkSync extends AbstractSync {
/** @var array<string> */
protected array $linkedFiles;
/** @var array<string> */
protected array $linkedDirectories;
/** @var array<string> */
protected array $skipped;
/** @var array<string> */
protected array $failed;

// phpcs:ignore
public function exec(int $settings = 0):void {
$this->linkedFiles = [];
$this->linkedDirectories = [];
$this->skipped = [];
$this->failed = [];

if(is_dir($this->source)) {
if(!is_dir(dirname($this->destination))) {
mkdir(dirname($this->destination), recursive: true);
}

if(symlink($this->source, $this->destination)) {
array_push($this->linkedDirectories, $this->destination);
}
else {
array_push($this->failed, $this->destination);
}
}
elseif(is_file($this->source)) {
if(!is_dir(dirname($this->destination))) {
mkdir(dirname($this->destination), recursive: true);
}

if(symlink($this->source, $this->destination)) {
array_push($this->linkedFiles, $this->destination);
}
else {
array_push($this->failed, $this->destination);
}
}
}

/** @return array<string> */
public function getLinkedFilesList():array {
return $this->linkedFiles;
}

/** @return array<string> */
public function getLinkedDirectoriesList():array {
return $this->linkedDirectories;
}

/** @return array<string> */
public function getCombinedLinkedList():array {
return array_merge(
$this->getLinkedFilesList(),
$this->getLinkedDirectoriesList(),
);
}

/** @return array<string> */
public function getFailedList():array {
return $this->failed;
}
}
Loading

0 comments on commit 5764001

Please sign in to comment.