forked from opentelemetry-php/dev-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatePackagesCommand.php
More file actions
106 lines (84 loc) · 3.15 KB
/
ValidatePackagesCommand.php
File metadata and controls
106 lines (84 loc) · 3.15 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
<?php
declare(strict_types=1);
namespace OpenTelemetry\DevTools\Console\Command\Packages;
use Composer\Command\ValidateCommand;
use Composer\Util\Filesystem;
use OpenTelemetry\DevTools\Console\Command\BaseCommand;
use OpenTelemetry\DevTools\Console\Command\CommandRunner;
use OpenTelemetry\DevTools\Console\Command\Packages\Behavior\UsesThirdPartyCommandTrait;
use OpenTelemetry\DevTools\Package\Composer\ConfigResolverInterface;
use OpenTelemetry\DevTools\Util\WorkingDirectoryResolver;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
class ValidatePackagesCommand extends BaseCommand
{
use UsesThirdPartyCommandTrait;
public const NAME = 'packages:composer:validate';
public const DESCRIPTION = 'Validates composer files of the individual packages';
private ConfigResolverInterface $resolver;
public function __construct(ConfigResolverInterface $resolver, ?CommandRunner $commandRunner = null)
{
parent::__construct(self::NAME);
$this->resolver = $resolver;
$this->commandRunner = $commandRunner ?? CommandRunner::create();
}
#[\Override]
protected function configure(): void
{
$this->setDescription(self::DESCRIPTION);
}
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->registerInputAndOutput($input, $output);
$this->writeIntro();
$configs = $this->resolver->resolve();
foreach ($configs as $directory => $composerFile) {
try {
$this->writeBlankLine();
$this->writeSection(sprintf(
'Validating: %s',
$composerFile
));
$res = $this->runValidateCommand($composerFile, $directory);
if ($res !== 0) {
$this->writeError(sprintf(
'Composer file is invalid %s',
$composerFile
));
return self::FAILURE;
}
$this->writeOk();
} catch (Throwable $t) {
$this->writeThrowable($t);
return self::FAILURE;
}
}
return self::SUCCESS;
}
private function runValidateCommand(string $composerFile, string $directory): int
{
if (!(new Filesystem())->isAbsolutePath($composerFile)) {
$composerFile = WorkingDirectoryResolver::create()->resolve() . '/' . $composerFile;
}
return $this->createAndRunCommand(
ValidateCommand::class,
new ArrayInput([
'file' => $composerFile,
]),
new ConsoleOutput(
OutputInterface::VERBOSITY_DEBUG
),
$directory,
);
}
private function writeIntro(): void
{
$this->writeTitle();
$this->writeSection('Validating composer files in: ' . WorkingDirectoryResolver::create()->resolve());
$this->writeBlankLine();
}
}