diff --git a/scripts/update-project-info.php b/scripts/update-project-info.php index a3f63b9..2ce32ee 100644 --- a/scripts/update-project-info.php +++ b/scripts/update-project-info.php @@ -2,10 +2,16 @@ /** * @file - * Injecte 'project:' et 'version:' dans les .info.yml des packages drupal/* - * installés en SOURCE (git), car les tags git ne les contiennent pas (seuls - * les tarballs drupal.org les ajoutent). Sans ça, le module update ne voit - * que le core dans /admin/reports/updates. + * Injecte 'project:' et 'version:' dans les .info.yml des packages drupal/*. + * + * - Les installs git (source) ne contiennent ni project: ni version: dans le + * .info.yml (seuls les tarballs packagés par drupal.org les ajoutent) : sans + * ça, le module update ne voit que le core dans /admin/reports/updates. + * - La version écrite doit être la version drupal.org (ex. '8.x-1.23', + * '8.x-3.x-dev'), PAS la version normalisée par composer (ex. '1.23.0') : + * sinon /admin/reports/updates affiche « Non supportée ! » en rouge. + * Le tag drupal.org est retrouvé via source.reference (tag git) ou l'URL + * dist ; pour les branches dev, via le release-history updates.drupal.org. * * Appelé via composer scripts post-install-cmd / post-update-cmd. */ @@ -18,6 +24,43 @@ if (!file_exists($installedFile)) { $installed = json_decode(file_get_contents($installedFile), TRUE); $packages = $installed['packages'] ?? $installed; +/** + * Retourne la version drupal.org d'un package, ou NULL si indéterminable. + */ +function drupal_org_version(array $pkg): ?string { + $ref = $pkg['source']['reference'] ?? ''; + // Un tag (ex. '8.x-1.23', '4.1.4', '2.0.0-beta14') — pas un hash de commit. + if ($ref !== '' && !preg_match('/^[0-9a-f]{40}$/i', $ref) && preg_match('/^[vV]?[0-9]/', $ref)) { + return ltrim($ref, 'vV'); + } + // Sinon, essaie d'extraire le tag de l'URL dist (ftp.drupal.org/files/projects/-.zip). + $dist = $pkg['dist']['url'] ?? ''; + if ($dist !== '' && preg_match('/-([0-9][^\/]*?)\.(zip|tar\.gz|tgz)$/', $dist, $m)) { + return $m[1]; + } + // Branche dev (ex. 'dev-3.x', 'dev-2.0.x') : interroge le release-history + // drupal.org pour trouver la version dev exacte ('8.x-3.x-dev', '3.x-dev'…). + $version = $pkg['version'] ?? ''; + if (preg_match('/^dev-(\d+(?:\.\d+)?\.x)$/', $version, $m)) { + $branch = $m[1]; + $short = substr($pkg['name'] ?? '', 7); + $url = 'https://updates.drupal.org/release-history/' . $short . '/current'; + $ctx = stream_context_create(['http' => ['timeout' => 5], 'https' => ['timeout' => 5]]); + $xml = @file_get_contents($url, false, $ctx); + if ($xml !== false && preg_match_all('/([^<]+)<\/version>/', $xml, $mm)) { + $candidates = ['8.x-' . $branch . '-dev', $branch . '-dev']; + foreach ($candidates as $cand) { + if (in_array($cand, $mm[1], true)) { + return $cand; + } + } + // Le XML ne connaît aucune version dev de cette branche : on laisse tel quel. + } + return $version; + } + return $version !== '' ? $version : NULL; +} + $patched = 0; foreach ($packages as $pkg) { $name = $pkg['name'] ?? ''; @@ -49,11 +92,11 @@ foreach ($packages as $pkg) { if (!preg_match('/^project:/mi', $content)) { $content = "project: '" . $short . "'\n" . $content; } - // version: à la fin si absent (on retire une éventuelle ligne version existante - // pour rester cohérent avec le lock). + // version: remplacée par la version drupal.org (jamais la version composer + // normalisée, ex. '1.23.0' au lieu de '8.x-1.23'). + $version = drupal_org_version($pkg); $content = preg_replace('/^version:.*$/mi', '', $content); - $version = $pkg['version'] ?? ''; - if ($version !== '') { + if ($version !== NULL && $version !== '') { $content = trim($content) . "\n\nversion: '" . $version . "'\n"; } if ($content !== $original) {