Updated Rector to commit 86c0920bdf0222f3c3e6ddf8490804f428a39fae

86c0920bdf [Init] Add smart paths detection to init command to make first Rector experience better (#3050)
This commit is contained in:
Tomas Votruba 2022-11-11 14:53:57 +00:00
parent 44bd6589c7
commit 9a86942399
9 changed files with 90 additions and 48 deletions

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'e3ff080a96e2efbcd4a62c07b44ef4f950bd023d';
public const PACKAGE_VERSION = '86c0920bdf0222f3c3e6ddf8490804f428a39fae';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-11-11 13:38:29';
public const RELEASE_DATE = '2022-11-11 14:49:31';
/**
* @var int
*/

View File

@ -134,11 +134,6 @@ final class Option
* @var string
*/
public const NO_DIFFS = 'no-diffs';
/**
* @deprecated This know-how should be mentioned in framework-specific documentation of the package instead.
* @var string
*/
public const TEMPLATE_TYPE = 'template-type';
/**
* @var string
*/

View File

@ -5,14 +5,12 @@ namespace Rector\Core\Console\Command;
use RectorPrefix202211\Nette\Utils\FileSystem;
use RectorPrefix202211\Nette\Utils\Strings;
use Rector\Core\Configuration\Option;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\FileSystem\InitFilePathsResolver;
use Rector\Core\Php\PhpVersionProvider;
use RectorPrefix202211\Symfony\Component\Console\Command\Command;
use RectorPrefix202211\Symfony\Component\Console\Input\InputInterface;
use RectorPrefix202211\Symfony\Component\Console\Input\InputOption;
use RectorPrefix202211\Symfony\Component\Console\Output\OutputInterface;
use RectorPrefix202211\Symfony\Component\Console\Style\SymfonyStyle;
final class InitCommand extends Command
{
/**
@ -36,45 +34,56 @@ final class InitCommand extends Command
private $phpVersionProvider;
/**
* @readonly
* @var \Symfony\Component\Console\Style\SymfonyStyle
* @var \Rector\Core\FileSystem\InitFilePathsResolver
*/
private $symfonyStyle;
public function __construct(\RectorPrefix202211\Symfony\Component\Filesystem\Filesystem $filesystem, OutputStyleInterface $rectorOutputStyle, PhpVersionProvider $phpVersionProvider, SymfonyStyle $symfonyStyle)
private $initFilePathsResolver;
public function __construct(\RectorPrefix202211\Symfony\Component\Filesystem\Filesystem $filesystem, OutputStyleInterface $rectorOutputStyle, PhpVersionProvider $phpVersionProvider, InitFilePathsResolver $initFilePathsResolver)
{
$this->filesystem = $filesystem;
$this->rectorOutputStyle = $rectorOutputStyle;
$this->phpVersionProvider = $phpVersionProvider;
$this->symfonyStyle = $symfonyStyle;
$this->initFilePathsResolver = $initFilePathsResolver;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$projectDirectory = \getcwd();
$rectorRootFilePath = $projectDirectory . '/rector.php';
$doesFileExist = $this->filesystem->exists($rectorRootFilePath);
if ($doesFileExist) {
$this->rectorOutputStyle->note('Config file "rector.php" already exists');
} else {
$rectorPhpTemplateContents = FileSystem::read(self::TEMPLATE_PATH);
$rectorPhpTemplateContents = $this->replacePhpLevelContents($rectorPhpTemplateContents);
$rectorPhpTemplateContents = $this->replacePathsContents($rectorPhpTemplateContents, $projectDirectory);
$this->filesystem->dumpFile($rectorRootFilePath, $rectorPhpTemplateContents);
$this->rectorOutputStyle->success('"rector.php" config file was added');
}
return Command::SUCCESS;
}
protected function configure() : void
{
$this->setName('init');
$this->setDescription('Generate rector.php configuration file');
// deprecated
$this->addOption(Option::TEMPLATE_TYPE, null, InputOption::VALUE_OPTIONAL, 'A template type like default, nette, doctrine etc.');
}
protected function execute(InputInterface $input, OutputInterface $output) : int
private function replacePhpLevelContents(string $rectorPhpTemplateContents) : string
{
$templateType = (string) $input->getOption(Option::TEMPLATE_TYPE);
if ($templateType !== '') {
// notice warning
$this->symfonyStyle->warning('The option "--type" is deprecated. Custom config should be part of project documentation instead.');
\sleep(3);
$fullPHPVersion = (string) $this->phpVersionProvider->provide();
$phpVersion = Strings::substring($fullPHPVersion, 0, 1) . Strings::substring($fullPHPVersion, 2, 1);
return \str_replace('LevelSetList::UP_TO_PHP_XY', 'LevelSetList::UP_TO_PHP_' . $phpVersion, $rectorPhpTemplateContents);
}
private function replacePathsContents(string $rectorPhpTemplateContents, string $projectDirectory) : string
{
$projectPhpDirectories = $this->initFilePathsResolver->resolve($projectDirectory);
// fallback to default 'src' in case of empty one
if ($projectPhpDirectories === []) {
$projectPhpDirectories[] = 'src';
}
$rectorRootFilePath = \getcwd() . '/rector.php';
$doesFileExist = $this->filesystem->exists($rectorRootFilePath);
if ($doesFileExist) {
$this->rectorOutputStyle->warning('Config file "rector.php" already exists');
} else {
$this->filesystem->copy(self::TEMPLATE_PATH, $rectorRootFilePath);
$fullPHPVersion = (string) $this->phpVersionProvider->provide();
$phpVersion = Strings::substring($fullPHPVersion, 0, 1) . Strings::substring($fullPHPVersion, 2, 1);
$fileContent = FileSystem::read($rectorRootFilePath);
$fileContent = \str_replace('LevelSetList::UP_TO_PHP_XY', 'LevelSetList::UP_TO_PHP_' . $phpVersion, $fileContent);
$this->filesystem->dumpFile($rectorRootFilePath, $fileContent);
$this->rectorOutputStyle->success('"rector.php" config file was added');
$projectPhpDirectoriesContents = '';
foreach ($projectPhpDirectories as $projectPhpDirectory) {
$projectPhpDirectoriesContents .= " __DIR__ . '/" . $projectPhpDirectory . "'," . \PHP_EOL;
}
return Command::SUCCESS;
$projectPhpDirectoriesContents = \rtrim($projectPhpDirectoriesContents);
return \str_replace('__PATHS__', $projectPhpDirectoriesContents, $rectorPhpTemplateContents);
}
}

View File

@ -0,0 +1,36 @@
<?php
declare (strict_types=1);
namespace Rector\Core\FileSystem;
use RectorPrefix202211\Symfony\Component\Finder\Finder;
use RectorPrefix202211\Symfony\Component\Finder\SplFileInfo;
/**
* @see \Rector\Core\Tests\FileSystem\InitFilePathsResolver\InitFilePathsResolverTest
*/
final class InitFilePathsResolver
{
/**
* @return string[]
*/
public function resolve(string $projectDirectory) : array
{
$rootDirectoryFinder = Finder::create()->directories()->depth(0)->notPath('#(vendor|var|stubs|temp|templates|tmp|e2e|bin|build)#')->in($projectDirectory)->sortByName();
/** @var SplFileInfo[] $rootDirectoryFileInfos */
$rootDirectoryFileInfos = \iterator_to_array($rootDirectoryFinder);
$projectDirectories = [];
foreach ($rootDirectoryFileInfos as $rootDirectoryFileInfo) {
if (!$this->hasDirectoryFileInfoPhpFiles($rootDirectoryFileInfo)) {
continue;
}
$projectDirectories[] = $rootDirectoryFileInfo->getRelativePathname();
}
return $projectDirectories;
}
private function hasDirectoryFileInfoPhpFiles(SplFileInfo $rootDirectoryFileInfo) : bool
{
// is directory with PHP files?
$phpFilesFinder = Finder::create()->files()->in($rootDirectoryFileInfo->getPathname())->name('*.php');
return \count($phpFilesFinder) !== 0;
}
}

View File

@ -8,7 +8,7 @@ use Rector\Set\ValueObject\LevelSetList;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src'
__PATHS__
]);
// register a single rule

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit87ee7d81f16938e812ed5860b53df937::getLoader();
return ComposerAutoloaderInit0bea191d0981c9c87a1f32d396d321b7::getLoader();

View File

@ -1371,6 +1371,7 @@ return array(
'Rector\\Core\\FileSystem\\FilePathHelper' => $baseDir . '/src/FileSystem/FilePathHelper.php',
'Rector\\Core\\FileSystem\\FilesFinder' => $baseDir . '/src/FileSystem/FilesFinder.php',
'Rector\\Core\\FileSystem\\FilesystemTweaker' => $baseDir . '/src/FileSystem/FilesystemTweaker.php',
'Rector\\Core\\FileSystem\\InitFilePathsResolver' => $baseDir . '/src/FileSystem/InitFilePathsResolver.php',
'Rector\\Core\\FileSystem\\PhpFilesFinder' => $baseDir . '/src/FileSystem/PhpFilesFinder.php',
'Rector\\Core\\Kernel\\ContainerBuilderFactory' => $baseDir . '/src/Kernel/ContainerBuilderFactory.php',
'Rector\\Core\\Kernel\\RectorKernel' => $baseDir . '/src/Kernel/RectorKernel.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit87ee7d81f16938e812ed5860b53df937
class ComposerAutoloaderInit0bea191d0981c9c87a1f32d396d321b7
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit87ee7d81f16938e812ed5860b53df937
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit87ee7d81f16938e812ed5860b53df937', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit0bea191d0981c9c87a1f32d396d321b7', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit87ee7d81f16938e812ed5860b53df937', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit0bea191d0981c9c87a1f32d396d321b7', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit87ee7d81f16938e812ed5860b53df937::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit0bea191d0981c9c87a1f32d396d321b7::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit87ee7d81f16938e812ed5860b53df937::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInit0bea191d0981c9c87a1f32d396d321b7::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire87ee7d81f16938e812ed5860b53df937($fileIdentifier, $file);
composerRequire0bea191d0981c9c87a1f32d396d321b7($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit87ee7d81f16938e812ed5860b53df937
* @param string $file
* @return void
*/
function composerRequire87ee7d81f16938e812ed5860b53df937($fileIdentifier, $file)
function composerRequire0bea191d0981c9c87a1f32d396d321b7($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit87ee7d81f16938e812ed5860b53df937
class ComposerStaticInit0bea191d0981c9c87a1f32d396d321b7
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1621,6 +1621,7 @@ class ComposerStaticInit87ee7d81f16938e812ed5860b53df937
'Rector\\Core\\FileSystem\\FilePathHelper' => __DIR__ . '/../..' . '/src/FileSystem/FilePathHelper.php',
'Rector\\Core\\FileSystem\\FilesFinder' => __DIR__ . '/../..' . '/src/FileSystem/FilesFinder.php',
'Rector\\Core\\FileSystem\\FilesystemTweaker' => __DIR__ . '/../..' . '/src/FileSystem/FilesystemTweaker.php',
'Rector\\Core\\FileSystem\\InitFilePathsResolver' => __DIR__ . '/../..' . '/src/FileSystem/InitFilePathsResolver.php',
'Rector\\Core\\FileSystem\\PhpFilesFinder' => __DIR__ . '/../..' . '/src/FileSystem/PhpFilesFinder.php',
'Rector\\Core\\Kernel\\ContainerBuilderFactory' => __DIR__ . '/../..' . '/src/Kernel/ContainerBuilderFactory.php',
'Rector\\Core\\Kernel\\RectorKernel' => __DIR__ . '/../..' . '/src/Kernel/RectorKernel.php',
@ -3053,9 +3054,9 @@ class ComposerStaticInit87ee7d81f16938e812ed5860b53df937
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit87ee7d81f16938e812ed5860b53df937::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit87ee7d81f16938e812ed5860b53df937::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit87ee7d81f16938e812ed5860b53df937::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit0bea191d0981c9c87a1f32d396d321b7::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit0bea191d0981c9c87a1f32d396d321b7::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit0bea191d0981c9c87a1f32d396d321b7::$classMap;
}, null, ClassLoader::class);
}