From 5e141b575e419f8a9fcabb46cbd7df112e7759af Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 16 Apr 2025 14:44:04 +0400 Subject: [PATCH] =?UTF-8?q?pman:=20ajout=20des=20cl=C3=A9s=20match=5Frequi?= =?UTF-8?q?re=20et=20match=5Frequire-dev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/pman | 6 ++++ php/src/db/sqlite/SqliteStorage.php | 2 +- php/src/str.php | 15 ++++++++++ php/src/tools/pman/ComposerFile.php | 2 +- php/src/tools/pman/ComposerPmanFile.php | 37 ++++++++++++++++++++++++- 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/bin/pman b/bin/pman index 0648670..27c6c57 100755 --- a/bin/pman +++ b/bin/pman @@ -98,11 +98,17 @@ function _init_composer() { # -*- coding: utf-8 mode: yaml -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8 composer: + match_prefix: + match_prefix-dev: profiles: [ dev, dist ] dev: link: true + require: + reqire-dev: dist: link: false + require: + reqire-dev: EOF "${EDITOR:-nano}" "$config" [ -s "$config" ] || return 1 diff --git a/php/src/db/sqlite/SqliteStorage.php b/php/src/db/sqlite/SqliteStorage.php index 1f09520..9d52cc9 100644 --- a/php/src/db/sqlite/SqliteStorage.php +++ b/php/src/db/sqlite/SqliteStorage.php @@ -68,7 +68,7 @@ class SqliteStorage extends CapacitorStorage { if (!$this->channelExists($channel->getName())) { # ne pas insérer si la ligne existe déjà, pour éviter d'avoir besoin d'un # verrou en écriture - $this->_addToChannelsSql($channel); + $db->exec($this->_addToChannelsSql($channel)); } } diff --git a/php/src/str.php b/php/src/str.php index 9a54876..b0f3497 100644 --- a/php/src/str.php +++ b/php/src/str.php @@ -242,6 +242,21 @@ class str { return true; } + /** + * vérifier si $s a le préfixe $prefix + * - si $prefix commence par /, c'est une expression régulière, et elle doit + * matcher $s + * - sinon $s doit commencer par la chaine $prefix + */ + static final function match_prefix(?string $s, ?string $prefix): bool { + if ($s === null || $prefix === null) return false; + if (substr($prefix, 0, 1) === "/") { + return preg_match($prefix, $s); + } else { + return self::_starts_with($prefix, $s); + } + } + /** * ajouter $sep$prefix$text$suffix à $s si $text est non vide * diff --git a/php/src/tools/pman/ComposerFile.php b/php/src/tools/pman/ComposerFile.php index 1cb8ed0..c3dd7a1 100644 --- a/php/src/tools/pman/ComposerFile.php +++ b/php/src/tools/pman/ComposerFile.php @@ -78,7 +78,7 @@ class ComposerFile { ]; function selectProfile(string $profile, ComposerPmanFile $config): void { - $config = $config->getProfileConfig($profile); + $config = $config->getProfileConfig($profile, $this->getRequires(), $this->getRequireDevs()); // corriger les liens $deps = cl::merge(array_keys($config["require"]), array_keys($config["require-dev"])); $paths = []; diff --git a/php/src/tools/pman/ComposerPmanFile.php b/php/src/tools/pman/ComposerPmanFile.php index b0520c0..d8c6474 100644 --- a/php/src/tools/pman/ComposerPmanFile.php +++ b/php/src/tools/pman/ComposerPmanFile.php @@ -4,6 +4,7 @@ namespace nulib\tools\pman; use nulib\A; use nulib\ext\yaml; use nulib\os\path; +use nulib\str; use nulib\ValueException; class ComposerPmanFile { @@ -49,6 +50,8 @@ class ComposerPmanFile { $composer =& $data["composer"]; A::ensure_array($composer); A::ensure_array($composer["profiles"]); + A::ensure_array($composer["match_require"]); + A::ensure_array($composer["match_require-dev"]); foreach ($composer["profiles"] as $profileName) { $profile =& $composer[$profileName]; A::ensure_array($profile); @@ -61,11 +64,43 @@ class ComposerPmanFile { return $this->data; } - function getProfileConfig(string $profile): array { + function getProfileConfig(string $profile, ?array $composerRequires=null, ?array $composerRequireDevs=null): array { $config = $this->data["composer"][$profile] ?? null; if ($config === null) { throw new ValueException("$profile: profil invalide"); } + if ($composerRequires !== null) { + $matchRequires = $this->data["composer"]["match_require"]; + foreach ($composerRequires as $dep => $version) { + $found = false; + foreach ($matchRequires as $matchRequire) { + if (str::match_prefix($dep, $matchRequire)) { + $found = true; + break; + } + } + $require = $config["require"][$dep] ?? null; + if ($found && $require === null) { + $config["require"][$dep] = $version; + } + } + } + if ($composerRequireDevs !== null) { + $matchRequireDevs = $this->data["composer"]["match_require-dev"]; + foreach ($composerRequireDevs as $dep => $version) { + $found = false; + foreach ($matchRequireDevs as $matchRequireDev) { + if (str::match_prefix($dep, $matchRequireDev)) { + $found = true; + break; + } + } + $requireDev = $config["require-dev"][$dep] ?? null; + if ($found && $requireDev === null) { + $config["require"][$dep] = $version; + } + } + } return $config; }