From ddddfacb77eb54f7f2b4d66c72deaca5e23d5017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Mon, 13 Mar 2023 18:31:59 +0100 Subject: [PATCH] feat(main-config.json): usse it --- src/LocalInstaller.php | 15 ---- src/Plugin.php | 181 +++++++++++++++++++++++++++-------------- 2 files changed, 119 insertions(+), 77 deletions(-) delete mode 100644 src/LocalInstaller.php diff --git a/src/LocalInstaller.php b/src/LocalInstaller.php deleted file mode 100644 index fdf2f0b..0000000 --- a/src/LocalInstaller.php +++ /dev/null @@ -1,15 +0,0 @@ -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("Using local path for '$projectName'"); - // $previousRepositories = $composerConfig->all()['repositories']; - // $this->io->write("previous : ".json_encode($previousRepositories).""); - // $newRepositories = $this->replaceFromConfigWithPath($composerConfig,$projectName,$packageName); - // $this->io->write("new : ".json_encode($newRepositories).""); - $composerConfig->merge([ - 'repositories' => [ - $packageName => [ - 'type' => 'path', - 'url' => "../$projectName", - 'options' => [ - 'name' => $packageName, - 'versions' => [ - $packageName => 'dev-master', - 'symlink' => true - ] - ] - ] - ] - ]); - $previousRepositories = $composerConfig->all()['repositories']; - $this->io->write("previous : ".json_encode($previousRepositories).""); + $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"); } } \ No newline at end of file