nur-sery/nur_src/tools/compctl/ut.php

47 lines
1.5 KiB
PHP

<?php
namespace nur\tools\compctl;
use Exception;
use nur\b\ValueException;
use nur\os;
use nur\path;
class ut {
static function resolve_projdir(?string $projdir): array {
if ($projdir !== null) {
if (is_file($projdir) && basename($projdir) === "composer.json") {
$projdir = dirname($projdir);
}
if (!path::is_dir($projdir)) {
throw ValueException::invalid_value($projdir, "directory");
}
} else {
$projdir = getcwd();
while (!file_exists("$projdir/composer.json")) {
$projdir = path::dirname($projdir);
if ($projdir == os::homedir() || $projdir == "/") {
throw new Exception("unable to find a composer project dir");
}
}
}
$composerFile = "$projdir/composer.json";
if (!file_exists($composerFile)) {
throw new Exception("$projdir: not a composer project dir");
}
$configFile = "$projdir/.composer.yaml";
return [$projdir, $composerFile, $configFile];
}
public static function resolve_branch(?string $branch, string $projdir): array {
$gitdir = git::rcmd1($retcode, "-C", $projdir, "rev-parse", "--show-toplevel");
if ($retcode != 0) {
if ($branch === null) $branch = "master";
} elseif ($branch === null) {
$branch = git::cmd1("-C", $projdir, "rev-parse", "--abbrev-ref", "HEAD");
}
#XXX s'il y a un répertoire git et que la branche est spécifiée, faut-il
# vérifier si elle existe?
return [$gitdir, $branch];
}
}