1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mediact\TestingSuite\Composer\Installer; |
8
|
|
|
|
9
|
|
|
use Composer\IO\IOInterface; |
10
|
|
|
use Mediact\Composer\FileInstaller; |
11
|
|
|
use Mediact\FileMapping\UnixFileMapping; |
12
|
|
|
use Mediact\TestingSuite\Composer\Factory\ProcessFactoryInterface; |
13
|
|
|
|
14
|
|
|
class PipelinesInstaller implements InstallerInterface |
15
|
|
|
{ |
16
|
|
|
/** @var FileInstaller */ |
17
|
|
|
private $fileInstaller; |
18
|
|
|
|
19
|
|
|
/** @var IOInterface */ |
20
|
|
|
private $io; |
21
|
|
|
|
22
|
|
|
/** @var ProcessFactoryInterface */ |
23
|
|
|
private $processFactory; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $destination; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $pattern = 'bitbucket.org'; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $filename = 'bitbucket-pipelines.yml'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param FileInstaller $fileInstaller |
38
|
|
|
* @param IOInterface $io |
39
|
|
|
* @param ProcessFactoryInterface $processFactory |
40
|
|
|
* @param string|null $destination |
41
|
|
|
* @param string|null $pattern |
42
|
|
|
* @param string|null $filename |
43
|
|
|
*/ |
44
|
4 |
|
public function __construct( |
45
|
|
|
FileInstaller $fileInstaller, |
46
|
|
|
IOInterface $io, |
47
|
|
|
ProcessFactoryInterface $processFactory, |
48
|
|
|
string $destination = null, |
49
|
|
|
string $pattern = null, |
50
|
|
|
string $filename = null |
51
|
|
|
) { |
52
|
4 |
|
$this->fileInstaller = $fileInstaller; |
53
|
4 |
|
$this->io = $io; |
54
|
4 |
|
$this->processFactory = $processFactory; |
55
|
4 |
|
$this->destination = $destination ?? getcwd(); |
56
|
4 |
|
$this->pattern = $pattern ?? $this->pattern; |
57
|
4 |
|
$this->filename = $filename ?? $this->filename; |
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Install. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
4 |
|
public function install() |
66
|
|
|
{ |
67
|
4 |
|
if (file_exists($this->destination . '/' . $this->filename) |
68
|
3 |
|
|| !$this->isBitbucket() |
69
|
|
|
) { |
70
|
2 |
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$mapping = new UnixFileMapping( |
74
|
2 |
|
__DIR__ . '/../../templates/files/pipelines', |
75
|
2 |
|
$this->destination, |
76
|
2 |
|
$this->filename |
77
|
|
|
); |
78
|
|
|
|
79
|
2 |
|
$this->fileInstaller->installFile($mapping); |
80
|
2 |
|
$this->io->write( |
81
|
2 |
|
sprintf( |
82
|
2 |
|
'<info>Installed:</info> %s', |
83
|
2 |
|
$mapping->getRelativeDestination() |
84
|
|
|
) |
85
|
|
|
); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check whether the project is on Bitbucket. |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
3 |
|
private function isBitbucket(): bool |
94
|
|
|
{ |
95
|
3 |
|
$process = $this->processFactory->create('git remote -v'); |
96
|
3 |
|
$process->run(); |
97
|
|
|
|
98
|
3 |
|
return strpos($process->getOutput(), $this->pattern) !== false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|