Skip to content

Commit e16b49d

Browse files
committed
Case 27689; removing code duplication and using custom ShellProcess class with optional output
1 parent 9da8e46 commit e16b49d

File tree

6 files changed

+219
-173
lines changed

6 files changed

+219
-173
lines changed

src/Command/Site/AbstractCommand.php

Lines changed: 5 additions & 2 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
@@ -365,10 +366,12 @@ public function settingsPhpDirectory() {
365366
/**
366367
* Get the shell process.
367368
*
368-
* @return Drupal\Console\Core\Command\Exec\ExecCommand
369+
* @param bool $print
370+
* @return ShellProcess
369371
*/
370372
protected function getShellProcess() {
371-
return $this->container->get('console.shell_process');
373+
$app_root = $this->container->get('app.root');
374+
return new ShellProcess($app_root);
372375
}
373376

374377
/**

src/Command/Site/Checkout/AbstractCheckoutCommand.php

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
use Symfony\Component\Console\Input\InputOption;
1313
use Symfony\Component\Console\Input\InputInterface;
1414
use Symfony\Component\Console\Output\OutputInterface;
15-
use DennisDigital\Drupal\Console\Command\Site\Exception\CommandException;
16-
use DennisDigital\Drupal\Console\Command\Site\BaseCommand;
15+
use DennisDigital\Drupal\Console\Command\Exception\CommandException;
16+
use DennisDigital\Drupal\Console\Command\Site\AbstractCommand;
1717

1818
/**
1919
* Class AbstractCheckoutCommand
2020
*
2121
* @package DennisDigital\Drupal\Console\Command\Site\Checkout
2222
*/
23-
abstract class AbstractCheckoutCommand extends BaseCommand {
23+
abstract class AbstractCheckoutCommand extends AbstractCommand {
2424

2525
/**
2626
* Stores repo information.
@@ -32,10 +32,17 @@ abstract class AbstractCheckoutCommand extends BaseCommand {
3232
/**
3333
* The branch/tag to checkout.
3434
*
35-
* @var string ref
35+
* @var string
3636
*/
3737
protected $ref;
3838

39+
/**
40+
* Current branch/tag
41+
*
42+
* @var string
43+
*/
44+
protected $currentRef;
45+
3946
/**
4047
* {@inheritdoc}
4148
*/
@@ -44,10 +51,10 @@ protected function configure() {
4451

4552
// Custom options.
4653
$this->addOption(
47-
'ignore-changes',
54+
'force',
4855
'',
4956
InputOption::VALUE_NONE,
50-
'Ignore local changes when checking out the site'
57+
'Will force the checkout and replace all local changes'
5158
);
5259
}
5360

@@ -59,6 +66,8 @@ protected function interact(InputInterface $input, OutputInterface $output) {
5966

6067
// Validate repo.
6168
$this->validateRepo();
69+
70+
$this->currentRef = $this->getCurrentRef();
6271
}
6372

6473
/**
@@ -69,6 +78,48 @@ protected function execute(InputInterface $input, OutputInterface $output) {
6978

7079
// Validate repo.
7180
$this->validateRepo();
81+
82+
// Validate ref.
83+
$this->ref = $this->getRef($input);
84+
85+
if ($this->ref == $this->currentRef) {
86+
$this->io->commentBlock('Current branch/tag selected, skipping checkout command.');
87+
return;
88+
}
89+
90+
$this->io->comment(sprintf('Checking out %s (%s) on %s',
91+
$this->siteName,
92+
$this->ref,
93+
$this->destination
94+
));
95+
96+
switch ($this->repo['type']) {
97+
case 'git':
98+
// Check if repo exists and has any changes.
99+
if ($this->fileExists($this->destination) &&
100+
$this->fileExists($this->destination . '.' . $this->repo['type'])
101+
) {
102+
if ($input->hasOption('force') &&
103+
!$input->getOption('force')
104+
) {
105+
// Check for uncommitted changes.
106+
$this->gitDiff();
107+
}
108+
// Check out ref on existing repo.
109+
$this->gitCheckout();
110+
}
111+
else {
112+
// Clone repo.
113+
$this->gitClone();
114+
}
115+
break;
116+
117+
default:
118+
$message = sprintf('%s is not supported.',
119+
$this->repo['type']
120+
);
121+
throw new CommandException($message);
122+
}
72123
}
73124

74125
/**
@@ -108,7 +159,7 @@ protected function gitDiff() {
108159
if (!empty($shellProcess->getOutput())) {
109160
$message = sprintf('You have uncommitted changes on %s' . PHP_EOL .
110161
'Please commit or revert your changes before checking out the site.' . PHP_EOL .
111-
'If you want to check out the site without committing the changes use --ignore-changes.',
162+
'If you want to wipe your local changes use --force.',
112163
$this->destination
113164
);
114165
throw new CommandException($message);
@@ -163,7 +214,7 @@ protected function gitCheckout() {
163214
'git fetch --all && ' .
164215
'chmod 777 web/sites/default && ' .
165216
'chmod 777 web/sites/default/settings.php && ' .
166-
'git checkout %s ',
217+
'git checkout %s --force',
167218
$this->shellPath($this->destination),
168219
$this->ref
169220
);
@@ -181,4 +232,35 @@ protected function gitCheckout() {
181232
return TRUE;
182233
}
183234

235+
/**
236+
* Helper to retrieve the current working branch on the site's directory.
237+
*
238+
* @return mixed
239+
*/
240+
protected function getCurrentRef() {
241+
if ($this->fileExists($this->destination)) {
242+
// Get branch from site directory.
243+
$command = sprintf('cd %s && git branch',
244+
$this->shellPath($this->destination)
245+
);
246+
247+
$shellProcess = $this->getShellProcess()->printOutput(FALSE);
248+
249+
if ($shellProcess->exec($command, TRUE)) {
250+
preg_match_all("|\*\s(.*)|", $shellProcess->getOutput(), $matches);
251+
if (!empty($matches[1] && is_array($matches[1]))) {
252+
$match = explode(' ', trim(reset($matches[1]), '()'));
253+
return array_pop($match);
254+
}
255+
}
256+
}
257+
}
258+
259+
/**
260+
* Get the requested ref (tag/branch).
261+
*
262+
* @param InputInterface $input
263+
* @return string
264+
*/
265+
abstract protected function getRef(InputInterface $input);
184266
}

src/Command/Site/Checkout/BranchCommand.php

Lines changed: 13 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Console\Input\InputOption;
1313
use Symfony\Component\Console\Input\InputInterface;
1414
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Process\Process;
1516
use DennisDigital\Drupal\Console\Command\Exception\CommandException;
1617

1718
/**
@@ -20,12 +21,6 @@
2021
* @package DennisDigital\Drupal\Console\Command\Site\Checkout
2122
*/
2223
class BranchCommand extends AbstractCheckoutCommand {
23-
/**
24-
* Stores current branch of the checked out code.
25-
*
26-
* @var array currentBranch.
27-
*/
28-
protected $currentBranch;
2924

3025
/**
3126
* {@inheritdoc}
@@ -53,22 +48,21 @@ protected function interact(InputInterface $input, OutputInterface $output) {
5348

5449
$remoteBranches = $this->getRemoteBranches();
5550
$defaultBranch = $this->getDefaultBranch();
56-
$this->currentBranch = $this->getCurrentBranch();
5751

5852
$branch = $input->getOption('branch');
5953
if (!$branch) {
6054

6155
$options = array_values(array_unique(array_merge(
6256
['8.x'],
6357
[$defaultBranch],
64-
[$this->currentBranch],
58+
[$this->currentRef],
6559
$remoteBranches
6660
)));
6761

6862
$branch = $this->io->choice(
6963
$this->trans('Select a branch'),
7064
$options,
71-
isset($this->currentBranch) ? $this->currentBranch : $defaultBranch,
65+
isset($this->currentRef) ? $this->currentRef : $defaultBranch,
7266
TRUE
7367
);
7468

@@ -77,80 +71,31 @@ protected function interact(InputInterface $input, OutputInterface $output) {
7771
}
7872

7973
/**
80-
* {@inheritdoc}
81-
*/
82-
protected function execute(InputInterface $input, OutputInterface $output) {
83-
parent::execute($input, $output);
84-
85-
// Validate branch.
86-
$this->validateBranch($input);
87-
88-
if ($this->ref == $this->currentBranch) {
89-
$this->io->commentBlock('Current branch selected, skipping checkout command.');
90-
return;
91-
}
92-
93-
$this->io->comment(sprintf('Checking out %s (%s) on %s',
94-
$this->siteName,
95-
$this->ref,
96-
$this->destination
97-
));
98-
99-
switch ($this->repo['type']) {
100-
case 'git':
101-
// Check if repo exists and has any changes.
102-
if ($this->fileExists($this->destination) &&
103-
$this->fileExists($this->destination . '.' . $this->repo['type'])
104-
) {
105-
if ($input->hasOption('ignore-changes') &&
106-
!$input->getOption('ignore-changes')
107-
) {
108-
// Check for uncommitted changes.
109-
$this->gitDiff();
110-
}
111-
// Check out branch on existing repo.
112-
$this->gitCheckout();
113-
}
114-
else {
115-
// Clone repo.
116-
$this->gitClone();
117-
}
118-
break;
119-
120-
default:
121-
$message = sprintf('%s is not supported.',
122-
$this->repo['type']
123-
);
124-
throw new CommandException($message);
125-
}
126-
}
127-
128-
/**
129-
* Helper to validate branch option.
130-
*
131-
* @param InputInterface $input
74+
* Get branch option.
13275
*
133-
* @throws CommandException
76+
* @inheritdoc
13477
*/
135-
protected function validateBranch(InputInterface $input) {
78+
protected function getRef(InputInterface $input) {
13679
if ($input->hasOption('branch') &&
13780
!is_null($input->getOption('branch'))
13881
) {
13982
// Use config from parameter.
140-
$this->ref = $input->getOption('branch');
83+
$ref = $input->getOption('branch');
14184
}
14285
elseif (isset($this->config['repo']['branch'])) {
14386
// Use config from sites.yml.
144-
$this->ref = $this->config['repo']['branch'];
87+
$ref = $this->config['repo']['branch'];
14588
}
14689
else {
147-
$this->ref = '8.x';
90+
$ref = '8.x';
14891
}
14992

15093
// Update input.
15194
if ($input->hasOption('branch')) {
152-
$input->setOption('branch', $this->ref);
95+
$input->setOption('branch', $ref);
15396
}
97+
98+
return $ref;
15499
}
155100

156101
/**
@@ -164,8 +109,7 @@ protected function getRemoteBranches() {
164109
$this->repo['url']
165110
);
166111

167-
$shellProcess = $this->getShellProcess();
168-
112+
$shellProcess = $this->getShellProcess()->printOutput(FALSE);
169113
if ($shellProcess->exec($command, TRUE)) {
170114
preg_match_all("|refs/heads/(.*)|", $shellProcess->getOutput(), $matches);
171115
if (!empty($matches[1] && is_array($matches[1]))) {
@@ -174,7 +118,6 @@ protected function getRemoteBranches() {
174118
}
175119
else {
176120
throw new CommandException($shellProcess->getOutput());
177-
178121
}
179122
}
180123

@@ -191,27 +134,4 @@ protected function getDefaultBranch() {
191134
}
192135
}
193136

194-
/**
195-
* Helper to retrieve the current working branch on the site's directory.
196-
*
197-
* @return mixed
198-
*/
199-
protected function getCurrentBranch() {
200-
if ($this->fileExists($this->destination)) {
201-
// Get branch from site directory.
202-
$command = sprintf('cd %s && git branch',
203-
$this->shellPath($this->destination)
204-
);
205-
206-
$shellProcess = $this->getShellProcess();
207-
208-
if ($shellProcess->exec($command, TRUE)) {
209-
preg_match_all("|\*\s(.*)|", $shellProcess->getOutput(), $matches);
210-
if (!empty($matches[1] && is_array($matches[1]))) {
211-
return reset($matches[1]);
212-
}
213-
}
214-
}
215-
}
216-
217137
}

0 commit comments

Comments
 (0)