Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove patches #15

Merged
merged 17 commits into from
Aug 28, 2024
184 changes: 111 additions & 73 deletions BackendCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@
$this->filesystem = new Filesystem();
}

/**
* @hook init @options-backend
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function initCommands(InputInterface $input, AnnotationData $annotationData)
{
$this->projectDirectory = $input->getOption('project-directory') ?: Drush::bootstrapManager()->getComposerRoot();
}

/**
* Define default options for most backend commands.
*
* @hook option @options-backend
*
* @option project-directory The base directory of the project. Defaults to composer root of project.
*
* @param \Symfony\Component\Console\Command\Command $command
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function optionsBackend(Command $command, AnnotationData $annotationData)
{
$command->addOption(
'project-directory',
'',
InputOption::VALUE_NONE,
'The base directory of the project. Defaults to composer root of project. Option added by burdastyle backend commands.'
);
}

/**
* Prepare file system and code to be ready for install.
*
Expand All @@ -48,9 +79,6 @@
public function preInstallCommand(CommandData $commandData)
{
$this->populateConfigSyncDirectory();

// Apply core patches
$this->corePatches();
}

/**
Expand All @@ -71,12 +99,11 @@
*/
public function install()
{
// Cleanup existing installation.
$this->drush($this->selfRecord(), 'sql-create', [], ['yes' => $this->input()->getOption('yes')]);
$this->drush($this->selfRecord(), 'cache:rebuild');

// Do the site install
// Do the site install.
$this->drush($this->selfRecord(), 'site:install', [], ['existing-config' => true, 'yes' => $this->input()->getOption('yes')]);

// Clear caches.
$this->drush($this->selfRecord(), 'cache:rebuild');
}

/**
Expand All @@ -89,8 +116,6 @@
*/
public function postInstallCommand($result, CommandData $commandData)
{
// Remove the patch.
$this->corePatches($revert = true);
$this->process(['git', 'checkout', $this->siteDirectory().'/settings.php'], $this->projectDirectory());
}

Expand Down Expand Up @@ -137,7 +162,7 @@
* @param \Symfony\Component\Console\Command\Command $command
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function additionalConfigExportOption(Command $command, AnnotationData $annotationData)
public function additionalConfigExportOptions(Command $command, AnnotationData $annotationData)
{
$command->addOption(
'project-directory',
Expand All @@ -153,7 +178,36 @@
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function initConfigExportCommand(InputInterface $input, AnnotationData $annotationData)
public function initConfigExportCommands(InputInterface $input, AnnotationData $annotationData)
{
$this->initCommands($input, $annotationData);
}

/**
* Add option to command.
*
* @hook option config:import
*
* @param \Symfony\Component\Console\Command\Command $command
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function additionalConfigImportOptions(Command $command, AnnotationData $annotationData)
{
$command->addOption(
'project-directory',
'',
InputOption::VALUE_NONE,
'The base directory of the project. Defaults to composer root of project. Option added by burdastyle backend commands.'
);
}

/**
* @hook init config:import
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function initConfigImportCommands(InputInterface $input, AnnotationData $annotationData)
{
$this->initCommands($input, $annotationData);
}
Expand Down Expand Up @@ -238,36 +292,6 @@
$this->populateConfigSyncDirectory();
}

/**
* Add option to command.
*
* @hook option config:import
*
* @param \Symfony\Component\Console\Command\Command $command
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function additionalConfigImportOption(Command $command, AnnotationData $annotationData)
{
$command->addOption(
'project-directory',
'',
InputOption::VALUE_NONE,
'The base directory of the project. Defaults to composer root of project. Option added by burdastyle backend commands.'
);
}

/**
* @hook init config:import
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function initConfigImportCommand(InputInterface $input, AnnotationData $annotationData)
{
$this->initCommands($input, $annotationData);
}


/**
* Prepare an update branch. Does code update, database update and config export.
*
Expand Down Expand Up @@ -327,9 +351,24 @@
{
$sql = SqlBase::create();
$dbSpec = $sql->getDbSpec();
$dbUrl = $dbSpec['driver'].'://'.$dbSpec['username'].':'.$dbSpec['password'].'@'.$dbSpec['host'].':'.$dbSpec['port'].'/'.$dbSpec['database'];

$this->process(['php', 'core/scripts/db-tools.php', 'dump-database-d8-mysql', '--database-url', $dbUrl], $this->drupalRootDirectory());
// Prepare settings file.
$defaultSettingsFile = $this->drupalRootDirectory().'/sites/default/settings.php';
if (file_exists($defaultSettingsFile)) {
$tmpName = tempnam($this->drupalRootDirectory().'/sites/default/', 'settings.tmp');
rename($defaultSettingsFile, $tmpName);
}
$this->prepareSettingsFile($defaultSettingsFile, $dbSpec);

$this->process(['php', 'core/scripts/db-tools.php', 'dump-database-d8-mysql'], $this->drupalRootDirectory());

// Cleanup settings file.
if (!empty($tmpName)) {
rename($tmpName, $defaultSettingsFile);
}

Check failure on line 368 in BackendCommands.php

View workflow job for this annotation

GitHub Actions / PHPCS

Expected 1 space after closing brace; newline found
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the settingsfile we created, when no previous file existed?

else {
unlink($defaultSettingsFile);
}
}

/**
Expand Down Expand Up @@ -396,35 +435,6 @@
}
}

/**
* Apply or revoke patches to drupal core.
*
* @param bool $revert
*/
protected function corePatches(bool $revert = false)
{
$patches = [
'https://www.drupal.org/files/issues/2020-09-14/3169756-2-11.patch',
'https://www.drupal.org/files/issues/2020-06-03/2488350-3-98.patch',
];

$command = ['patch', '-p1', '--silent'];
if ($revert) {
$command[] = '-R';
$patches = array_reverse($patches);
}

foreach ($patches as $patch) {
$stream = fopen($patch, 'r');
try {
$this->process($command, $this->drupalRootDirectory(), null, $stream);
} catch (\Exception $e) {
$this->logger()->info('A patch was not applied correctly, continuing without this patch.');
}
fclose($stream);
}
}

/**
* Get all config files in a given directory.
*
Expand Down Expand Up @@ -480,4 +490,32 @@

return true;
}

/**
* Generates default settings file with current db params.
*
* @param $defaultSettingsFile
* @param $dbSpec
*
* @return void
*/
private function prepareSettingsFile($defaultSettingsFile, $dbSpec)
{

$fileString = <<<EOF
<?php
\$databases['default']['default'] = [
'database' => '{{ database }}',
'username' => '{{ username }}',
'password' => '{{ password }}',
'prefix' => '',
'host' => '{{ host }}',
'port' => '{{ port }}',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => '{{ driver }}',
];
EOF;
$fileString = str_replace(['{{ database }}', '{{ username }}', '{{ password }}', '{{ host }}', '{{ port }}', '{{ driver }}'], [$dbSpec['database'], $dbSpec['username'], $dbSpec['password'], $dbSpec['host'], $dbSpec['port'], $dbSpec['driver']], $fileString);
file_put_contents($defaultSettingsFile, $fileString, FILE_APPEND);
}
}
31 changes: 4 additions & 27 deletions BackendCommandsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Drush\Commands\BurdaStyleGroup;

use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\AnnotatedCommand\CommandError;
use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Drupal\Core\Site\Settings;
use Drush\Drush;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Webmozart\PathUtil\Path;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Path;

/**
* Trait for backend drush commands.
Expand All @@ -26,6 +27,7 @@ trait BackendCommandsTrait
private $siteDomainDirectoryMapping = [
'@elle.dev' => 'elle.de',
'@esquire.dev' => 'esquire.de',
'@focusplus.dev' => 'focusplus.de',
'@freundin.dev' => 'freundin.de',
'@harpersbazaar.dev' => 'harpersbazaar.de',
'@instyle.dev' => 'instyle.de',
Expand All @@ -36,31 +38,6 @@ trait BackendCommandsTrait
*/
private $projectDirectory;

/**
* @hook init
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function initCommands(InputInterface $input, AnnotationData $annotationData)
{
// Initialize project directory.
$this->projectDirectory = $input->getOption('project-directory') ?: Drush::bootstrapManager()->getComposerRoot();
}

/**
* Define default options for most backend commands.
*
* @hook option @options-backend
*
* @option project-directory The base directory of the project. Defaults to composer root of project.
*
* @param array $options
*/
public function optionsBackend($options = ['project-directory' => false])
{
}

/**
* Drush command wrapper.
*
Expand Down
Loading