This repository was archived by the owner on Dec 5, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposerPackageCommand.php
More file actions
152 lines (129 loc) · 4.8 KB
/
ComposerPackageCommand.php
File metadata and controls
152 lines (129 loc) · 4.8 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
<?php
/**
* SebSept Ps_dev_base - Tools for quality Prestashop Module development.
*
* Copyright (c) 2021 Sébastien Monterisi
*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/MIT
*
* @author Sébastien Monterisi <contact@seb7.fr>
* @copyright since 2021 Sébastien Monterisi
* @license https://opensource.org/licenses/MIT MIT License
*/
declare(strict_types=1);
namespace SebSept\PsDevToolsPlugin\Command;
use Composer\Package\Link;
use Exception;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class PsDevToolsPackageCommand.
*/
abstract class ComposerPackageCommand extends BaseCommand
{
/**
* @var OutputInterface
*/
private $output;
abstract public function getPackageName(): ?string;
abstract public function getPackageVersionConstraint(): ?string;
abstract public function isToolConfigured(): bool;
/**
* it configures the tool.
* It doesn't do checks, only perform addComposerScript(), file copy, etc.
*/
abstract public function configureTool(): void;
final protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->output = $output;
// -- installation / configuration / execution
try {
$isToolConfigured = $this->isToolConfigured();
$isPackageInstalled = $this->isPackageInstalled();
// state messages
$isPackageInstalled
? $this->getIO()->write(sprintf('%s is installed.', $this->getPackageName() ?? $this->getComposerScriptName()))
: $this->getIO()->error("{$this->getPackageName()} is not installed.");
$isToolConfigured
? $this->getIO()->write("{$this->getComposerScriptName()} is configured.")
: $this->getIO()->error("{$this->getComposerScriptName()} is not configured.");
// run
$runConfiguration = $input->getOption('reconfigure') || !$isToolConfigured;
$readyToRun = $isPackageInstalled && !$runConfiguration;
$isPackageInstalled ?: $this->installPackage();
!$runConfiguration ?: $this->configureTool();
if (!$readyToRun) {
$this->getIO()->write("<bg=green>{$this->getComposerScriptName()} is installed and configured.</>");
$this->getIO()->write("run the same command <comment>composer {$this->getName()}</comment> to run the tool.");
return 0;
}
$this->runComposerScript($output);
} catch (RuntimeException $exception) {
$this->getIO()->alert($exception->getMessage());
return 7;
} catch (Exception $exception) {
$this->getIO()->critical($exception->getMessage());
return 1;
}
return 0;
}
/**
* Common options.
*/
protected function configure(): void
{
$this->addOption('reconfigure', null, InputOption::VALUE_NONE, 'rerun configuration');
}
final protected function isPackageInstalled(): bool
{
return is_null($this->getPackageName())
|| array_key_exists($this->getPackageName(), $this->getInstalledDevRequires());
}
/**
* @return int execution result code - 1 if no package
*
* @throws \Exception
*/
final protected function installPackage(): int
{
if (is_null($this->getPackageName())) {
return 1;
}
return $this->getApplication()->find('require')->run(
new ArrayInput([
'packages' => [sprintf('%s:%s', $this->getPackageName(), $this->getPackageVersionConstraint())],
'--dev' => true,
'--quiet' => true, // ne pas mettre quiet si (very)verbose
]),
$this->output
);
}
/**
* DevRequired packages as an array.
* Key is package name (eg. mypack/mypack)
* Value is the version constraint.
*
* @return array<string, string>
*/
private function getInstalledDevRequires(): array
{
if (is_null($this->getComposer())) {
return [];
}
$devRequires = array_map(
function (Link $require) {
return ['version' => $require->getPrettyConstraint(), 'package' => $require->getTarget()];
},
$this->getComposer()->getPackage()->getDevRequires()
);
return array_combine(array_column($devRequires, 'package'), array_column($devRequires, 'version')) ?: [];
}
}