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 Symfony\Component\Process\ProcessBuilder; |
13
|
|
|
|
14
|
|
|
class PipelinesInstaller implements InstallerInterface |
15
|
|
|
{ |
16
|
|
|
/** @var FileInstaller */ |
17
|
|
|
private $fileInstaller; |
18
|
|
|
|
19
|
|
|
/** @var IOInterface */ |
20
|
|
|
private $io; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $pattern = 'bitbucket.org'; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $filename = 'bitbucket-pipelines.yml'; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
private $types = [ |
30
|
|
|
'mediact' => 'MediaCT pipelines script', |
31
|
|
|
'basic' => 'Basic pipelines script' |
32
|
|
|
]; |
33
|
|
|
/** |
34
|
|
|
* @var null|string |
35
|
|
|
*/ |
36
|
|
|
private $destination; |
37
|
|
|
/** |
38
|
|
|
* @var ProcessBuilder |
39
|
|
|
*/ |
40
|
|
|
private $processBuilder; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor. |
44
|
|
|
* |
45
|
|
|
* @param FileInstaller $fileInstaller |
46
|
|
|
* @param IOInterface $io |
47
|
|
|
* @param ProcessBuilder $processBuilder |
48
|
|
|
* @param string|null $destination |
49
|
|
|
* @param string|null $pattern |
50
|
|
|
* @param string|null $filename |
51
|
|
|
* @param array|null $types |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
FileInstaller $fileInstaller, |
55
|
|
|
IOInterface $io, |
56
|
|
|
ProcessBuilder $processBuilder = null, |
57
|
|
|
string $destination = null, |
58
|
|
|
string $pattern = null, |
59
|
|
|
string $filename = null, |
60
|
|
|
array $types = null |
61
|
|
|
) { |
62
|
|
|
$this->fileInstaller = $fileInstaller; |
63
|
|
|
$this->io = $io; |
64
|
|
|
$this->processBuilder = $processBuilder ?: new ProcessBuilder(); |
65
|
|
|
$this->destination = $destination ?: getcwd(); |
66
|
|
|
|
67
|
|
|
$pattern !== null && $this->pattern = $pattern; |
68
|
|
|
$filename !== null && $this->filename = $filename; |
69
|
|
|
$types !== null && $this->types = $types; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Install. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function install() |
78
|
|
|
{ |
79
|
|
|
if (file_exists($this->destination . '/' . $this->filename) |
80
|
|
|
|| !$this->isBitbucket() |
81
|
|
|
) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
$mapping = new UnixFileMapping( |
87
|
|
|
sprintf( |
88
|
|
|
__DIR__ . '/../../templates/files/pipelines-%s', |
89
|
|
|
$this->chooseMapping() |
90
|
|
|
), |
91
|
|
|
$this->destination, |
|
|
|
|
92
|
|
|
$this->filename |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->fileInstaller->installFile($mapping); |
96
|
|
|
$this->io->write( |
97
|
|
|
sprintf( |
98
|
|
|
'<info>Installed:</info> %s', |
99
|
|
|
$mapping->getRelativeDestination() |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Choose the mapping to install. |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
private function chooseMapping() |
110
|
|
|
{ |
111
|
|
|
$labels = array_values($this->types); |
112
|
|
|
$keys = array_keys($this->types); |
113
|
|
|
|
114
|
|
|
$selected = $this->io->select( |
115
|
|
|
'Bitbucket has been detected. Which pipelines script do you want to install?', |
116
|
|
|
$labels, |
117
|
|
|
0 |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
return $keys[$selected]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check whether the project is on Bitbucket. |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
private function isBitbucket(): bool |
129
|
|
|
{ |
130
|
|
|
$process = $this->processBuilder |
131
|
|
|
->setPrefix('git remote -v') |
132
|
|
|
->getProcess(); |
133
|
|
|
|
134
|
|
|
$process->run(); |
135
|
|
|
return strpos($process->getOutput(), $this->pattern) !== false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|