Skip to content

Commit 0e7e8cb

Browse files
committed
Use normal command to build the site
1 parent 6016337 commit 0e7e8cb

File tree

4 files changed

+191
-11
lines changed

4 files changed

+191
-11
lines changed

chain/chain-site-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Usage: drupal site:build --placeholder="name:subscriptions"
22
command:
3-
name: site:build
3+
name: chain_site:build
44
description: 'Performs multiple commands to build a site'
55
commands:
66
# Checkout site

console.config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ application:
22
autowire:
33
commands:
44
forced:
5+
"site:build":
6+
class: \DennisDigital\Drupal\Console\Command\SiteBuildCommand
57
'site:checkout':
68
class: \DennisDigital\Drupal\Console\Command\SiteCheckoutCommand
79
'site:compose':

src/Command/SiteBaseCommand.php

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Finder\Finder;
1718
use Drupal\Console\Core\Style\DrupalStyle;
1819
use Drupal\Console\Core\Command\Shared\CommandTrait;
1920
use DennisDigital\Drupal\Console\Exception\SiteCommandException;
@@ -68,6 +69,11 @@ class SiteBaseCommand extends Command {
6869
*/
6970
protected $container;
7071

72+
/**
73+
* @var ConfigurationManager
74+
*/
75+
protected $configurationManager;
76+
7177
/**
7278
* Constructor.
7379
*/
@@ -112,15 +118,24 @@ protected function configure() {
112118
protected function interact(InputInterface $input, OutputInterface $output) {
113119
$this->io = new DrupalStyle($input, $output);
114120

121+
$this->configurationManager = $this->container
122+
->get('console.configuration_manager');
123+
124+
$sitesDirectory = $this->configurationManager->getSitesDirectory();
125+
$options = $this->siteList($sitesDirectory);
126+
115127
$name = $input->getArgument('name');
116128
if (!$name) {
117-
$name = $this->io->ask($this->trans('Site name (In small letters. Don\'t use spaces or hyphens, use underlines.)'));
118-
119-
$input->setArgument('name', $name);
129+
$name = $this->io->choice(
130+
$this->trans('Select a site'),
131+
$options,
132+
reset($options),
133+
TRUE
134+
);
135+
$input->setArgument('name', reset($name));
120136
}
121137

122138
$this->validateSiteParams($input, $output);
123-
124139
}
125140

126141
/**
@@ -160,15 +175,12 @@ protected function validateSiteParams(InputInterface $input, OutputInterface $ou
160175
*/
161176
protected function _siteConfig(InputInterface $input) {
162177
$siteName = $input->getArgument('name');
163-
164-
$configurationManager = $this->container
165-
->get('console.configuration_manager');
166-
178+
//var_dump($this->config);
167179
// $environment = $input->getOption('env')
168-
$environment = $configurationManager->getConfiguration()
180+
$environment = $this->configurationManager->getConfiguration()
169181
->get('application.environment');
170182

171-
$config = $configurationManager->readTarget($siteName . '.' . $environment);
183+
$config = $this->configurationManager->readTarget($siteName . '.' . $environment);
172184

173185
if (empty($config))
174186
{
@@ -366,4 +378,38 @@ protected function cleanFileName($file_name) {
366378
protected function shellPath($file_name) {
367379
return addcslashes($this->cleanFileName($file_name), ' ');
368380
}
381+
382+
/**
383+
* Extracted from DebugCommand.
384+
* This function lists all sites found in ~/.console/sites folder
385+
* It will only return sites that have 'repo' configured
386+
*
387+
* @param string $sitesDirectory
388+
* @return array
389+
*/
390+
private function siteList($sitesDirectory)
391+
{
392+
$finder = new Finder();
393+
$finder->in($sitesDirectory);
394+
$finder->name("*.yml");
395+
396+
$tableRows = [];
397+
foreach ($finder as $site) {
398+
$siteName = $site->getBasename('.yml');
399+
$environments = $this->configurationManager
400+
->readSite($site->getRealPath());
401+
402+
if (!$environments || !is_array($environments)) {
403+
continue;
404+
}
405+
406+
foreach ($environments as $environment => $config) {
407+
if (isset($config['repo'])) {
408+
$tableRows[] = $siteName;
409+
}
410+
}
411+
}
412+
413+
return $tableRows;
414+
}
369415
}

src/Command/SiteBuildCommand.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \DennisDigital\Drupal\Console\Command\SiteBuildCommand.
6+
*
7+
* Builds the site by calling various commands.
8+
*/
9+
10+
namespace DennisDigital\Drupal\Console\Command;
11+
12+
use Symfony\Component\Console\Input\InputOption;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Process\Process;
16+
use Symfony\Component\Process\Exception\ProcessFailedException;
17+
use DennisDigital\Drupal\Console\Exception\SiteCommandException;
18+
19+
/**
20+
* Class SiteBuildCommand
21+
*
22+
* @package DennisDigital\Drupal\Console\Command
23+
*/
24+
class SiteBuildCommand extends SiteBaseCommand {
25+
26+
/**
27+
* Stores branch information.
28+
*
29+
* @var array Branch.
30+
*/
31+
protected $branch;
32+
33+
/**
34+
* @var ShellProcess
35+
*/
36+
private $process;
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected function configure() {
42+
parent::configure();
43+
44+
$this->setName('site:build')
45+
->setDescription('Build a site');
46+
47+
// Custom options.
48+
$this->addOption(
49+
'branch',
50+
'-B',
51+
InputOption::VALUE_OPTIONAL,
52+
'Specify which branch to build if different than the global branch found in sites.yml'
53+
);
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
protected function interact(InputInterface $input, OutputInterface $output) {
60+
parent::interact($input, $output);
61+
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
protected function execute(InputInterface $input, OutputInterface $output) {
68+
parent::execute($input, $output);
69+
70+
$commands = [
71+
'drupal site:checkout %s',
72+
// 'drupal site:compose %s',
73+
// 'drupal site:npm %s',
74+
// 'drupal site:grunt %s',
75+
// 'drupal site:settings:db %s',
76+
// 'drupal site:phpunit:setup %s',
77+
// 'drupal site:behat:setup %s',
78+
// 'drupal site:settings:memcache %s',
79+
// 'drupal site:db:import %s',
80+
];
81+
82+
foreach ($commands as $item) {
83+
$command = sprintf(
84+
$item,
85+
$this->siteName
86+
);
87+
$this->process($command);
88+
//print (shell_exec($command));
89+
}
90+
91+
$commands = [
92+
'cd %s/web; drush updb -y;',
93+
'cd %s/web; drush cr;',
94+
];
95+
96+
foreach ($commands as $item) {
97+
$command = sprintf(
98+
$item,
99+
$this->destination
100+
);
101+
$this->process($command);
102+
}
103+
}
104+
105+
protected function process($command) {
106+
$this->process = new Process($command, null, null, 'subscriptions');
107+
//$this->process->setWorkingDirectory($this->destination);
108+
$this->process->enableOutput();
109+
$this->process->setTimeout(null);
110+
$this->process->setPty(true);
111+
$this->process->run(
112+
function ($type, $buffer) {
113+
$this->io->write($buffer);
114+
}
115+
);
116+
117+
if (!$this->process->isSuccessful()) {
118+
throw new SiteCommandException($this->process->getOutput());
119+
}
120+
121+
// $shellProcess = $this->getShellProcess();
122+
// $shellProcess->process->enableOutput();
123+
//
124+
// if ($shellProcess->exec($command, TRUE)) {
125+
// $this->io->writeln($shellProcess->getOutput());
126+
// }
127+
// else {
128+
// throw new SiteCommandException($shellProcess->getOutput());
129+
// }
130+
}
131+
132+
}

0 commit comments

Comments
 (0)