feat(Config): update repositories WIP

master
dufraissejeremy 2 years ago
parent 9067b4dd5c
commit 363d8c7491
  1. 4
      composer.json
  2. 17
      src/NotFoundFileException.php
  3. 137
      src/Plugin.php

@ -28,7 +28,7 @@
}, },
"extra": { "extra": {
"class": "Seacms\\ComposerInstaller\\Plugin", "class": "Seacms\\ComposerInstaller\\Plugin",
"plugin-modifies-downloads": true, "plugin-modifies-downloads": false,
"plugin-modifies-install-path": true "plugin-modifies-install-path": false
} }
} }

@ -0,0 +1,17 @@
<?php
/**
* SPDX-License-Identifier: EUPL-1.2
* Authors: see /README.md
*/
namespace Seacms\ComposerInstaller;
use Exception;
/**
* Exception when file not found
*/
class NotFoundFileException extends Exception
{}

@ -8,15 +8,46 @@
namespace Seacms\ComposerInstaller; namespace Seacms\ComposerInstaller;
use Composer\Composer; use Composer\Composer;
use Composer\Config;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\Json\JsonFile;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface; use Composer\Plugin\PluginInterface;
use Seacms\ComposerInstaller\NotFoundFileException;
/** /**
* Composer plugin to install or not packages from local path * Composer plugin to install or not packages from local path
*/ */
class Plugin implements PluginInterface class Plugin implements PluginInterface, EventSubscriberInterface
{ {
/**
* composer instance
* @var Composer
*/
protected $composer;
/**
* io instance
* @var IOInterface
*/
protected $io;
/**
* Returns an array of event names this subscriber wants to listen to.
* @return array<string, string|array{0: string, 1?: int}|array<array{0: string, 1?: int}>> The event names to listen to
*/
public static function getSubscribedEvents(){
return [
PackageEvents::PRE_PACKAGE_UPDATE => 'prePackageUpdate',
];
}
public function prePackageUpdate(PackageEvent $event)
{
$event->getIO()->write("<info>Test : {$event->getOperation()}</info>");
}
/** /**
* Apply plugin modifications to Composer * Apply plugin modifications to Composer
@ -24,7 +55,13 @@ class Plugin implements PluginInterface
* @return void * @return void
*/ */
public function activate(Composer $composer, IOInterface $io){ public function activate(Composer $composer, IOInterface $io){
$this->composer = $composer;
$this->io = $io;
try {
$this->updateConfig($this->getConfigJsonFile());
} catch (NotFoundFileException $ex){
// do nothing
}
} }
/** /**
@ -50,4 +87,100 @@ class Plugin implements PluginInterface
public function uninstall(Composer $composer, IOInterface $io){ public function uninstall(Composer $composer, IOInterface $io){
} }
/**
* get config.json File from root path
* @return array ['config' => $config,'path'=>path]
* @throws NotFoundFileException
*/
protected function getConfigJsonFile(): array
{
$file = new JsonFile('./main-config.json');
if (!$file->exists()){
throw new NotFoundFileException("main-config.json file not existing");
}
$config = $file->read();
if (!is_array($config)){
$this->io->write('<error>Error loading main-config.json : it should contain an array</error>');
throw new NotFoundFileException("main-config.json file not existing");
}
return ['config'=>$config,'path'=>$file->getPath()];
}
/**
* update config with configFile
* @param array $config
*/
protected function updateConfig(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>");
}
}
}
}
}
/**
* replace from config repositories vcs by path
* @param Config $composerConfig
* @param string $projectName
* @param string $packageName
* @return array $newRepositories
*/
protected function replaceFromConfigWithPath(
Config $composerConfig,
string $projectName,
string $packageName): array
{
$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
]
]
];
}
}
return $repositories;
}
} }
Loading…
Cancel
Save