diff --git a/src/Command/CoreListerVersions.php b/src/Command/CoreListerVersions.php
index 124b7d0..b221298 100644
--- a/src/Command/CoreListerVersions.php
+++ b/src/Command/CoreListerVersions.php
@@ -9,10 +9,11 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class CoreListerVersions extends Command {
- private $chemin_svn_racine = 'svn://trac.rezo.net/spip';
- private $versions = array();
- private $last = '';
-
+ private $spip_git_repo = 'https://git.spip.net/spip/spip.git';
+ private $versions = [];
+ private $lastXY = '';
+ private $prevXY = '';
+
protected function configure() {
$this
->setName('core:listerversions')
@@ -21,8 +22,8 @@ class CoreListerVersions extends Command {
'type',
't',
InputOption::VALUE_OPTIONAL,
- 'branches ou tags ?',
- '' // Par défaut, tout
+ 'tags ou branches ?',
+ 'tags' // Par défaut, tout
)
->setAliases(array(
'versions'
@@ -30,132 +31,127 @@ class CoreListerVersions extends Command {
;
}
+ /**
+ * execute
+ *
+ * @param mixed $input
+ * @param mixed $output
+ * @return void
+ */
protected function execute(InputInterface $input, OutputInterface $output) {
// On travaille dans le dossier courant
$dossier = getcwd();
-
+
$type = $input->getOption('type');
-
+
if (!function_exists('passthru')) {
$output->writeln("Votre installation de PHP doit pouvoir exécuter des commandes externes avec la fonction passthru().");
return Command::FAILURE;
}
- // Si c'est bon on continue
else{
- $versions = $this->get_versions();
-
- // Seulement ce type
- if (array_key_exists($type, $versions)) {
- $versions = array_intersect_key($versions, array($type=>'yes'));
+ if (!is_dir('.git')) {
+ $this->git_init();
}
-
- foreach ($versions as $type => $numeros) {
- $output->writeln("$type");
-
- foreach ($numeros as $numero => $url) {
- $output->writeln("$numero");
- }
+
+ $this->versions = $this->lister_versions($type);
+ foreach ($this->versions as $key => $value) {
+ $output->writeln($value);
}
- }
- return Command::SUCCESS;
- }
-
- public function get_last_release($xy='') {
- $versions = $this->get_versions();
- $tags = array_flip($versions['tags']);
-
- if ($xy) {
- $masque = "/^$xy\.\d+$/";
- }
- else {
- $masque = '/^\d+\.\d+\.\d+$/';
- }
-
- // On ne garde que les trucs stables
- $stables = array_filter(
- $tags,
- function ($cle) use ($masque) {
- return preg_match($masque, $cle);
+
+ // On fait en sorte de renvoyer au même format X.Y les dernière
+ // et avant-dernière versions que ce soit des branches ou des tags
+ // Utilisable tel quel ensuite pour gérer la compat des plugins
+ if ($type == 'branches') {
+ $versionsXYZ = preg_grep('/^\d+.\d+/', $this->versions);
+ $this->lastXY = end($versionsXYZ);
+ $this->prevXY = $this->trouver_version_precedente(end($versionsXYZ));
}
- );
-
- // On ne renvoit que la dernière version
- natsort($stables);
- return array_pop($stables);
- }
-
- public function get_last_branche($x='') {
- $versions = $this->get_versions();
- $branches = array_flip($versions['branches']);
-
- if ($x) {
- $masque = "/^$x.\d+$/";
- }
- else {
- $masque = '/^\d+\.\d+$/';
- }
-
- // On ne garde que les trucs stables
- $stables = array_filter(
- $branches,
- function ($cle) use ($masque) {
- return preg_match($masque, $cle);
+ else {
+ $this->lastXY = preg_replace('/^(\d+.\d+).\d+/', '${1}', end($this->versions));
+ $this->prevXY = preg_replace('/^(\d+.\d+).\d+/', '${1}',$this->trouver_version_precedente(end($this->versions)));
}
- );
-
- // On ne renvoit que la dernière version
- natsort($stables);
- return array_pop($stables);
- }
-
- public function get_versions() {
- if (!$this->versions) {
- $this->versions = $this->lister_versions();
}
-
- return $this->versions;
+
+ return Command::SUCCESS;
}
-
- public function lister_versions($type='') {
- $versions = array();
-
- if ($type != 'tags') {
- // On cherche les branches
+
+ /**
+ * git_init
+ *
+ * Initialisation d'un dépôt GIT vierge auquel on déclare le dépôt distant
+ * du core de spip afin de pouvoir ensuite appeler `git ls-remote origin`
+ * pour obtenir toutes les branches et tags
+ *
+ * @return void
+ */
+ public function git_init() {
+ ob_start();
+ passthru('git init');
+ passthru('git remote add origin ' . $this->spip_git_repo);
+ ob_end_clean();
+ }
+
+ /**
+ * lister_versions
+ *
+ * @param mixed $type
+ * @return array $liste
+ */
+ public function lister_versions(string $type) {
+ $liste = [];
+ if ($type == 'tags') {
ob_start();
- passthru("svn list {$this->chemin_svn_racine}/branches");
- $liste_branches = ob_get_contents();
+ passthru('git ls-remote --tags origin');
+ $tags = ob_get_contents();
ob_end_clean();
-
- // On transforme en tableau et nettoie
- $versions['branches'] = $this->svn_to_array($liste_branches, 'branches');
- $versions['branches']['trunk'] = 'svn://trac.rezo.net/spip/spip';
+
+ $tags_pattern = '/v\d+.\d+.\d+[a-z0-9+-]*$/sm';
+ preg_match_all($tags_pattern, $tags, $liste);
+
+ $liste = preg_replace('/v/', '', $liste[0]);
}
-
- if ($type != 'tags') {
- // On cherche les tags
+
+ if ($type == 'branches') {
ob_start();
- passthru("svn list {$this->chemin_svn_racine}/tags");
- $liste_tags = ob_get_contents();
+ passthru('git ls-remote --heads origin');
+ $branches = ob_get_contents();
ob_end_clean();
-
- // On transforme en tableau et nettoie
- $versions['tags'] = $this->svn_to_array($liste_tags, 'tags');
+
+ $branches_pattern = '/refs\/heads\/[a-z0-9_.\/-]*/s';
+ preg_match_all($branches_pattern, $branches, $liste);
+
+ $liste = preg_replace('/refs\/heads\//', '', $liste[0]);
}
-
- return $versions;
+
+ return $liste;
}
-
- private function svn_to_array($svn, $type) {
- $liste = array();
- $temp = explode(PHP_EOL, $svn);
-
- foreach ($temp as $dossier) {
- if ($cle = preg_replace('|(spip-)?(.*?)/?|i', '$2', $dossier)) {
- $liste[$cle] = "{$this->chemin_svn_racine}/$type/$dossier";
+
+ /**
+ * trouver_version_precedente
+ *
+ * @param mixed $version
+ * @return string $prevVersion
+ */
+ public function trouver_version_precedente($version) {
+
+ $versions = $this->versions;
+
+ if ($key = array_search($version, $versions)) {
+ if (preg_match('/^\d+.\d+/', $versions[$key]) && array_key_exists($key - 1, $versions)) {
+ $key -= 1;
+ $prevVersion = $versions[$key];
+
+ preg_match('/\d+.\d+/', $version, $versionXY);
+ preg_match('/\d+.\d+/', $prevVersion, $prevVersionXY);
+
+ while ($versionXY[0] == $prevVersionXY[0]) {
+ $prevVersion = $versions[$key];
+ preg_match('/\d+.\d+/', $prevVersion, $prevVersionXY);
+ $key -= 1;
+ }
+
+ return $prevVersion;
}
}
- $liste = array_filter(array_unique($liste));
-
- return $liste;
}
}