-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCurrentProjectRootHandler.php
58 lines (50 loc) · 2.19 KB
/
CurrentProjectRootHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace DigipolisGent\Robo\Helpers\EventHandler\DefaultHandler;
use DigipolisGent\CommandBuilder\CommandBuilder;
use DigipolisGent\Robo\Helpers\EventHandler\AbstractTaskEventHandler;
use DigipolisGent\Robo\Task\Deploy\Ssh\Auth\KeyFile;
use Robo\Contract\ConfigAwareInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class CurrentProjectRootHandler extends AbstractTaskEventHandler implements ConfigAwareInterface
{
use \Consolidation\Config\ConfigAwareTrait;
use \DigipolisGent\Robo\Task\General\Common\DigipolisPropertiesAware;
use \DigipolisGent\Robo\Task\General\Tasks;
use \DigipolisGent\Robo\Task\Deploy\Tasks;
protected $projectRoots = [];
/**
* {@inheritDoc}
*/
public function handle(GenericEvent $event)
{
$host = $event->getArgument('host');
$user = $event->getArgument('user');
$privateKeyFile = $event->getArgument('privateKeyFile');
$remoteSettings = $event->getArgument('remoteSettings');
$key = $host . ':' . $user . ':' . $privateKeyFile . ':' . $remoteSettings['releasesdir'];
$auth = new KeyFile($user, $privateKeyFile);
if (!array_key_exists($key, $this->projectRoots)) {
$fullOutput = '';
$this->taskSsh($host, $auth)
->remoteDirectory($remoteSettings['releasesdir'], true)
->exec(
(string) CommandBuilder::create('ls')
->addFlag('1')
->pipeOutputTo(
CommandBuilder::create('sort')
->addFlag('r')
->pipeOutputTo(
CommandBuilder::create('head')
->addFlag('1')
)
),
function ($output) use (&$fullOutput) {
$fullOutput .= $output;
}
)
->run();
$this->projectRoots[$key] = $remoteSettings['releasesdir'] . '/' . substr($fullOutput, 0, (strpos($fullOutput, "\n") ?: strlen($fullOutput)));
}
return $this->projectRoots[$key];
}
}