Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions config/dist/chain/quick-start.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# How to use
# quick:start --placeholder="directory:/path/to/drupal-project/" --placeholder="repository:acquia/lightning-project:^8.1" --placeholder="profile:minimal"
# quick:start --directory="/path/to/drupal-project/"
# quick:start --directory="/path/to/drupal-project/" --profile="minimal"
# quick:start --repository="acquia/lightning-project:^8.1" --directory="/path/to/drupal-project/" --profile="lightning"
command:
name: quick:start
description: 'Download, install and serve a new Drupal project'
vars:
repository: drupal-composer/drupal-project:8.x-dev
profile: standard
commands:
# Create Drupal project using DrupalComposer
- command: exec
arguments:
bin: composer create-project %{{repository|drupal-composer/drupal-project:8.x-dev}} %{{directory}} --prefer-dist --no-progress --no-interaction
bin: composer create-project %{{repository}} %{{directory}} --prefer-dist --no-progress --no-interaction
# Install Drupal
- command: exec
arguments:
bin: drupal site:install %{{profile|standard}} --root=%{{directory}} --no-interaction
bin: drupal site:install %{{profile}} --root=%{{directory}} --no-interaction
# Start PHP bult-in server
- command: exec
arguments:
Expand Down
15 changes: 10 additions & 5 deletions config/dist/chain/site-install-placeholers.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
vars:
db_type: mysql
db_host: 127.0.0.1
db_name: drupal
db_port: 3306
commands:
# Install Drupal
- command: site:install
options:
langcode: 'en'
db-type: '%{{db_type|mysql}}'
db-host: '%{{db_host|127.0.0.1}}'
db-name: '%{{db_name|drupal}}'
db-user: '%{{db_user|}}'
db-type: '%{{db_type}}'
db-host: '%{{db_host}}'
db-name: '%{{db_name}}'
db-user: '%{{db_user}}'
db-pass: '%{{db_pass}}'
db-port: '%{{db_port|3306}}'
db-port: '%{{db_port}}'
site-name: 'Drupal 8 site'
site-mail: 'admin@example.org' # default email
account-name: 'admin' # default account
Expand Down
3 changes: 2 additions & 1 deletion config/dist/chain/site-new.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# How to use
# site:new --placeholder="directory:/path/to/drupal-project/" --placeholder="repository:acquia/lightning-project"
# site:new --directory="/path/to/drupal-project/"
# site:new --repository="acquia/lightning-project" --directory="/path/to/drupal-project/"
command:
name: site:new
description: 'Download a new Drupal project'
Expand Down
8 changes: 7 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,13 @@ public function registerChainCommands()
try {
$file = $chainCommand['file'];
$description = $chainCommand['description'];
$command = new ChainCustomCommand($name, $description, $file);
$placeHolders = $chainCommand['placeholders'];
$command = new ChainCustomCommand(
$name,
$description,
$placeHolders,
$file
);
$this->add($command);
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
Expand Down
234 changes: 95 additions & 139 deletions src/Command/Chain/ChainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,36 @@ protected function interact(InputInterface $input, OutputInterface $output)
$file = calculateRealPath($file);
$input->setOption('file', $file);

$chainContent = $this->getFileContents($file);
$chainContent = $this->chainDiscovery->getFileContents($file);
$inlinePlaceHolders = $this->chainDiscovery->extractInlinePlaceHolders($chainContent);

$placeholder = $input->getOption('placeholder');
if ($placeholder) {
$placeholder = $this->placeHolderInlineValueAsArray($placeholder);
}

$inlinePlaceHolders = $this->extractInlinePlaceHolders($chainContent);

if (!$placeholder && $inlinePlaceHolders) {
foreach ($inlinePlaceHolders as $key => $inlinePlaceHolder) {
$inlinePlaceHolderDefault = '';
if (strpos($inlinePlaceHolder, '|')>0) {
$placeholderParts = explode('|', $inlinePlaceHolder);
$inlinePlaceHolder = $placeholderParts[0];
$inlinePlaceHolderDefault = $placeholderParts[1];
$inlinePlaceHolders[$key] = $inlinePlaceHolder;
$placeholder = array_merge(
array_filter(
$inlinePlaceHolders,
function($value) {
return $value !== null;
}
),
$placeholder
);

$inlinePlaceHolders = array_merge(
$inlinePlaceHolders,
$placeholder
);

$missingInlinePlaceHolders = array_diff_key(
$inlinePlaceHolders,
$placeholder
);

if ($missingInlinePlaceHolders) {
foreach ($inlinePlaceHolders as $inlinePlaceHolder => $inlinePlaceHolderValue) {
$placeholder[] = sprintf(
'%s:%s',
$inlinePlaceHolder,
Expand All @@ -124,7 +138,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
'Enter value for %s placeholder',
$inlinePlaceHolder
),
$inlinePlaceHolderDefault
$inlinePlaceHolderValue
)
);
}
Expand Down Expand Up @@ -163,44 +177,87 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

// Resolve inlinePlaceHolders
$chainContent = $this->chainDiscovery->getFileContents($file);
$inlinePlaceHolders = $this->chainDiscovery->extractInlinePlaceHolders($chainContent);

$placeholder = $input->getOption('placeholder');
if ($placeholder) {
$placeholder = $this->inlineValueAsArray($placeholder);
}
$placeHolderOptions = [];
foreach ($placeholder as $placeholderItem) {
$placeHolderOptions[] = key($placeholderItem);
$placeholder = $this->placeHolderInlineValueAsArray($placeholder);
}

$chainContent = $this->getFileContents($file);
$inlinePlaceHolders = $this->extractInlinePlaceHolders($chainContent);

if ($inlinePlaceHolders) {
foreach ($inlinePlaceHolders as $key => $inlinePlaceHolder) {
if (!strpos($inlinePlaceHolder, '|')) {
continue;
$placeholder = array_merge(
array_filter(
$inlinePlaceHolders,
function($value) {
return $value !== null;
}
),
$placeholder
);

$placeholderParts = explode('|', $inlinePlaceHolder);
$inlinePlaceHolder = $placeholderParts[0];
$inlinePlaceHolderDefault = $placeholderParts[1];
$inlinePlaceHolders = array_merge(
$inlinePlaceHolders,
$placeholder
);

if (!$inlinePlaceHolderDefault) {
continue;
}
$missingInlinePlaceHolders = array_diff_key(
$inlinePlaceHolders,
$placeholder
);

if (in_array($inlinePlaceHolder, $placeHolderOptions)) {
continue;
}
$missingInlinePlaceHoldersMessages = [];
foreach ($missingInlinePlaceHolders as $inlinePlaceHolder => $inlinePlaceHolderValue) {
$missingInlinePlaceHoldersMessages['default'][] = sprintf(
'--placeholder="%s:%s_VALUE"',
$inlinePlaceHolder,
strtoupper($inlinePlaceHolder)
);
$missingInlinePlaceHoldersMessages['custom'][] = sprintf(
'--%s="%s_VALUE"',
$inlinePlaceHolder,
strtoupper($inlinePlaceHolder)
);
}

$placeholder[] = [$inlinePlaceHolder => $inlinePlaceHolderDefault];
if ($missingInlinePlaceHolders) {
$io->error(
sprintf(
$this->trans('commands.chain.messages.missing-inline-placeholders'),
implode(', ', array_keys($missingInlinePlaceHolders))
)
);

$io->info(
$this->trans(
'commands.chain.messages.set-inline-placeholders'
)
);
foreach ($missingInlinePlaceHoldersMessages['default'] as $missingInlinePlaceHoldersMessage) {
$io->block($missingInlinePlaceHoldersMessage);
}

$io->info(
$this->trans(
'commands.chain.messages.set-inline-placeholders'
)
);
foreach ($missingInlinePlaceHoldersMessages['custom'] as $missingInlinePlaceHoldersMessage) {
$io->block($missingInlinePlaceHoldersMessage);
}

return 1;
}

$environmentPlaceHolders = $this->extractEnvironmentPlaceHolders($chainContent);
$inlinePlaceHolderData = new ArrayDataSource($placeholder);
$placeholderResolver = new RegexPlaceholderResolver($inlinePlaceHolderData, '%{{', '}}');
$chainContent = $placeholderResolver->resolvePlaceholder($chainContent);

// Resolve environmentPlaceHolders
$environmentPlaceHolders = $this->chainDiscovery->extractEnvironmentPlaceHolders($chainContent);
$envPlaceHolderMap = [];
$missingEnvironmentPlaceHolders = [];
foreach ($environmentPlaceHolders as $envPlaceHolder) {
foreach ($environmentPlaceHolders as $envPlaceHolder => $envPlaceHolderValue) {
if (!getenv($envPlaceHolder)) {
$missingEnvironmentPlaceHolders[$envPlaceHolder] = sprintf(
'export %s=%s_VALUE',
Expand All @@ -216,12 +273,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($missingEnvironmentPlaceHolders) {
$io->error(
sprintf(
$this->trans('commands.chain.messages.missing-environment-placeholders'),
$this->trans('commands.chain.messages.missing-environment-placeholders-default'),
implode(', ', array_keys($missingEnvironmentPlaceHolders))
)
);

$io->info($this->trans('commands.chain.messages.set-environment-placeholders'));
$io->info($this->trans('commands.chain.messages.set-environment-placeholders-custom'));
$io->block(array_values($missingEnvironmentPlaceHolders));

return 1;
Expand All @@ -231,61 +288,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$placeholderResolver = new RegexPlaceholderResolver($envPlaceHolderData, '${{', '}}');
$chainContent = $placeholderResolver->resolvePlaceholder($chainContent);

$inlinePlaceHolders = $this->extractInlinePlaceHolders($chainContent);

$inlinePlaceHoldersReplacements = [];
foreach ($inlinePlaceHolders as $key => $inlinePlaceHolder) {
if (strpos($inlinePlaceHolder, '|') > 0) {
$placeholderParts = explode('|', $inlinePlaceHolder);
$inlinePlaceHoldersReplacements[] = $placeholderParts[0];
continue;
}
$inlinePlaceHoldersReplacements[] = $inlinePlaceHolder;
}

$chainContent = str_replace(
$inlinePlaceHolders,
$inlinePlaceHoldersReplacements,
$chainContent
);

$inlinePlaceHolders = $inlinePlaceHoldersReplacements;

$inlinePlaceHolderMap = [];
foreach ($placeholder as $key => $placeholderItem) {
$inlinePlaceHolderMap = array_merge($inlinePlaceHolderMap, $placeholderItem);
}

$missingInlinePlaceHolders = [];

foreach ($inlinePlaceHolders as $inlinePlaceHolder) {
if (!array_key_exists($inlinePlaceHolder, $inlinePlaceHolderMap)) {
$missingInlinePlaceHolders[$inlinePlaceHolder] = sprintf(
'--placeholder="%s:%s_VALUE"',
$inlinePlaceHolder,
strtoupper($inlinePlaceHolder)
);
}
}

if ($missingInlinePlaceHolders) {
$io->error(
sprintf(
$this->trans('commands.chain.messages.missing-inline-placeholders'),
implode(', ', array_keys($missingInlinePlaceHolders))
)
);

$io->info($this->trans('commands.chain.messages.set-inline-placeholders'));
$io->block(array_values($missingInlinePlaceHolders));

return 1;
}

$inlinePlaceHolderData = new ArrayDataSource($inlinePlaceHolderMap);
$placeholderResolver = new RegexPlaceholderResolver($inlinePlaceHolderData, '%{{', '}}');
$chainContent = $placeholderResolver->resolvePlaceholder($chainContent);

$parser = new Parser();
$configData = $parser->parse($chainContent);

Expand Down Expand Up @@ -350,50 +352,4 @@ protected function execute(InputInterface $input, OutputInterface $output)

return 0;
}

/**
* Helper to load and clean up the chain file.
*
* @param string $file The file name
*
* @return string $contents The contents of the file
*/
private function getFileContents($file)
{
$contents = file_get_contents($file);

// Remove lines with comments.
$contents = preg_replace('![ \t]*#.*[ \t]*[\r|\r\n|\n]!', PHP_EOL, $contents);
// Strip blank lines
$contents = preg_replace("/(^[\r\n]*|[\r\n]+)[\t]*[\r\n]+/", PHP_EOL, $contents);

return $contents;
}

private function extractPlaceHolders($chainContent, $identifier)
{
$placeHolders = [];
$regex = '/\\'.$identifier.'{{(.*?)}}/';
preg_match_all(
$regex,
$chainContent,
$placeHolders
);

if (!$placeHolders) {
return [];
}

return array_unique($placeHolders[1]);
}

private function extractInlinePlaceHolders($chainContent)
{
return $this->extractPlaceHolders($chainContent, '%');
}

private function extractEnvironmentPlaceHolders($chainContent)
{
return $this->extractPlaceHolders($chainContent, '$');
}
}
Loading