Skip to content

Commit

Permalink
Add dev commands (#9)
Browse files Browse the repository at this point in the history
* Add dev commands

* remove environment option

* replace backend:cex and backend-cim with default cex/cim
  • Loading branch information
ol0lll authored Mar 17, 2021
1 parent 7980459 commit d851b27
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 113 deletions.
165 changes: 82 additions & 83 deletions BackendCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

namespace Drush\Commands\BurdaStyleGroup;

use Consolidation\AnnotatedCommand\AnnotationData;
use Drupal\Core\Site\Settings;
use Consolidation\AnnotatedCommand\CommandData;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Drush\Sql\SqlBase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -124,62 +130,50 @@ public function updateDatabase()
}

/**
* Runs populateConfigSyncDirectory() for backend:config-export.
* Add option to command.
*
* As long as we have to handle environment specific config, this can not
* be a post command for the default drush config:import command.
* @hook option config:export
*
* TODO: Revisit when we do not need the local environment config folder anymore.
* TODO: Then decide if we can make this a post command for config:*.
*
* @hook pre-command backend:config-export
*
* @param \Consolidation\AnnotatedCommand\CommandData $commandData
* @param \Symfony\Component\Console\Command\Command $command
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function preConfigExportCommand(CommandData $commandData)
public function additionalConfigExportOption(Command $command, AnnotationData $annotationData)
{
$this->populateConfigSyncDirectory();
$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.'
);
}

/**
* Export configuration of BurdaStyle backend site.
*
* TODO: Revisit when we do not need the local environment config folder anymore.
* TODO: Then decide if we can make this a post command for config:export.
*
* @command backend:config-export
* @hook init config:export
*
* @aliases backend:cex
*
* @options-backend
*
* @usage drush @elle backend:config-export
* Exports the elle configuration.
*
* @bootstrap config
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function configExport()
public function initConfigExportCommand(InputInterface $input, AnnotationData $annotationData)
{
if ($this->environment !== 'prod') {
$this->logger()->error('Only production will be exported. Current environment is "%environment".', ['%environment' => $this->environment]);

return;
}
$this->initCommands($input, $annotationData);
}

// export the config into the export folder.
$this->drush($this->selfRecord(), 'config:export', [], ['yes' => $this->input()->getOption('yes')]);
/**
* Runs populateConfigSyncDirectory() for config:export.
*
* @hook pre-command config:export
*
* @param \Consolidation\AnnotatedCommand\CommandData $commandData
*/
public function preConfigExportCommand(CommandData $commandData)
{
$this->populateConfigSyncDirectory();
}

/**
* Move files from sync folder to shared or override folders.
*
* As long as we have to handle environment specific config, this can not
* be a post command for the default drush config:import command.
*
* TODO: Revisit when we do not need the local environment config folder anymore.
* TODO: Then decide if we can make this a post command for config:export.
*
* @hook post-command backend:config-export
* @hook post-command config:export
*
* @param $result
* @param \Consolidation\AnnotatedCommand\CommandData $commandData
Expand All @@ -189,7 +183,6 @@ public function postConfigExportCommand($result, CommandData $commandData)
$exportedFiles = $this->getConfigFilesInDirectory($this->siteConfigSyncDirectory());
$overrideFiles = $this->getConfigFilesInDirectory($this->siteConfigOverrideDirectory());
$sharedFiles = $this->getConfigFilesInDirectory($this->configSharedDirectory());
$localFiles = $this->getConfigFilesInDirectory($this->siteConfigEnvironmentDirectory('local'));
$modifiedFiles = [];

foreach ($exportedFiles as $fileName => $fullPath) {
Expand All @@ -214,14 +207,6 @@ public function postConfigExportCommand($result, CommandData $commandData)
}
}

// Give information to the user, when we modified a configuration that also exists in local config.
// TODO: revisit, when local config folder has been removed.
foreach ($modifiedFiles as $fileName => $fullPath) {
if (isset($localFiles[$fileName])) {
$this->io()->block('Configuration file "'.$fileName.'" was changed and exists in local config folder. Please check, if local config has to be manually modified.', 'INFO', 'fg=yellow');
}
}

// Remove files from override, that were not exported anymore.
foreach ($overrideFiles as $fileName => $fullPath) {
if (!isset($exportedFiles[$fileName])) {
Expand All @@ -242,15 +227,9 @@ public function postConfigExportCommand($result, CommandData $commandData)
}

/**
* Runs populateConfigSyncDirectory() for backend:config-import.
* Runs populateConfigSyncDirectory() for config:import.
*
* As long as we have to handle environment specific config, this can not
* be a post command for the default drush config:import command.
*
* TODO: Revisit when we do not need the local environment config folder anymore.
* TODO: Then decide if we can make this a post command for config:*.
*
* @hook pre-command backend:config-import
* @hook pre-command config:import
*
* @param \Consolidation\AnnotatedCommand\CommandData $commandData
*/
Expand All @@ -260,32 +239,35 @@ public function preConfigImportCommand(CommandData $commandData)
}

/**
* Import configuration of BurdaStyle backend site.
*
* This is simple wrapper to default the config:import command. it is only
* until we can get rid of the environment specific config directories
* (local and testing).
* Add option to command.
*
* TODO: Revisit when we do not need local config folder anymore. Then we can
* TODO: delete this command and change the preConfigImportCommand to hook
* TODO: to the default config:import command.
* @hook option config:import
*
* @command backend:config-import
*
* @aliases backend:cim
*
* @options-backend
*
* @usage drush @elle backend:config-import
* Exports the elle configuration.
* @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
*
* @bootstrap config
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function configImport()
public function initConfigImportCommand(InputInterface $input, AnnotationData $annotationData)
{
$this->drush($this->selfRecord(), 'config:import', [], ['yes' => $this->input()->getOption('yes')]);
$this->initCommands($input, $annotationData);
}


/**
* Prepare an update branch. Does code update, database update and config export.
*
Expand Down Expand Up @@ -323,7 +305,7 @@ public function prepareUpdateBranch()
// Update database and export config for all sites.
foreach ($aliases as $alias) {
$this->process(['drush', $alias, 'backend:update-database'] + $this->getOptions(), $this->projectDirectory());
$this->process(['drush', $alias, 'backend:config-export'] + $this->getOptions(), $this->projectDirectory());
$this->process(['drush', $alias, 'config:export'] + $this->getOptions(), $this->projectDirectory());
}
}

Expand All @@ -350,6 +332,29 @@ public function createTestingDump()
$this->process(['php', 'core/scripts/db-tools.php', 'dump-database-d8-mysql', '--database-url', $dbUrl], $this->drupalRootDirectory());
}

/**
* Enable modules that are excluded from config export.
*
* @command backend:enable-dev-modules
*
* @options-backend
*
* @bootstrap full
*/
public function enableDevModules()
{
$modules = Settings::get('config_exclude_modules', []);

if (!count($modules)) {
$this->logger()->warning('No modules defined in $settings[\'config_exclude_modules\'].');

return;
}

$process = $this->processManager()->drush($this->siteAliasManager()->getSelf(), 'pm:enable', $modules, Drush::redispatchOptions());
$process->mustRun($process->showRealtime());
}

/**
* Gets a cleaned up array of $key=$value strings from the input options.
*
Expand Down Expand Up @@ -389,12 +394,6 @@ protected function populateConfigSyncDirectory(): void
foreach ($overrideFiles as $fileName => $fullPath) {
$this->filesystem->copy($fullPath, $syncDirectory.'/'.$fileName, true);
}
if ($this->filesystem->exists($this->siteConfigEnvironmentDirectory($this->environment))) {
$environmentFiles = $this->getConfigFilesInDirectory($this->siteConfigEnvironmentDirectory($this->environment));
foreach ($environmentFiles as $fileName => $fullPath) {
$this->filesystem->copy($fullPath, $syncDirectory.'/'.$fileName, true);
}
}
}

/**
Expand Down
35 changes: 5 additions & 30 deletions BackendCommandsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ trait BackendCommandsTrait
*/
private $projectDirectory;

/**
* @var string
*/
private $environment;

/**
* @hook init
*
Expand All @@ -51,21 +46,18 @@ public function initCommands(InputInterface $input, AnnotationData $annotationDa
{
// Initialize project directory.
$this->projectDirectory = $input->getOption('project-directory') ?: Drush::bootstrapManager()->getComposerRoot();
$this->environment = $input->getOption('environment');
}

/**
* 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 .
* @option environment Choose environment the installation is built for, i.e. which config folders are used.
* Possible values are "local", "testing" and "prod". (Defaults to "prod").
* @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, 'environment' => 'prod'])
public function optionsBackend($options = ['project-directory' => false])
{
}

Expand Down Expand Up @@ -114,18 +106,13 @@ protected function process($command, $cwd = null, array $env = null, $input = nu

/**
* The base project directory, where most commands will be executed from.
* Defaults to composer root of project-
*
* @return \Consolidation\AnnotatedCommand\CommandError|string
* @return string
*/
protected function projectDirectory(): string
{
if (isset($this->projectDirectory)) {
return $this->projectDirectory;
}

$msg = dt('The project directory has not been set.');

return new CommandError($msg);
return $this->projectDirectory;
}

/**
Expand Down Expand Up @@ -193,18 +180,6 @@ protected function siteConfigOverrideDirectory(): string
return $this->siteConfigSyncDirectory().'/../override';
}

/**
* The directory, where site specific configuration for the current environment is placed.
*
* @param string $environment
* The environment to get the config directory for.
* @return string
*/
protected function siteConfigEnvironmentDirectory(string $environment): string
{
return $this->siteConfigSyncDirectory().'/../'.$environment;
}

/**
* Get the '@self' alias record.
*
Expand Down

0 comments on commit d851b27

Please sign in to comment.