From 8cfd803ead516b7f7e98cda369ea18e2729c30b3 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Sun, 2 Mar 2025 19:03:10 +0400 Subject: [PATCH] gestion des profils composer --- bash/src/pman.sh | 4 + bin/_pman-composer_select_profile.php | 19 +++ bin/pman | 5 + php/src/ext/json.php | 1 - php/src/tools/pman/ComposerFile.php | 150 ++++++++++++++++++++++ php/src/tools/pman/PmanYamlConfigFile.php | 57 ++++++++ 6 files changed, 235 insertions(+), 1 deletion(-) create mode 100755 bin/_pman-composer_select_profile.php create mode 100644 php/src/tools/pman/ComposerFile.php create mode 100644 php/src/tools/pman/PmanYamlConfigFile.php diff --git a/bash/src/pman.sh b/bash/src/pman.sh index a9c4c49..dcd9dfb 100644 --- a/bash/src/pman.sh +++ b/bash/src/pman.sh @@ -241,6 +241,10 @@ function load_config() { else ConfigFile="$NULIBDIR/bash/src/pman.conf.sh" fi + + # S'assurer que nulib est dans le PATH pour que les scripts utilisateurs + # puissent utiliser les outils fournis + export PATH="$NULIBDIR/bin:$PATH" } ################################################################################ diff --git a/bin/_pman-composer_select_profile.php b/bin/_pman-composer_select_profile.php new file mode 100755 index 0000000..7f95bdf --- /dev/null +++ b/bin/_pman-composer_select_profile.php @@ -0,0 +1,19 @@ +#!/usr/bin/php +selectProfile($profile, $config); +//$composer->print(); +$composer->write(); diff --git a/bin/pman b/bin/pman index e8607cd..2c999e4 100755 --- a/bin/pman +++ b/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" -w,--show-config action=show "++\ afficher la configuration chargée" + --composer-select-profile action=composer_select_profile "\ +sélectionner le profil composer spécifié en argument" -O:,--origin Origin= "++\ origine vers laquelle pousser les branches" -n,--no-push Push= "\ @@ -265,6 +267,9 @@ init) git_ensure_cleancheckout init_action "$@" ;; +composer_select_profile) + exec "$MYDIR/_pman-$action.php" "$@" + ;; *) die "$action: action non implémentée" ;; diff --git a/php/src/ext/json.php b/php/src/ext/json.php index 68316f8..287a574 100644 --- a/php/src/ext/json.php +++ b/php/src/ext/json.php @@ -2,7 +2,6 @@ namespace nulib\ext; use Exception; -use nulib\ext\json\JsonException; use nulib\file; use nulib\os\IOException; diff --git a/php/src/tools/pman/ComposerFile.php b/php/src/tools/pman/ComposerFile.php new file mode 100644 index 0000000..5a48a1c --- /dev/null +++ b/php/src/tools/pman/ComposerFile.php @@ -0,0 +1,150 @@ +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"); + } +} diff --git a/php/src/tools/pman/PmanYamlConfigFile.php b/php/src/tools/pman/PmanYamlConfigFile.php new file mode 100644 index 0000000..c0998dd --- /dev/null +++ b/php/src/tools/pman/PmanYamlConfigFile.php @@ -0,0 +1,57 @@ +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); + } +}