feat(main-config.json): usse it

This commit is contained in:
Jérémy Dufraisse 2023-03-13 18:31:59 +01:00
parent 4826be297c
commit ddddfacb77
2 changed files with 119 additions and 77 deletions

View File

@ -1,15 +0,0 @@
<?php
/**
* SPDX-License-Identifier: EUPL-1.2
* Authors: see /README.md
*/
namespace Seacms\ComposerInstaller;
use Composer\Installer\LibraryInstaller;
class LocalInstaller extends LibraryInstaller
{
}

View File

@ -11,8 +11,11 @@ use Composer\Composer;
use Composer\Config;
use Composer\Json\JsonFile;
use Composer\IO\IOInterface;
use Composer\Repository\PathRepository;
use Composer\Repository\RepositoryManager;
use Composer\Repository\VcsRepository;
use Exception;
use Seacms\ComposerInstaller\Handler;
use Seacms\ComposerInstaller\LocalInstaller;
use Seacms\ComposerInstaller\NotFoundFileException;
use TopFloor\ComposerCleanupVcsDirs\Plugin as ParentPlugin;
@ -44,7 +47,8 @@ class Plugin extends ParentPlugin
$this->io = $io;
$this->handler = new Handler($composer, $io);
try {
// list('config' => $config,'path' => $path) = $this->getConfigJsonFile();
list('config' => $config,'path' => $path) = $this->getConfigJsonFile();
$this->updateRepositoryManager($config);
// $composer->getInstallationManager()->addInstaller(new LocalInstaller($io,$composer));
} catch (NotFoundFileException $ex){
// do nothing
@ -71,79 +75,132 @@ class Plugin extends ParentPlugin
}
/**
* update config with configFile
* update updateRepositoryManager with configFile
* @param array $config
*/
protected function updateConfig(array $config)
protected function updateRepositoryManager(array $config)
{
if (isset($config['config']['local-repositories']) && is_array($config['config']['local-repositories'])){
$composerConfig = $this->composer->getConfig();
foreach($config['config']['local-repositories'] as $name => $packageName){
if (is_string($name) && !empty(basename($name))
&& is_string($packageName) && !empty($packageName) && !in_array(basename($name),['.','..'])){
$projectName = basename($name);
$dirName = "../$projectName";
if (is_dir($dirName)){
$this->io->write("<info>Using local path for '$projectName'</info>");
// $previousRepositories = $composerConfig->all()['repositories'];
// $this->io->write("<info>previous : ".json_encode($previousRepositories)."</info>");
// $newRepositories = $this->replaceFromConfigWithPath($composerConfig,$projectName,$packageName);
// $this->io->write("<info>new : ".json_encode($newRepositories)."</info>");
$composerConfig->merge([
'repositories' => [
$packageName => [
'type' => 'path',
'url' => "../$projectName",
'options' => [
'name' => $packageName,
'versions' => [
$packageName => 'dev-master',
'symlink' => true
]
]
]
]
]);
$previousRepositories = $composerConfig->all()['repositories'];
$this->io->write("<info>previous : ".json_encode($previousRepositories)."</info>");
$correspondances = $this->extractAvailableCorrespondances($config);
if (!empty($correspondances)){
$repositoryManager = $this->composer->getRepositoryManager();
$repos = $repositoryManager->getRepositories();
foreach ($repos as $repo) {
if ($repo instanceof VcsRepository){
try {
$this->prependRepoIfFound($repositoryManager,$correspondances,$repo);
} catch (Exception $th) {
// do nothing
}
}
}
}
}
/**
* replace from config repositories vcs by path
* @param Config $composerConfig
* @param string $projectName
* @param string $packageName
* @return array $newRepositories
* prepend new PathRepository if url found in correspondance
* @param RepositoryManager $repositoryManager
* @param array $correspondances
* @param VcsRepository $repo
* @throws Exception if not found
*/
protected function replaceFromConfigWithPath(
Config $composerConfig,
string $projectName,
string $packageName): array
protected function prependRepoIfFound(
RepositoryManager $repositoryManager,
array $correspondances,
VcsRepository $repo
)
{
$repositories = $composerConfig->all()['repositories'];
foreach($repositories as $idx => $data){
if (is_array($data) && isset($data['type']) && $data['type'] === 'vcs' &&
isset($data['url']) && is_string($data['url']) &&
substr($data['url'],0,-strlen($projectName)) == 'https://git.defis.info/SeaCMS/' &&
substr($data['url'],-strlen($projectName)) == $projectName
){
$repositories[$idx] = [
'type' => 'path',
'url' => "../$projectName",
'options' => [
'name' => $packageName,
'versions' => [
$packageName => 'dev-master',
'symlink' => true
]
$url = $this->getVCSUrl($repo);
list(
'projectName' => $projectName,
'packageName'=>$packageName,
'path'=>$path
) = $this->findUrlInCorrespondances($correspondances,$url);
$repositoryManager->prependRepository(new PathRepository(
[
'type' => 'path',
'url' => $path,
'options' => [
'name' => $packageName,
'versions' => [
$packageName => 'dev-master',
'symlink' => true
]
];
]
],
$this->io,
$this->composer->getConfig()
));
$this->io->write("Using symlink \"$path\" to replace VCS $url");
}
/**
* extract available folder from main-config.json
* @param array $config
* @return array [$projectName => ['packageName' => string, 'path' => string ],...]
*/
protected function extractAvailableCorrespondances(array $config): array
{
$correspondances = [];
if (isset($config['local-repositories']) && is_array($config['local-repositories'])){
foreach($config['local-repositories'] as $name => $packageName){
if (is_string($name)
&& !empty(basename($name))
&& is_string($packageName)
&& !empty($packageName)
&& !in_array(basename($name),['.','..'])){
$projectName = basename($name);
$dirName = "../$projectName";
if (is_dir($dirName)){
$correspondances[$projectName] = [
'packageName' => $packageName,
'path' => $dirName
];
}
}
}
}
return $repositories;
return $correspondances;
}
/**
* getUrlFromVcsRepo
* @param VcsRepository $repo
* @return string
* @throws Exception
*/
protected function getVCSUrl(VcsRepository $repo): string
{
$repoName = $repo->getRepoName();
$start = 'vcs repo (';
$end = ')';
if (substr($repoName,0,strlen($start)) != $start || substr($repoName,-strlen($end)) != $end){
throw new Exception("VCS' repoName format not recognized !");
}
$url = substr($repoName,strlen($start),-strlen($end));
if (strpos($url,' ') === false){
throw new Exception("VCS' repoName format not recognized !");
}
list($driverType,$url) = explode(' ',$url,2);
return $url;
}
/**
* search url in corespondances
* @param array $correspondances
* @param string $url
* @return array
* @throws Exception
*/
protected function findUrlInCorrespondances(array $correspondances, string $url): array
{
foreach ($correspondances as $projectName => $data) {
if (substr($url,-strlen($projectName)-1) == "/$projectName"){
return array_merge([
'projectName' => $projectName
],$data);
}
}
throw new Exception("Correspondance not found");
}
}