Skip to content
Extraits de code Groupes Projets
Valider 626f2d9f rédigé par JamesRezo's avatar JamesRezo :tada:
Parcourir les fichiers

feat(mode-dev): git facilities

parent 5687c59f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
<?php
namespace Spip\Composer\Git;
/**
* git utilities
* @since 0.6.0
*/
class RemoteUrls
{
private array $matching = [
'github.com',
'gitlab.com',
'git.spip.net',
];
protected function regexp(): string
{
return '(' . \implode('|', \str_replace('.', '\.',$this->matching)) . ')';
}
/**
* Provides the ssh counterpart of an https matching url
*/
public function toSsh(string $url): string
{
return \preg_replace(',https://'.$this->regexp().'/,', 'git@$1:', $url);
}
/**
* Provides the https counterpart of an ssh matching url
*/
public function toHttps(string $url): string
{
return \preg_replace(',git@'.$this->regexp().':,', 'https://$1/', $url);
}
}
......@@ -4,15 +4,10 @@ namespace Spip\Composer;
use Composer\Script\Event;
use Composer\Util\ProcessExecutor;
use Spip\Composer\Git\RemoteUrls;
class ModeDev
{
private static array $matching = [
'github.com',
'gitlab.com',
'git.spip.net',
];
public static function activate(Event $event)
{
$io = $event->getIO();
......@@ -52,15 +47,14 @@ class ModeDev
return $list;
}, []);
$changer = new RemoteUrls();
foreach ($toChange as $path) {
$url = '';
$processor->execute('git -C '.$path.' remote get-url origin', $url);
foreach (self::$matching as $server) {
if (str_starts_with($url, 'https://'.$server.'/')) {
$newUrl = str_replace('https://'.$server.'/', 'git@'.$server.':', $url);
$io->write($url . ' -> ' . $newUrl);
$processor->execute('git -C '.$path.' remote set-url origin '.$newUrl);
}
$newUrl = $changer->toSsh($url);
if ($newUrl !== $url) {
$io->write($url . ' -> ' . $newUrl);
$processor->execute('git -C '.$path.' remote set-url origin '.$newUrl);
}
}
$io->write('Done.');
......
<?php
namespace Spip\Test\Composer\Git;
use PHPUnit\Framework\TestCase;
use Spip\Composer\Git\RemoteUrls;
/**
* @covers Spip\Composer\Git\RemoteUrls
*/
class RemoteUrlsTest extends TestCase
{
private RemoteUrls $changer;
protected function setUp(): void
{
$this->changer = new RemoteUrls();
}
public static function dataHttpsToSsh()
{
return [
'empty-url' => [
'expected' => '',
'url' => '',
],
'matching-spip-url' => [
'expected' => 'git@git.spip.net:owner/package.git',
'url' => 'https://git.spip.net/owner/package.git',
],
'non-matching-spip-url' => [
'expected' => 'git@git.spip.net:owner/package.git',
'url' => 'git@git.spip.net:owner/package.git',
],
];
}
/**
* @dataProvider dataHttpsToSsh
*/
public function testToSsh($expected, $url)
{
$this->assertSame($expected, $this->changer->toSsh($url));
}
public static function dataSshToHttps()
{
return [
'empty-url' => [
'expected' => '',
'url' => '',
],
'matching-spip-url' => [
'expected' => 'https://git.spip.net/owner/package.git',
'url' => 'git@git.spip.net:owner/package.git',
],
'non-matching-spip-url' => [
'expected' => 'https://git.spip.net/owner/package.git',
'url' => 'https://git.spip.net/owner/package.git',
],
];
}
/**
* @dataProvider dataSshToHttps
*/
public function testToHttps($expected, $url)
{
$this->assertSame($expected, $this->changer->toHttps($url));
}
}
......@@ -3,16 +3,11 @@
namespace Spip\Test\Composer;
use Composer;
use Composer\Composer as ComposerObject;
use Composer\IO\IOInterface;
use Composer\Package\Package;
use Composer\Package\RootPackage;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Spip\Composer\Classic;
use Spip\Composer\SpipInstaller;
use Spip\Test\Composer\Mocks\SpipClassic;
use Spip\Test\Composer\Mocks\SpipProject;
/**
* @covers Spip\Composer\SpipInstaller
......@@ -34,9 +29,9 @@ class SpipInstallerTest extends TestCase
],
],
]);
$composer->setPackage($package);
$composer->setPackage($package);
$this->installer = new SpipInstaller($io, $composer);
$this->installer = new SpipInstaller($io, $composer);
}
public static function dataPluginTypes()
......@@ -46,7 +41,7 @@ class SpipInstallerTest extends TestCase
'expected' => null,
'name' => 'spip/test',
'type' => 'library',
],
'classic' => [
'expected' => './tmp/__spip_classic__',
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter