gestion des profils composer
This commit is contained in:
parent
d9989c6be7
commit
8cfd803ead
@ -241,6 +241,10 @@ function load_config() {
|
|||||||
else
|
else
|
||||||
ConfigFile="$NULIBDIR/bash/src/pman.conf.sh"
|
ConfigFile="$NULIBDIR/bash/src/pman.conf.sh"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# S'assurer que nulib est dans le PATH pour que les scripts utilisateurs
|
||||||
|
# puissent utiliser les outils fournis
|
||||||
|
export PATH="$NULIBDIR/bin:$PATH"
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
|
19
bin/_pman-composer_select_profile.php
Executable file
19
bin/_pman-composer_select_profile.php
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/php
|
||||||
|
<?php
|
||||||
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
|
use nulib\tools\pman\ComposerFile;
|
||||||
|
use nulib\tools\pman\PmanYamlConfigFile;
|
||||||
|
use nulib\ValueException;
|
||||||
|
|
||||||
|
$composer = new ComposerFile();
|
||||||
|
$config = new PmanYamlConfigFile();
|
||||||
|
|
||||||
|
if ($argc <= 1) {
|
||||||
|
throw new ValueException("Il faut spécifier le profil à sélectionner");
|
||||||
|
}
|
||||||
|
$profile = $argv[1];
|
||||||
|
|
||||||
|
$composer->selectProfile($profile, $config);
|
||||||
|
//$composer->print();
|
||||||
|
$composer->write();
|
5
bin/pman
5
bin/pman
@ -238,6 +238,8 @@ fichier de configuration des branches. cette option est prioritaire sur --config
|
|||||||
par défaut, utiliser le fichier .pman.conf dans le répertoire du dépôt s'il existe"
|
par défaut, utiliser le fichier .pman.conf dans le répertoire du dépôt s'il existe"
|
||||||
-w,--show-config action=show "++\
|
-w,--show-config action=show "++\
|
||||||
afficher la configuration chargée"
|
afficher la configuration chargée"
|
||||||
|
--composer-select-profile action=composer_select_profile "\
|
||||||
|
sélectionner le profil composer spécifié en argument"
|
||||||
-O:,--origin Origin= "++\
|
-O:,--origin Origin= "++\
|
||||||
origine vers laquelle pousser les branches"
|
origine vers laquelle pousser les branches"
|
||||||
-n,--no-push Push= "\
|
-n,--no-push Push= "\
|
||||||
@ -265,6 +267,9 @@ init)
|
|||||||
git_ensure_cleancheckout
|
git_ensure_cleancheckout
|
||||||
init_action "$@"
|
init_action "$@"
|
||||||
;;
|
;;
|
||||||
|
composer_select_profile)
|
||||||
|
exec "$MYDIR/_pman-$action.php" "$@"
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
die "$action: action non implémentée"
|
die "$action: action non implémentée"
|
||||||
;;
|
;;
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
namespace nulib\ext;
|
namespace nulib\ext;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use nulib\ext\json\JsonException;
|
|
||||||
use nulib\file;
|
use nulib\file;
|
||||||
use nulib\os\IOException;
|
use nulib\os\IOException;
|
||||||
|
|
||||||
|
150
php/src/tools/pman/ComposerFile.php
Normal file
150
php/src/tools/pman/ComposerFile.php
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
<?php
|
||||||
|
namespace nulib\tools\pman;
|
||||||
|
|
||||||
|
use nulib\cl;
|
||||||
|
use nulib\ext\json;
|
||||||
|
use nulib\file;
|
||||||
|
use nulib\os\path;
|
||||||
|
use nulib\ValueException;
|
||||||
|
|
||||||
|
class ComposerFile {
|
||||||
|
function __construct(string $composerFile=".", bool $ensureExists=true) {
|
||||||
|
if (is_dir($composerFile)) $composerFile = path::join($composerFile, 'composer.json');
|
||||||
|
if ($ensureExists && !file_exists($composerFile)) {
|
||||||
|
$message = path::ppath($composerFile).": fichier introuvable";
|
||||||
|
throw new ValueException($message);
|
||||||
|
}
|
||||||
|
$this->composerFile = $composerFile;
|
||||||
|
$this->load();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected string $composerFile;
|
||||||
|
|
||||||
|
function getComposerFile(): string {
|
||||||
|
return $this->composerFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ?array $data = null;
|
||||||
|
|
||||||
|
protected function load(): array {
|
||||||
|
if ($this->data === null) {
|
||||||
|
$this->data = json::load($this->composerFile);
|
||||||
|
}
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getv(string $pkey="", $default=null) {
|
||||||
|
return cl::pget($this->data, $pkey, $default);
|
||||||
|
}
|
||||||
|
|
||||||
|
function geta(string $pkey="", ?array $default=[]): ?array {
|
||||||
|
return cl::withn($this->getv($pkey, $default));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRequires(): array {
|
||||||
|
return $this->geta("require");
|
||||||
|
}
|
||||||
|
|
||||||
|
function setRequire(string $dep, string $version): void {
|
||||||
|
$this->setPkey("require.$dep", $version);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRequireDevs(): array {
|
||||||
|
return $this->geta("require-dev");
|
||||||
|
}
|
||||||
|
|
||||||
|
function setRequireDev(string $dep, string $version): void {
|
||||||
|
$this->setPkey("require-dev.$dep", $version);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRepositories(): array {
|
||||||
|
return $this->geta("repositories");
|
||||||
|
}
|
||||||
|
|
||||||
|
function setRepositories(array $repositories): void {
|
||||||
|
$this->data["repositories"] = $repositories;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setPkey(string $pkey, $value): void {
|
||||||
|
cl::pset($this->data, $pkey, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function delPkey(string $pkey): void {
|
||||||
|
cl::pdel($this->data, $pkey);
|
||||||
|
}
|
||||||
|
|
||||||
|
const PATHS = [
|
||||||
|
"nulib/php" => "nulib",
|
||||||
|
];
|
||||||
|
|
||||||
|
function selectProfile(string $profile, PmanYamlConfigFile $config): void {
|
||||||
|
$config = $config->getProfileConfig($profile);
|
||||||
|
// corriger les liens
|
||||||
|
$deps = cl::merge(array_keys($config["require"]), array_keys($config["require-dev"]));
|
||||||
|
$paths = [];
|
||||||
|
foreach ($deps as $dep) {
|
||||||
|
$path = cl::get(self::PATHS, $dep, $dep);
|
||||||
|
$path = str_replace("/", "-", $path);
|
||||||
|
$path = "../$path";
|
||||||
|
$paths[$dep] = $path;
|
||||||
|
}
|
||||||
|
if ($config["link"]) {
|
||||||
|
// Ajouter les liens
|
||||||
|
$adds = [];
|
||||||
|
$repositories = $this->getRepositories();
|
||||||
|
foreach ($deps as $dep) {
|
||||||
|
$found = false;
|
||||||
|
foreach ($repositories as $repository) {
|
||||||
|
if ($repository["type"] === "path" && $repository["url"] === $paths[$dep]) {
|
||||||
|
$found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$found) {
|
||||||
|
$adds[] = [
|
||||||
|
"type" => "path",
|
||||||
|
"url" => $paths[$dep],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($adds) {
|
||||||
|
$this->setRepositories(cl::merge($adds, $repositories));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Supprimer les liens
|
||||||
|
$dels = [];
|
||||||
|
$repositories = $this->getRepositories();
|
||||||
|
foreach ($deps as $dep) {
|
||||||
|
foreach ($repositories as $key => $repository) {
|
||||||
|
if ($repository["type"] === "path" && $repository["url"] === $paths[$dep]) {
|
||||||
|
$dels[] = $key;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($dels) {
|
||||||
|
foreach (array_reverse($dels) as $key) {
|
||||||
|
unset($repositories[$key]);
|
||||||
|
}
|
||||||
|
$this->setRepositories(array_values($repositories));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// corriger les versions
|
||||||
|
foreach ($config["require"] as $dep => $version) {
|
||||||
|
$this->data["require"][$dep] = $version;
|
||||||
|
}
|
||||||
|
foreach ($config["require-dev"] as $dep => $version) {
|
||||||
|
$this->data["require-dev"][$dep] = $version;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function print(): void {
|
||||||
|
$contents = json::with($this->data, json::INDENT_TABS);
|
||||||
|
if ($contents) echo "$contents\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
function write(): void {
|
||||||
|
$contents = json::with($this->data, json::INDENT_TABS);
|
||||||
|
file::writer($this->composerFile)->putContents("$contents\n");
|
||||||
|
}
|
||||||
|
}
|
57
php/src/tools/pman/PmanYamlConfigFile.php
Normal file
57
php/src/tools/pman/PmanYamlConfigFile.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
namespace nulib\tools\pman;
|
||||||
|
|
||||||
|
use nulib\A;
|
||||||
|
use nulib\ext\yaml;
|
||||||
|
use nulib\os\path;
|
||||||
|
use nulib\ValueException;
|
||||||
|
|
||||||
|
class PmanYamlConfigFile {
|
||||||
|
function __construct(string $configFile=".", bool $ensureExists=true) {
|
||||||
|
if (is_dir($configFile)) $configFile = path::join($configFile, '.pman.yml');
|
||||||
|
if ($ensureExists && !file_exists($configFile)) {
|
||||||
|
$message = path::ppath($configFile).": fichier introuvable";
|
||||||
|
throw new ValueException($message);
|
||||||
|
}
|
||||||
|
$this->configFile = $configFile;
|
||||||
|
$this->load();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected string $configFile;
|
||||||
|
|
||||||
|
function getConfigFile(): string {
|
||||||
|
return $this->configFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ?array $data = null;
|
||||||
|
|
||||||
|
protected function load(): array {
|
||||||
|
if ($this->data === null) {
|
||||||
|
$data = yaml::load($this->configFile);
|
||||||
|
$composer =& $data["composer"];
|
||||||
|
A::ensure_array($composer);
|
||||||
|
A::ensure_array($composer["profiles"]);
|
||||||
|
foreach ($composer["profiles"] as $profileName) {
|
||||||
|
$profile =& $composer[$profileName];
|
||||||
|
A::ensure_array($profile);
|
||||||
|
$profile["link"] = boolval($profile["link"] ?? false);
|
||||||
|
A::ensure_array($profile["require"]);
|
||||||
|
A::ensure_array($profile["require-dev"]);
|
||||||
|
}
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getProfileConfig(string $profile): array {
|
||||||
|
$config = $this->data["composer"][$profile] ?? null;
|
||||||
|
if ($config === null) {
|
||||||
|
throw new ValueException("$profile: profil invalide");
|
||||||
|
}
|
||||||
|
return $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
function print(): void {
|
||||||
|
yaml::dump($this->data);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user