Skip to content
Snippets Groups Projects
Commit 5b207a22 authored by jurgenhaas's avatar jurgenhaas
Browse files

docker/l3d#58 Move project settings out of composer.json

parent 728993c9
No related branches found
No related tags found
No related merge requests found
ahoyapi: v2
commands:
behat:
imports:
- ahoy.yml
usage: Behat for Drupal
/vendor/
/.env
/composer.lock
.env
......@@ -39,10 +39,8 @@
"drush-ops/behat-drush-endpoint": "^9.2",
"jcalderonzumba/gastonjs": "^1.2",
"jcalderonzumba/mink-phantomjs-driver": "^0.3",
"lakedrops/composer-json-utils": "^1.5.0",
"lakedrops/docker4drupal": "^1.9.0",
"lakedrops/dotenv": "^1.1.3",
"twig/twig": "^1.23.1"
"lakedrops/composer-json-utils": "^1.7||dev-master",
"lakedrops/docker4drupal": "^1.18||dev-drupal-8"
},
"require-dev": {
"composer/composer": "^1||^2",
......@@ -56,14 +54,6 @@
}
},
"extra": {
"class": "LakeDrops\\Behat4Drupal\\Plugin",
"lakedrops": {
"ahoy": {
"behat": {
"usage": "Behat for Drupal",
"imports": ["ahoy.yml"]
}
}
}
"class": "LakeDrops\\Behat4Drupal\\Plugin"
}
}
......@@ -17,4 +17,5 @@ class CommandProvider implements CommandProviderCapability {
new UpdateCommand(),
];
}
}
......@@ -3,12 +3,9 @@
namespace LakeDrops\Behat4Drupal;
use LakeDrops\Component\Composer\BaseHandler;
use LakeDrops\Component\Dotenv\Dotenv;
use LakeDrops\Docker4Drupal\Handler as D4DHandler;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
use Twig_Environment;
use Twig_Loader_Array;
/**
* Class Handler.
......@@ -17,24 +14,48 @@ use Twig_Loader_Array;
*/
class Handler extends BaseHandler {
/**
* {@inheritdoc}
*/
public function configId(): string {
return 'behat4drupal';
}
/**
* {@inheritdoc}
*/
protected function configDefault(): array {
$d4dHandler = new D4DHandler($this->composer, $this->io);
$d4dConfig = $d4dHandler->getConfig();
$projectname = $d4dConfig->readValue('projectname');
$webserver = $d4dConfig->readValue('webserver');
$options = [
'projectname' => $projectname,
'urlprefix' => '',
];
$options['baseurl'] = empty($options['global']) ?
$webserver['type'] :
$projectname . '.docker.localhost:8000';
return $options;
}
/**
* Post project create event to execute the scaffolding.
*
* @param bool $overwrite
* Whether to overwrite existing config files.
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function configureProject($overwrite = FALSE) {
public function configureProject($overwrite = FALSE): void {
$this->init();
// We only do the fancy stuff for developers and for CI.
if (!$this->isDevMode() && !$this->isCiContext()) {
return;
}
$options = $this->getOptions();
$fs = new Filesystem();
$installationManager = $this->composer->getInstallationManager();
......@@ -44,22 +65,19 @@ class Handler extends BaseHandler {
$pluginRoot = $installationManager->getInstallPath($this->getPackage('lakedrops/behat4drupal'));
// Provide all the required files.
$twig_loader = new Twig_Loader_Array([]);
$twig = new Twig_Environment($twig_loader);
foreach ($this->getFiles($projectRoot) as $template => $def) {
if (!$fs->exists($def['dest'])) {
$fs->mkdir($def['dest']);
}
$twig_loader->setTemplate($template, $template);
$filename = $twig->render($template, $options);
$filename = $this->config->render($template, $template);
$file = $def['dest'] . '/' . $filename;
if (($overwrite && empty($def['custom'])) || !$fs->exists($file)) {
$twig_loader->setTemplate($filename, file_get_contents($pluginRoot . '/templates/' . $template . '.twig'));
$rendered = $twig->render($filename, $options);
if (!empty($def['add2yaml']) && isset($options[$filename])) {
$rendered = $this->config->render($filename, file_get_contents($pluginRoot . '/templates/' . $template . '.twig'));
$extraOptions = $this->config->readValue($filename);
if (!empty($def['add2yaml']) && $extraOptions !== NULL) {
$yaml = Yaml::parse($rendered);
/** @noinspection SlowArrayOperationsInLoopInspection */
$yaml = array_merge_recursive($yaml, $options[$filename]);
$yaml = array_merge_recursive($yaml, $extraOptions);
$rendered = Yaml::dump($yaml, 9, 2);
}
if ($fs->exists($file)) {
......@@ -89,7 +107,7 @@ class Handler extends BaseHandler {
* @return array
* List of files.
*/
protected function getFiles($projectRoot): array {
protected function getFiles(string $projectRoot): array {
return [
'behat.yml' => [
'dest' => $projectRoot . '/tests/behat',
......@@ -106,29 +124,4 @@ class Handler extends BaseHandler {
];
}
/**
* Retrieve configuration for this package.
*
* @return array
* The settings from the extra configuration.
*/
protected function getOptions(): array {
$handler = new D4DHandler($this->composer, $this->io);
$projectname = $handler->getOptions('projectname');
$webserver = $handler->getOptions('webserver');
$env = new Dotenv('behat4drupal', $this->io);
$extra = $this->composer->getPackage()->getExtra() + ['behat4drupal' => []];
$options = $extra['behat4drupal'] + [
'projectname' => $projectname,
'urlprefix' => '',
];
$options['baseurl'] = empty($options['global']) ?
$webserver['type'] :
$projectname . '.docker.localhost:8000';
return $env->replaceEnvironmentVariables($options);
}
}
......@@ -14,7 +14,7 @@ class Plugin extends BasePlugin {
/**
* {@inheritdoc}
*/
public function getHandlerClass() {
public function getHandlerClass(): string {
return Handler::class;
}
......@@ -41,12 +41,8 @@ class Plugin extends BasePlugin {
*
* @param \Composer\Script\Event $event
* The event that triggered the plugin.
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function postCreateProject(Event $event) {
public function postCreateProject(Event $event): void {
/** @var Handler $handler */
$handler = $this->handler;
$handler
......
......@@ -11,7 +11,7 @@ class UpdateCommand extends BaseCommand {
/**
* {@inheritdoc}
*/
protected function configure() {
protected function configure(): void {
$this->setName('lakedrops:behat');
$this->setDescription('(Re-)Configure Behat for this project.');
}
......@@ -19,22 +19,19 @@ class UpdateCommand extends BaseCommand {
/**
* {@inheritdoc}
*/
public function getHandlerClass() {
public function getHandlerClass(): string {
return Handler::class;
}
/**
* {@inheritdoc}
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
protected function execute(InputInterface $input, OutputInterface $output) {
protected function execute(InputInterface $input, OutputInterface $output): int {
parent::execute($input, $output);
/** @var Handler $handler */
$handler = $this->handler;
$handler->configureProject(TRUE);
return 0;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment