Skip to content

Commit

Permalink
Remove patches (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ol0lll authored Aug 28, 2024
1 parent adfb1b6 commit 4edda9a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 100 deletions.
183 changes: 110 additions & 73 deletions BackendCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@ public function __construct()
$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 __construct()
public function preInstallCommand(CommandData $commandData)
{
$this->populateConfigSyncDirectory();

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

/**
Expand All @@ -71,12 +99,11 @@ public function preInstallCommand(CommandData $commandData)
*/
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 install()
*/
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 @@ public function updateDatabase()
* @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 @@ public function additionalConfigExportOption(Command $command, AnnotationData $a
* @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 @@ public function preConfigImportCommand(CommandData $commandData)
$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,23 @@ public function createTestingDump()
{
$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);
} else {
unlink($defaultSettingsFile);
}
}

/**
Expand Down Expand Up @@ -396,35 +434,6 @@ protected function populateConfigSyncDirectory(): void
}
}

/**
* 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 +489,32 @@ private function filesAreEqual($firstFile, $secondFile): bool

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

0 comments on commit 4edda9a

Please sign in to comment.