[ContributorTools] Make create add autoload if needed [closes #1204]

This commit is contained in:
Tomas Votruba 2019-03-16 16:49:22 +01:00
parent a29cf5d467
commit 8f8778124d
2 changed files with 69 additions and 4 deletions

View File

@ -59,7 +59,8 @@
"Rector\\ContributorTools\\": "packages/ContributorTools/src",
"Rector\\Laravel\\": "packages/Laravel/src",
"Rector\\PhpSpecToPHPUnit\\": "packages/PhpSpecToPHPUnit/src",
"Rector\\Shopware\\": "packages/Shopware/src"
"Rector\\Shopware\\": "packages/Shopware/src",
"Rector\\NetteTesterToPHPUnit\\": "packages/NetteTesterToPHPUnit/src"
}
},
"autoload-dev": {
@ -90,7 +91,8 @@
"Rector\\Laravel\\Tests\\": "packages/Laravel/tests",
"Rector\\PhpSpecToPHPUnit\\Tests\\": "packages/PhpSpecToPHPUnit/tests",
"Rector\\PHPStanExtensions\\": "utils/PHPStanExtensions/src",
"Rector\\Shopware\\Tests\\": "packages/Shopware/tests"
"Rector\\Shopware\\Tests\\": "packages/Shopware/tests",
"Rector\\NetteTesterToPHPUnit\\Tests\\": "packages/NetteTesterToPHPUnit/tests"
},
"classmap": [
"packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource",
@ -106,7 +108,12 @@
"symplify/easy-coding-standard": "Required to enable '--with-style' option. Use in case you don't have PHP_CodeSniffer or PHP-CS-Fixer yet."
},
"scripts": {
"complete-check": ["@check-cs", "phpunit", "@phpstan", "@update-docs"],
"complete-check": [
"@check-cs",
"phpunit",
"@phpstan",
"@update-docs"
],
"check-cs": "vendor/bin/ecs check bin packages src tests utils",
"fix-cs": [
"vendor/bin/ecs check bin packages src tests utils --fix",
@ -124,4 +131,4 @@
"config": {
"sort-packages": true
}
}
}

View File

@ -3,6 +3,7 @@
namespace Rector\ContributorTools\Command;
use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use Rector\ContributorTools\Configuration\Configuration;
use Rector\ContributorTools\Configuration\ConfigurationFactory;
@ -84,6 +85,9 @@ final class CreateRectorCommand extends Command implements ContributorCommandInt
$configuration = $this->configurationFactory->createFromConfigFile(getcwd() . '/create-rector.yaml');
$templateVariables = $this->templateVariablesFactory->createFromConfiguration($configuration);
// setup psr-4 autoload, if not already in
$this->processComposerAutoload($templateVariables);
foreach ($this->findTemplateFileInfos() as $smartFileInfo) {
$destination = $this->resolveDestination($smartFileInfo, $templateVariables, $configuration);
@ -209,4 +213,58 @@ final class CreateRectorCommand extends Command implements ContributorCommandInt
{
return str_replace(array_keys($variables), array_values($variables), $content);
}
/**
* @param mixed[] $templateVariables
*/
private function processComposerAutoload(array $templateVariables): void
{
$composerJsonContent = FileSystem::read(getcwd() . '/composer.json');
$composerJson = Json::decode($composerJsonContent, Json::FORCE_ARRAY);
$package = $templateVariables['_Package_'];
// already autoloaded?
if ($package === 'Rector') {
return;
}
$package = $templateVariables['_Package_'];
$namespace = 'Rector\\' . $package . '\\';
$namespaceTest = 'Rector\\' . $package . '\\Tests\\';
// already autoloaded?
if (isset($composerJson['autoload']['psr-4'][$namespace])) {
return;
}
$composerJson['autoload']['psr-4'][$namespace] = 'packages/' . $package . '/src';
$composerJson['autoload-dev']['psr-4'][$namespaceTest] = 'packages/' . $package . '/tests';
$composerJsonContent = Json::encode($composerJson, Json::PRETTY);
$composerJsonContent = $this->inlineSections($composerJsonContent, ['keywords', 'bin']);
// inline short arrays
FileSystem::write(getcwd() . '/composer.json', $composerJsonContent);
}
/**
* @param string[] $sections
*/
private function inlineSections(string $jsonContent, array $sections): string
{
foreach ($sections as $section) {
$pattern = '#("' . preg_quote($section, '#') . '": )\[(.*?)\](,)#ms';
$jsonContent = Strings::replace($jsonContent, $pattern, function (array $match) {
$inlined = Strings::replace($match[2], '#\s+#', ' ');
$inlined = trim($inlined);
$inlined = '[' . $inlined . ']';
return $match[1] . $inlined . $match[3];
});
}
return $jsonContent;
}
}