Skip to content

Commit aff83c2

Browse files
authored
Merge pull request #31 from dennisinteractive/27689_artifact_tag_checkout
27689 artifact tag checkout
2 parents b6f1a51 + 8e8f3f0 commit aff83c2

23 files changed

+867
-588
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ e.g. https://raw.githubusercontent.com/dennisinteractive/drupal_console_commands
1717
- drupal **site:new**
1818
Builds a new site using Drupal project as template https://github.com/dennisinteractive/drupal-project
1919

20-
- drupal **site:checkout** *site-name*
20+
- drupal **site:checkout** *site-name* [--branch|--tag]
21+
Performs a git clone and checks out the specified branch or tag
22+
23+
- drupal **site:checkout:tag** *site-name* --tag
24+
Performs a git clone and checks out the specified tag/revision
25+
26+
- drupal **site:checkout:branch** *site-name* --branch
2127
Performs a git clone and checks out the specified branch
2228

2329
- drupal **site:compose** *site-name*
@@ -38,9 +44,6 @@ e.g. https://raw.githubusercontent.com/dennisinteractive/drupal_console_commands
3844
- drupal **site:settings:memcache** *site-name*
3945
Creates *settings.memcache.php* in the *web/sites/default* folder. This file contains Memcache configuration and should not be committed.
4046

41-
- drupal **site:drush:alias** *site-name*
42-
Sets up drush aliases
43-
4447
- drupal **site:phpunit:setup** *site-name*
4548
Creates *phpunit.xml* in the root. This file contains PHPUnit configuration and should not be committed.
4649

@@ -75,7 +78,6 @@ Chains that can be reused on various environments
7578
- site:settings:db
7679
- site:settings:local
7780
- site:settings:memcache
78-
- site:drush:alias
7981

8082
- drupal **site:test:setup** Sets the test suites
8183
- site:phpunit:setup
@@ -99,11 +101,8 @@ Each environment will have its own chain that executes the relevant commands and
99101
- site:update
100102

101103
### Artifact
102-
- drupal **site:build** Builds a site for artifacts
103-
- site:checkout
104-
- site:rebuild (chain)
105-
106104
- drupal **site:build:artifact** Prepare artifacts
105+
- site:checkout
107106
- site:compose
108107
- site:npm
109108
- site:grunt

chain/chain-site-build-artifact.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ command:
33
name: site:build:artifact
44
description: 'Prepare artifacts.'
55
commands:
6+
# Checkout site
7+
- command: site:checkout
8+
arguments:
9+
name: '%{{name}}'
10+
options:
11+
tag: '%{{tag}}'
12+
destination-directory: '%{{root}}'
613
# Run composer.
714
- command: site:compose
815
arguments:

chain/chain-site-configure.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,3 @@ commands:
1515
- command: site:settings:memcache
1616
arguments:
1717
name: '%{{name}}'
18-
# Create the drush alias
19-
- command: site:drush:alias
20-
arguments:
21-
name: '%{{name}}'

console.config.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ application:
33
commands:
44
forced:
55
'site:checkout':
6-
class: \DennisDigital\Drupal\Console\Command\Site\CheckoutCommand
6+
class: \DennisDigital\Drupal\Console\Command\Site\Checkout\CheckoutCommand
7+
'site:checkout:tag':
8+
class: \DennisDigital\Drupal\Console\Command\Site\Checkout\TagCommand
9+
'site:checkout:branch':
10+
class: \DennisDigital\Drupal\Console\Command\Site\Checkout\BranchCommand
711
'site:compose':
812
class: \DennisDigital\Drupal\Console\Command\Site\ComposeCommand
913
'site:settings:db':
@@ -12,8 +16,6 @@ application:
1216
class: \DennisDigital\Drupal\Console\Command\Site\Settings\MemcacheCommand
1317
'site:settings:local':
1418
class: \DennisDigital\Drupal\Console\Command\Site\Settings\LocalCommand
15-
'site:drush:alias':
16-
class: \DennisDigital\Drupal\Console\Command\Site\DrushAliasCommand
1719
'site:db:import':
1820
class: \DennisDigital\Drupal\Console\Command\Site\DbImportCommand
1921
'site:behat:setup':

src/Command/Site/AbstractCommand.php

Lines changed: 122 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Drupal\Console\Core\Style\DrupalStyle;
1919
use Drupal\Console\Core\Command\Shared\CommandTrait;
2020
use DennisDigital\Drupal\Console\Command\Exception\CommandException;
21+
use DennisDigital\Drupal\Console\Utils\ShellProcess;
2122

2223
/**
2324
* Class AbstractCommand
@@ -69,11 +70,29 @@ abstract class AbstractCommand extends Command {
6970
protected $profile = NULL;
7071

7172
/**
72-
* Stores the destination directory.
73+
* The root directory.
7374
*
7475
* @var string
7576
*/
76-
protected $destination = NULL;
77+
private $root = NULL;
78+
79+
/**
80+
* The web root directory.
81+
*
82+
* This is the web directory within the root.
83+
*
84+
* @var string
85+
*/
86+
private $webRoot = NULL;
87+
88+
/**
89+
* The site root directory.
90+
*
91+
* This is where we put settings.php
92+
*
93+
* @var string
94+
*/
95+
private $siteRoot = NULL;
7796

7897
/**
7998
* Stores the site url.
@@ -179,13 +198,58 @@ protected function validateSiteParams(InputInterface $input, OutputInterface $ou
179198
// Validate profile.
180199
$this->validateProfile($input);
181200

182-
// Validate destination.
183-
$this->validateDestination($input);
201+
// Validate root.
202+
$this->validateRoot($input);
203+
204+
// Validate web root.
205+
$this->validateWebRoot();
206+
207+
// Validate settings.php directory.
208+
$this->validateSiteRoot();
184209

185210
// Validate url.
186211
$this->validateUrl($input);
187212
}
188213

214+
/**
215+
* Getter for the root directory property.
216+
*/
217+
protected function getRoot() {
218+
if (is_null($this->root)) {
219+
throw new CommandException('Root directory is not available.');
220+
}
221+
return $this->root;
222+
}
223+
224+
/**
225+
* Getter for the web root directory property.
226+
*/
227+
protected function getWebRoot() {
228+
if (is_null($this->webRoot)) {
229+
throw new CommandException('Web root directory is not available.');
230+
}
231+
return $this->webRoot;
232+
}
233+
234+
/**
235+
* Getter for the site root directory property.
236+
*/
237+
protected function getSiteRoot() {
238+
if (is_null($this->siteRoot)) {
239+
throw new CommandException('Site root directory is not available.');
240+
}
241+
return $this->siteRoot;
242+
}
243+
244+
/**
245+
* Check if the current build has a site root directory.
246+
*
247+
* @return bool
248+
*/
249+
protected function hasSiteRoot() {
250+
return !is_null($this->siteRoot);
251+
}
252+
189253
/**
190254
* Helper to check that the config file exits and load the configuration.
191255
*
@@ -252,54 +316,51 @@ protected function validateProfile(InputInterface $input) {
252316
}
253317

254318
/**
255-
* Helper to validate destination parameter.
319+
* Validate and set the web root directory.
320+
*/
321+
protected function validateWebRoot() {
322+
$web_directory = empty($this->config['web_directory']) ? 'web' : $this->config['web_directory'];
323+
$this->webRoot = $this->getRoot() . trim($web_directory, '/') . '/';
324+
}
325+
326+
/**
327+
* Validate and set the root directory.
256328
*
257329
* @param InputInterface $input
258-
*
259-
* @throws CommandException
260-
*
261-
* @return string Destination
330+
* @return string
262331
*/
263-
protected function validateDestination(InputInterface $input) {
332+
protected function validateRoot(InputInterface $input) {
264333
if ($input->hasOption('destination-directory') &&
265334
!is_null($input->getOption('destination-directory'))
266335
) {
267336
// Use config from parameter.
268-
$this->destination = $input->getOption('destination-directory');
337+
$this->root = $input->getOption('destination-directory');
269338
}
270339
elseif (isset($this->config['root'])) {
271340
// Use config from sites.yml.
272-
$this->destination = $this->config['root'];
341+
$this->root = $this->config['root'];
273342
}
274343
else {
275-
$this->destination = '/tmp/' . $this->siteName;
344+
$this->root = '/tmp/' . $this->siteName;
276345
}
277346

278347
// Allow destination to be overriden by environment variable. i.e.
279348
// export site_destination_directory="/directory/"
280349
if (!getenv('site_destination_directory')) {
281-
putenv("site_destination_directory=$this->destination");
350+
putenv("site_destination_directory=$this->root");
282351
}
283352
else {
284-
$this->destination = getenv('site_destination_directory');
285-
}
286-
287-
// Make sure we have a slash at the end.
288-
if (substr($this->destination, -1) != '/') {
289-
$this->destination .= '/';
353+
$this->root = getenv('site_destination_directory');
290354
}
291355

292-
return $this->destination;
356+
$this->root = rtrim($this->root, '/') . '/';
293357
}
294358

295359
/**
296-
* Helper to validate destination parameter.
360+
* Helper to validate URL.
297361
*
298362
* @param InputInterface $input
299-
*
300-
* @throws CommandException
301-
*
302-
* @return string Destination
363+
* @return string
303364
*/
304365
protected function validateUrl(InputInterface $input) {
305366
$scheme = isset($this->config['scheme']) && !empty($this->config['scheme']) ? $this->config['scheme'] : 'http';
@@ -317,16 +378,25 @@ protected function validateUrl(InputInterface $input) {
317378
}
318379

319380
/**
320-
* Helper to return the path to settings.php
381+
* Helper to set the site root.
382+
*
383+
* This is where we place settings.php
384+
*
321385
* It will try to match a folder with same name as site name
322386
* If not found, it will try to match a folder called "default".
323387
*
324388
* @return string Path
325389
*/
326-
public function settingsPhpDirectory() {
327-
$webSitesPath = $this->destination . 'web/sites/';
390+
public function validateSiteRoot() {
391+
$webSitesPath = $this->getWebRoot() . 'sites/';
328392
$settingsPath = $webSitesPath . 'default';
329393

394+
// It's possible that a command is run before the site is available. e.g. checkout
395+
// We will skip setting in this situation, but throw an Exception in the site root getter to prevent any unpredictable behaviour.
396+
if (!file_exists($settingsPath)) {
397+
return;
398+
}
399+
330400
$command = sprintf(
331401
'cd %s && find . -name settings.php',
332402
$this->shellPath($webSitesPath)
@@ -359,16 +429,36 @@ public function settingsPhpDirectory() {
359429
$settingsPath .= '/';
360430
}
361431

362-
return $settingsPath;
432+
// Fix folder permissions.
433+
$this->fixSiteFolderPermissions();
434+
435+
$this->siteRoot = $settingsPath;
436+
}
437+
438+
/**
439+
* Fixes the site folder permissions which is often changed by Drupal core.
440+
*/
441+
protected function fixSiteFolderPermissions() {
442+
if ($this->hasSiteRoot()) {
443+
$commands[] = sprintf('chmod 777 %s', $this->getSiteRoot());
444+
$commands[] = sprintf('chmod 777 %ssettings.php', $this->getSiteRoot());
445+
$command = implode(' && ', $commands);
446+
$this->io->commentBlock($command);
447+
$shellProcess = $this->getShellProcess();
448+
if (!$shellProcess->exec($command, TRUE)) {
449+
throw new CommandException($shellProcess->getOutput());
450+
}
451+
}
363452
}
364453

365454
/**
366455
* Get the shell process.
367456
*
368-
* @return Drupal\Console\Core\Command\Exec\ExecCommand
457+
* @return ShellProcess
369458
*/
370459
protected function getShellProcess() {
371-
return $this->container->get('console.shell_process');
460+
$app_root = $this->container->get('app.root');
461+
return new ShellProcess($app_root);
372462
}
373463

374464
/**

src/Command/Site/AbstractConfigCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ protected function configure() {
7979
*/
8080
protected function interact(InputInterface $input, OutputInterface $output) {
8181
parent::interact($input, $output);
82-
8382
}
8483

8584
/**
@@ -97,8 +96,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
9796
* @throws \DennisDigital\Drupal\Console\Exception\CommandException
9897
*/
9998
protected function generateConfigFile() {
100-
$this->template = $this->destination . $this->template;
101-
$this->filename = $this->destination . $this->filename;
99+
$this->template = $this->getRoot() . $this->template;
100+
$this->filename = $this->getRoot() . $this->filename;
102101

103102
// Validation.
104103
if (!$this->fileExists($this->template)) {

0 commit comments

Comments
 (0)