-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
264 lines (220 loc) · 7.88 KB
/
Copy pathPlugin.php
File metadata and controls
264 lines (220 loc) · 7.88 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
declare(strict_types=1);
namespace PHPCensor\Common\Plugin;
use PHPCensor\Common\Application\ApplicationInterface;
use PHPCensor\Common\Build\BuildErrorWriterInterface;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Build\BuildLoggerInterface;
use PHPCensor\Common\Build\BuildMetaWriterInterface;
use PHPCensor\Common\CommandExecutorInterface;
use PHPCensor\Common\ParameterBag;
use PHPCensor\Common\Project\ProjectInterface;
use PHPCensor\Common\VariableInterpolatorInterface;
use Psr\Container\ContainerInterface;
/**
* @package PHP Censor
* @subpackage Common Library
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
abstract class Plugin implements PluginInterface
{
protected ParameterBag $options;
protected ParameterBag $buildSettings;
/**
* Working directory for plugin (Directory with files for inspecting or directory with tests).
* For example: `composer.phar --working-dir="<directory>"` install
*/
protected string $directory;
/**
* Array of ignoring files and directories. For example: ['.gitkeep', 'tests'].
*
* @var string[]
*/
protected array $ignores;
/**
* Path for searching plugin binary (executable). For example: '/home/user/bin/'.
*/
protected string $binaryPath;
/**
* Names of the binary (executable) for searching in the binary path or in the build directory.
*/
protected array $binaryNames = [];
protected string $artifactsPluginPath;
protected string $artifactsPluginBranchPath;
protected string $artifactsPluginLink;
protected string $artifactsPluginBranchLink;
/**
* @throws \Throwable
*/
public function __construct(
protected BuildInterface $build,
protected ProjectInterface $project,
protected BuildLoggerInterface $buildLogger,
protected BuildErrorWriterInterface $buildErrorWriter,
protected BuildMetaWriterInterface $buildMetaWriter,
protected CommandExecutorInterface $commandExecutor,
protected VariableInterpolatorInterface $variableInterpolator,
protected PathResolverInterface $pathResolver,
protected ApplicationInterface $application,
protected ContainerInterface $container
) {
$this->initOptions();
$this->initBuildSettings();
$this->directory = $this->pathResolver->resolveDirectory(
(string)$this->options->get('directory', '')
);
$this->ignores = $this->pathResolver->resolveIgnores(
(array)$this->options->get('ignore', [])
);
$this->binaryPath = $this->pathResolver->resolveBinaryPath(
(string)$this->options->get('binary_path', '')
);
/**
* Example: /var/www/php-censor.localhost/public/artifacts/phpunit/2/10_xxxxxxxx/
* Where: Project Id: 2, Build Id: 10
*/
$this->artifactsPluginPath = \sprintf(
'%s%s/%s/',
$this->application->getArtifactsPath(),
static::getName(),
$this->build->getBuildDirectory()
);
/**
* Example: /var/www/php-censor.localhost/public/artifacts/phpunit/2/master_xxxxxxxx/
* Where: Project Id: 2, Branch: "master"
*/
$this->artifactsPluginBranchPath = \sprintf(
'%s%s/%s/',
$this->application->getArtifactsPath(),
static::getName(),
$this->build->getBuildBranchDirectory()
);
/**
* Example: https://php-censor.localhost/artifacts/phpunit/2/10_xxxxxxxx
* Where: Project Id: 2, Build Id: 10
*/
$this->artifactsPluginLink = \sprintf(
'%s%s/%s',
$this->application->getArtifactsLink(),
static::getName(),
$this->build->getBuildDirectory()
);
/**
* Example: https://php-censor.localhost/artifacts/phpunit/2/master_xxxxxxxx
* Where: Project Id: 2, Branch: "master"
*/
$this->artifactsPluginBranchLink = \sprintf(
'%s%s/%s',
$this->application->getArtifactsLink(),
static::getName(),
$this->build->getBuildBranchDirectory()
);
$this->initBinaryNames();
$this->initPluginSettings();
}
abstract public static function getName(): string;
/**
* {@inheritDoc}
*/
abstract public function execute(): bool;
public static function canExecute(string $stage, BuildInterface $build): bool
{
return false;
}
/**
* @throws \Exception
*/
protected function initOptions(): void
{
$projectConfig = $this->project->getBuildConfig();
$pluginName = static::getName();
$pluginOptionsArray = [];
if (!empty($projectConfig[$pluginName])) {
$pluginOptionsArray = $projectConfig[$pluginName];
}
$this->options = new ParameterBag($pluginOptionsArray);
$this->buildLogger->logDebug('Plugin options: ' . \json_encode($this->options->all()));
}
protected function initBuildSettings(): void
{
$projectConfig = $this->project->getBuildConfig();
$buildSettingArray = [];
if (!empty($projectConfig['build_settings'])) {
$buildSettingArray = $projectConfig['build_settings'];
}
$this->buildSettings = new ParameterBag($buildSettingArray);
}
protected function initBinaryNames(): void
{
$this->binaryNames = $this->getPluginDefaultBinaryNames();
if ($this->options->has('binary_name')) {
$binaryNames = $this->options->get('binary_name', []);
if (!\is_array($binaryNames)) {
$binaryNames = [$binaryNames];
}
$this->binaryNames = \array_unique(
\array_merge($binaryNames, $this->binaryNames)
);
}
$normalizedNames = [];
foreach ($this->binaryNames as $binaryName) {
if ($binaryName) {
$normalizedNames[] = $binaryName;
}
}
$this->binaryNames = $normalizedNames;
}
/**
* @throws \Throwable
*/
protected function initPluginSettings(): void
{
}
/**
* If plugin has binary (executable) method should return array of default binary names.
* For example: ['executable', 'executable.phar'].
*/
protected function getPluginDefaultBinaryNames(): array
{
return [];
}
/**
* Example: /var/www/php-censor.localhost/public/artifacts/phpunit/2/10_xxxxxxxx/report.xml
* Where: Project Id: 2, Build Id: 10, File: report.xml
*/
protected function getArtifactPath(string $file = ''): string
{
return \rtrim(\sprintf('%s%s', $this->artifactsPluginPath, $file), '/');
}
/**
* Example: /var/www/php-censor.localhost/public/artifacts/phpunit/2/master_xxxxxxxx/report.xml
* Where: Project Id: 2, Branch: "master", File: report.xml
*/
protected function getArtifactPathForBranch(string $file = ''): string
{
return \rtrim(\sprintf('%s%s', $this->artifactsPluginBranchPath, $file), '/');
}
/**
* Example: https://php-censor.localhost/artifacts/phpunit/2/10_xxxxxxxx/report.xml
* Where: Project Id: 2, Build Id: 10, File: report.xml
*/
protected function getArtifactLink(string $file = ''): string
{
if ($file) {
return \sprintf('%s/%s', $this->artifactsPluginLink, $file);
}
return $this->artifactsPluginLink;
}
/**
* Example: https://php-censor.localhost/artifacts/phpunit/2/master_xxxxxxxx/report.xml
* Where: Project Id: 2, Branch: "master", File: report.xml
*/
protected function getArtifactLinkForBranch(string $file = ''): string
{
if ($file) {
return \sprintf('%s/%s', $this->artifactsPluginBranchLink, $file);
}
return $this->artifactsPluginBranchLink;
}
}