Move stow to it's own dir and separate include files out
This commit is contained in:
74
php/argvParser.php
Normal file
74
php/argvParser.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
class argParsed
|
||||
{
|
||||
public function __construct(
|
||||
private array $parsed = [],
|
||||
) {}
|
||||
|
||||
public function initOptions(array $options, mixed $default = false): self
|
||||
{
|
||||
foreach (array_keys($options) as $name) {
|
||||
$this->set($name, $default);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return $this->parsed[$key] ?? $default;
|
||||
}
|
||||
|
||||
public function set(string $key, mixed $value): self
|
||||
{
|
||||
$this->parsed[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
readonly class argvParser
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $options
|
||||
* @param array<string, string> $arguments
|
||||
*/
|
||||
public function __construct(
|
||||
private array $options = [],
|
||||
private array $arguments = [],
|
||||
) {}
|
||||
|
||||
public function getOptionsHelp(): string
|
||||
{
|
||||
$line = '';
|
||||
foreach ($this->options as $option => $description) {
|
||||
$line .= sprintf('[-%s (%s)] ', $option, $description);
|
||||
}
|
||||
foreach ($this->arguments as $argument => $description) {
|
||||
$line .= sprintf('<%s (%s)> ', $argument, $description);
|
||||
}
|
||||
return $line;
|
||||
}
|
||||
|
||||
public function parseArgv(array $argv): argParsed|false
|
||||
{
|
||||
array_shift($argv); // shift the script name
|
||||
|
||||
$parsed = (new argParsed())->initOptions($this->options);
|
||||
while (str_starts_with($argv[0] ?? '', '-')) {
|
||||
$option = substr($argv[0], 1);
|
||||
if ($this->options[$option]) {
|
||||
$parsed->set($option, true);
|
||||
}
|
||||
array_shift($argv);
|
||||
}
|
||||
if (count($argv) !== count($this->arguments)) {
|
||||
return false;
|
||||
}
|
||||
foreach ($this->arguments as $arg => $description) {
|
||||
$parsed->set($arg, array_shift($argv));
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
}
|
109
php/bin/stow
Executable file
109
php/bin/stow
Executable file
@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../argvParser.php';
|
||||
require_once __DIR__ . '/../functions.php';
|
||||
|
||||
readonly class stow
|
||||
{
|
||||
public function __construct(
|
||||
private string $action = 'stow',
|
||||
private bool $backup = false,
|
||||
private bool $force = false
|
||||
) {}
|
||||
|
||||
function run(string $sourcePath, string $targetPath): void
|
||||
{
|
||||
$sourceFiles = array_diff(scandir($sourcePath), ['..', '.']);
|
||||
|
||||
foreach ($sourceFiles as $sourceFile) {
|
||||
$targetFile = path($targetPath, $sourceFile);
|
||||
$stowFile = path($sourcePath, $sourceFile);
|
||||
|
||||
if ($this->action === 'stow') {
|
||||
if (is_link($targetFile)) {
|
||||
$targetLink = readlink($targetFile);
|
||||
if ($targetLink === $stowFile) {
|
||||
line("File $targetFile is already stowed from $stowFile");
|
||||
} else {
|
||||
line("File $targetFile is linked from $targetLink");
|
||||
}
|
||||
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 $targetFile from $stowFile");
|
||||
symlink($stowFile, $targetFile);
|
||||
} elseif ($this->action === 'unstow') {
|
||||
if (is_link($targetFile) && readlink($targetFile) === $stowFile) {
|
||||
line("Unstow $targetFile from $stowFile");
|
||||
unlink($targetFile);
|
||||
continue;
|
||||
}
|
||||
if (is_dir($targetFile)) {
|
||||
if (is_dir($stowFile)) {
|
||||
line("File $targetFile and $stowFile are folders, recurse into existing folders");
|
||||
$this->run($stowFile, $targetFile);
|
||||
continue;
|
||||
} else {
|
||||
line("File $targetFile is a folder, but stow file $stowFile is not a folder");
|
||||
}
|
||||
}
|
||||
line("File $targetFile is not stowed from $stowFile");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$argvParser = new ArgvParser(
|
||||
['u' => 'Unstow files', 'b' => 'Backup existing files', 'f' => 'Force overwrite'],
|
||||
['stow-directory' => 'The directory to stow']
|
||||
);
|
||||
$parsed = $argvParser->parseArgv($argv);
|
||||
if ($parsed === false) {
|
||||
line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$cwd = getcwd();
|
||||
$action = $parsed->get('u') ? 'unstow' : 'stow';
|
||||
$backup = $parsed->get('b');
|
||||
$force = $parsed->get('f');
|
||||
|
||||
$stowName = $parsed->get('stow-directory');
|
||||
$stowPath = path($cwd, $stowName);
|
||||
$targetPath = dirname($stowPath, 2);
|
||||
|
||||
if (!is_dir($stowPath)) {
|
||||
line("Directory '$stowName' does not exist");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$stow = new stow($action, $backup, $force);
|
||||
$stow->run($stowPath, $targetPath);
|
||||
|
||||
exit(0);
|
11
php/functions.php
Normal file
11
php/functions.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
function path(...$segments): string
|
||||
{
|
||||
return join(DIRECTORY_SEPARATOR, $segments);
|
||||
}
|
||||
|
||||
function line(string $line): void
|
||||
{
|
||||
echo $line . PHP_EOL;
|
||||
}
|
Reference in New Issue
Block a user