Skip to content
Snippets Groups Projects

Issue #3409491 add composer dependencies in conversion script

Files
3
@@ -4,7 +4,6 @@ declare(strict_types = 1);
namespace Drupal\automatic_updates\Development;
use Composer\Script\Event;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
@@ -38,8 +37,14 @@ class ConverterCommand extends Command {
private string $core_target_branch;
private string $contrib_dir;
private bool $no_commit;
private bool $skipCoreChecks;
/**
* @inheritDoc
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$this->core_dir = $input->getArgument('core_dir');
@@ -47,44 +52,66 @@ class ConverterCommand extends Command {
$this->contrib_branch = $input->getArgument('contrib_branch');
$this->package_manager_only = $input->getOption('package_manager_only');
$this->core_target_branch = $input->getOption('core_target_branch');
$this->contrib_dir = realpath(__DIR__ . '/../..');
$this->skipCoreChecks = $input->getOption('skip_core_checks');
$this->no_commit = $input->getOption('no_commit');
// Esnure core_dir is a directory.
if (!is_dir($this->core_dir)) {
throw new \Exception("$this->core_dir is not a directory.");
}
// Switch to core_target_branch to ensure it is clean.
$current_directory = getcwd();
chdir($this->core_dir);
static::switchToBranch($this->core_target_branch);
// Git pull to ensure we are up to date.
static::executeCommand('git pull');
// change back the previous directory.
chdir($current_directory);
chdir($this->contrib_dir);
// Ensure we are on the correct branches.
static::switchToBranches($this->core_dir, $this->core_branch, $this->contrib_branch);
// Checkout the composer.json from the core_target_branch.
// Switch to the core directory and checkout the files and folders that this
// conversion script will automatically update based on our composer.json
// file and our dictionary.txt file.
chdir($this->core_dir);
static::executeCommand("git checkout {$this->core_target_branch} -- composer.json");
// Same with composer.lock.
static::executeCommand("git checkout {$this->core_target_branch} -- composer.lock");
// Same with the base composer folder.
static::executeCommand("git checkout {$this->core_target_branch} -- composer");
// Pause for user to press enter.
print "Press enter to continue";
fgets(STDIN);
$this->requirePackageFromModule('require');
$this->requirePackageFromModule('require-dev');
static::executeCommand("git checkout {$this->core_target_branch} -- core/composer.json");
static::executeCommand("git checkout {$this->core_target_branch} -- core/misc/cspell/dictionary.txt");
// Switch to the core directory and require all of the packages there are in
// this module's composer.json require section.
chdir('core');
$require_section = $this->getContribModuleComposerJsonSection('require');
// Hardcode symfony version to 6.4 for core.
$core_symfony_version_update = function ($package_name, $version): string {
if (str_starts_with($package_name, 'symfony/')) {
return '^6.4';
}
return $version;
};
foreach ($require_section as $package_name => $version) {
$version = $core_symfony_version_update($package_name, $version);
static::executeCommand("composer require $package_name:$version --no-update");
}
// Switch back to the parent directory.
chdir('..');
// Run composer update for just this packages.
static::executeCommand("composer update " . implode(' ', array_keys($require_section)));
// print all vars for tests.
print_r(get_object_vars($this));
$require_dev_section = $this->getContribModuleComposerJsonSection('require-dev');
foreach ($require_dev_section as $package_name => $version) {
$version = $core_symfony_version_update($package_name, $version);
static::executeCommand("composer require --dev $package_name:$version");
}
$this->doConvert();
return Command::SUCCESS;
return static::doConvert();
}
/**
* Executes a command and throws an exception if it fails.
* @param string $cmd
*
* @return void
* @param string $cmd
* The command to execute.
*/
private static function executeCommand(string $cmd): void {
$result = NULL;
@@ -95,19 +122,18 @@ class ConverterCommand extends Command {
}
/**
* @inheritDoc
* {@inheritdoc}
*/
protected function configure() {
$this->addArgument('core_dir', InputArgument::REQUIRED, 'The path to the root of Drupal Core');
$this->addArgument('core_branch', InputArgument::REQUIRED, 'The core merge request branch');
$this->addArgument('contrib_branch', InputArgument::OPTIONAL, 'The contrib branch to switch to', '3.0.x');
$this->addOption('package_manager_only', NULL, InputOption::VALUE_NONE, 'Only convert package manager');
$this->addOption('core_target_branch', NULL, InputOption::VALUE_REQUIRED, 'The core target branch', '11.x');
$this->addOption('package_manager_only', NULL, InputOption::VALUE_NONE, 'Only convert package manager');
$this->addOption('core_target_branch', NULL, InputOption::VALUE_REQUIRED, 'The core target branch', '11.x');
$this->addOption('skip_core_checks', NULL, InputOption::VALUE_NONE, 'Skip core checks');
$this->addOption('no_commit', NULL, InputOption::VALUE_NONE, 'Do not make commit');
}
private const RUN_CHECKS = TRUE;
/**
* Prints message.
*
@@ -120,17 +146,10 @@ class ConverterCommand extends Command {
/**
* Converts the contrib module to core merge request.
*
* @param \Composer\Script\Event $event
* The Composer event.
*/
private function doConvert(): void {
$old_machine_name = 'automatic_updates';
$new_machine_name = 'auto_updates';
static::switchToBranches($this->core_dir, $this->core_branch, $this->contrib_branch);
self::info('Switched branches');
$fs = new Filesystem();
@@ -223,18 +242,21 @@ class ConverterCommand extends Command {
chdir($this->core_dir);
if (self::RUN_CHECKS) {
if ($this->skipCoreChecks) {
self::info('⚠️Skipped core checks');
}
else {
static::runCoreChecks($this->core_dir);
self::info('Ran core checks');
}
else {
self::info('⚠️Skipped core checks');
if (!$this->no_commit) {
static::doMakeCommit($this->core_dir);
self::info('Make commit');
self::info("Done. Probably good but you should check before you push. These are the files present in the contrib module absent in core:");
print shell_exec(sprintf("tree %s/package_manager > /tmp/contrib.txt && tree %s/core/modules/package_manager > /tmp/core.txt && diff /tmp/contrib.txt /tmp/core.txt", self::getContribDir(), $this->core_dir));
self::info('(Run diff /tmp/contrib.txt /tmp/core.txt to see that with color.');
}
static::doMakeCommit($this->core_dir);
self::info('Make commit');
self::info("Done. Probably good but you should check before you push. These are the files present in the contrib module absent in core:");
print shell_exec(sprintf("tree %s/package_manager > /tmp/contrib.txt && tree %s/core/modules/package_manager > /tmp/core.txt && diff /tmp/contrib.txt /tmp/core.txt", self::getContribDir(), $core_dir));
self::info('(Run diff /tmp/contrib.txt /tmp/core.txt to see that with color.');
}
/**
@@ -616,26 +638,9 @@ class ConverterCommand extends Command {
static::replaceContents([new \SplFileInfo($new_test)], 'action', $module_name);
}
/**
* @return void
* @throws \Exception
*/
protected function requirePackageFromModule(string $section): void {
// Switch to the core directory and require all of the packages there are in
// this module's composer.json require section.
chdir('core');
$composer_json = json_decode(file_get_contents('composer.json'), TRUE);
$require = $composer_json[$section];
// Get all the packages that are required in this module.
$packages = array_keys($require);
// Composer require them.
foreach ($packages as $package) {
static::executeCommand("composer $section $package --no-update");
}
// Switch back to the parent directory.
chdir('..');
// Run composer update for just this packages.
static::executeCommand("composer update " . implode(' ', $packages));
private function getContribModuleComposerJsonSection(string $section): array {
$composer_json = json_decode(file_get_contents($this->contrib_dir . '/composer.json'), TRUE);
return $composer_json[$section];
}
}
Loading