feat(main-config.json): usse it

master
dufraissejeremy 2 years ago
parent 4826be297c
commit ddddfacb77
  1. 15
      src/LocalInstaller.php
  2. 163
      src/Plugin.php

@ -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
{
}

@ -11,8 +11,11 @@ use Composer\Composer;
use Composer\Config; use Composer\Config;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\IO\IOInterface; 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\Handler;
use Seacms\ComposerInstaller\LocalInstaller;
use Seacms\ComposerInstaller\NotFoundFileException; use Seacms\ComposerInstaller\NotFoundFileException;
use TopFloor\ComposerCleanupVcsDirs\Plugin as ParentPlugin; use TopFloor\ComposerCleanupVcsDirs\Plugin as ParentPlugin;
@ -44,7 +47,8 @@ class Plugin extends ParentPlugin
$this->io = $io; $this->io = $io;
$this->handler = new Handler($composer, $io); $this->handler = new Handler($composer, $io);
try { 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)); // $composer->getInstallationManager()->addInstaller(new LocalInstaller($io,$composer));
} catch (NotFoundFileException $ex){ } catch (NotFoundFileException $ex){
// do nothing // do nothing
@ -71,41 +75,21 @@ class Plugin extends ParentPlugin
} }
/** /**
* update config with configFile * update updateRepositoryManager with configFile
* @param array $config * @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'])){ $correspondances = $this->extractAvailableCorrespondances($config);
$composerConfig = $this->composer->getConfig(); if (!empty($correspondances)){
foreach($config['config']['local-repositories'] as $name => $packageName){ $repositoryManager = $this->composer->getRepositoryManager();
if (is_string($name) && !empty(basename($name)) $repos = $repositoryManager->getRepositories();
&& is_string($packageName) && !empty($packageName) && !in_array(basename($name),['.','..'])){ foreach ($repos as $repo) {
$projectName = basename($name); if ($repo instanceof VcsRepository){
$dirName = "../$projectName"; try {
if (is_dir($dirName)){ $this->prependRepoIfFound($repositoryManager,$correspondances,$repo);
$this->io->write("<info>Using local path for '$projectName'</info>"); } catch (Exception $th) {
// $previousRepositories = $composerConfig->all()['repositories']; // do nothing
// $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>");
} }
} }
} }
@ -113,27 +97,28 @@ class Plugin extends ParentPlugin
} }
/** /**
* replace from config repositories vcs by path * prepend new PathRepository if url found in correspondance
* @param Config $composerConfig * @param RepositoryManager $repositoryManager
* @param string $projectName * @param array $correspondances
* @param string $packageName * @param VcsRepository $repo
* @return array $newRepositories * @throws Exception if not found
*/ */
protected function replaceFromConfigWithPath( protected function prependRepoIfFound(
Config $composerConfig, RepositoryManager $repositoryManager,
string $projectName, array $correspondances,
string $packageName): array VcsRepository $repo
)
{ {
$repositories = $composerConfig->all()['repositories']; $url = $this->getVCSUrl($repo);
foreach($repositories as $idx => $data){ list(
if (is_array($data) && isset($data['type']) && $data['type'] === 'vcs' && 'projectName' => $projectName,
isset($data['url']) && is_string($data['url']) && 'packageName'=>$packageName,
substr($data['url'],0,-strlen($projectName)) == 'https://git.defis.info/SeaCMS/' && 'path'=>$path
substr($data['url'],-strlen($projectName)) == $projectName ) = $this->findUrlInCorrespondances($correspondances,$url);
){ $repositoryManager->prependRepository(new PathRepository(
$repositories[$idx] = [ [
'type' => 'path', 'type' => 'path',
'url' => "../$projectName", 'url' => $path,
'options' => [ 'options' => [
'name' => $packageName, 'name' => $packageName,
'versions' => [ 'versions' => [
@ -141,9 +126,81 @@ class Plugin extends ParentPlugin
'symlink' => true '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");
} }
} }
Loading…
Cancel
Save