Skip to content
Snippets Groups Projects

Issue #3316721: Add script to convert this module into a core merge request

Merged Issue #3316721: Add script to convert this module into a core merge request
2 unresolved threads
2 unresolved threads
1 file
+ 55
17
Compare changes
  • Side-by-side
  • Inline
+ 55
17
@@ -10,11 +10,23 @@ use Symfony\Component\Filesystem\Filesystem;
*/
class Converter {
private static function info(string $msg) {
/**
* Prints message.
*
* @param string $msg
* The message to print.
*/
private static function info(string $msg): void {
print "\n$msg";
}
public static function doConvert(Event $event) {
/**
* Converts the contrib module to core merge request.
*
* @param \Composer\Script\Event $event
* The Composer event.
*/
public static function doConvert(Event $event): void {
$args = $event->getArguments();
if (count($args) !== 2) {
throw new \Exception("This scripts 2 arguments, a directory that is a core clone and the branch.");
@@ -33,7 +45,7 @@ class Converter {
$core_module_path = static::getCoreModulePath($core_dir);
$package_manager_core_path = $core_dir . "/core/modules/package_manager";
// Remove old module
// Remove old module.
$fs->remove($core_module_path);
self::info('Removed old core module');
$fs->remove($package_manager_core_path);
@@ -42,7 +54,7 @@ class Converter {
$fs->mirror(self::getContribDir(), $core_module_path);
self::info('Mirrored into core module');
// Remove unneeded
// Remove unneeded.
$removals = [
'package_manager/package_manager.install',
'automatic_updates_9_3_shim',
@@ -90,20 +102,28 @@ class Converter {
self::info('Ran core checks');
static::makeCommit($core_dir);
self::info('Make commit');
/**
* @todo Commit with the specific commit from contrib.
*/
// @todo Commit with the specific commit from contrib.
self::info("Done. Probably good but you should check before you push.");
}
private static function getContribDir() {
/**
* Returns the path to the root of the contrib module.
*
* @return string
* The full path to the root of the contrib module.
*/
private static function getContribDir(): string {
return realpath(__DIR__ . '/../..');
}
/**
* @param $core_dir
* Returns the path where the contrib module will be placed in Drupal Core.
*
* @param string $core_dir
* The path to the root of Drupal Core.
*
* @return string
* The path where the contrib module will be placed in Drupal Core
*/
private static function getCoreModulePath(string $core_dir): string {
return $core_dir . '/core/modules/auto_updates';
@@ -112,33 +132,39 @@ class Converter {
/**
* Replaces a string in the contents of the module files.
*
* @param string $core_module_path
* The path of module in Drupal Core.
* @param string $search
* The string to be replaced.
* @param string $replace
* The string to replace.
*/
private static function replaceContents(string $core_module_path, string $search, string $replace) {
private static function replaceContents(string $core_module_path, string $search, string $replace): void {
$files = static::getDirContents($core_module_path, TRUE);
foreach ($files as $file) {
$filePath = $file->getRealPath();
file_put_contents($filePath, str_replace($search, $replace, file_get_contents($filePath)));
}
}
/**
* Renames the module files.
*
* @param string $core_module_path
* The path of module in Drupal Core.
* @param string $old_pattern
* The old file name.
* @param string $new_pattern
* The new file name.
*/
private static function renameFiles(string $core_module_path, string $old_pattern, string $new_pattern) {
private static function renameFiles(string $core_module_path, string $old_pattern, string $new_pattern): void {
$files = static::getDirContents($core_module_path);
// Keep a record of the files and directories to change.
// We will change all the files first so we don't change the location of any
// of the files in the middle.
// This probably won't work if we had nested folders with the pattern on 2
// folder levels but we don't.
// We will change all the files first, so we don't change the location of
// any files in the middle. This probably won't work if we had nested
// folders with the pattern on 2 folder levels, but we don't.
$filesToChange = [];
$dirsToChange = [];
foreach ($files as $file) {
@@ -182,11 +208,14 @@ class Converter {
* Gets the contents of a directory.
*
* @param string $path
* The path of the directory.
* @param bool $excludeDirs
* (optional) If TRUE, all directories will be excluded. Defaults to FALSE.
*
* @return \SplFileInfo[]
* Array of objects containing file information.
*/
private static function getDirContents(string $path, $excludeDirs = FALSE): array {
private static function getDirContents(string $path, bool $excludeDirs = FALSE): array {
$rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
$files = [];
@@ -254,6 +283,9 @@ class Converter {
}
}
/**
*
*/
private static function makeCommit(string $core_dir) {
chdir(self::getContribDir());
self::ensureGitClean();
@@ -264,6 +296,9 @@ class Converter {
shell_exec("git commit -m 'Contrib: $message - https://git.drupalcode.org/project/automatic_updates/-/commit/$hash'");
}
/**
*
*/
private static function addWordsToDictionary(string $core_dir, array $new_words) {
$dict_file = $core_dir . '/core/misc/cspell/dictionary.txt';
$contents = file_get_contents($dict_file);
@@ -280,6 +315,9 @@ class Converter {
}
/**
*
*/
private static function runCoreChecks(string $core_dir) {
chdir($core_dir);
$output = NULL;
Loading