|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MonorepoBuilder; |
|
6
|
|
|
|
|
7
|
|
|
use PharIo\Version\Version; |
|
8
|
|
|
use Symplify\MonorepoBuilder\Exception\Git\InvalidGitVersionException; |
|
9
|
|
|
use Symplify\MonorepoBuilder\Release\Contract\ReleaseWorker\ReleaseWorkerInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class ValidateVersionReleaseWorker implements ReleaseWorkerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct( |
|
14
|
|
|
private readonly TagParserInterface $parser, |
|
15
|
|
|
private ?string $gitDirectory = null |
|
16
|
|
|
) { |
|
17
|
|
|
if ($gitDirectory === null) { |
|
18
|
|
|
$this->gitDirectory = \dirname(__DIR__); |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getDescription(Version $version): string |
|
23
|
|
|
{ |
|
24
|
|
|
return \sprintf( |
|
25
|
|
|
'Checking if the version [%s] is greater than the latest released version.', |
|
26
|
|
|
$version->getVersionString() |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function work(Version $version): void |
|
31
|
|
|
{ |
|
32
|
|
|
$mostRecentVersion = $this->findMostRecentVersion($version); |
|
33
|
|
|
|
|
34
|
|
|
// no tag yet |
|
35
|
|
|
if ($mostRecentVersion === null) { |
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// validation |
|
40
|
|
|
$mostRecentVersion = new Version(\strtolower($mostRecentVersion)); |
|
41
|
|
|
if ($version->isGreaterThan($mostRecentVersion)) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
throw new InvalidGitVersionException(\sprintf( |
|
46
|
|
|
'Provided version "%s" must be greater than the last one: "%s"', |
|
47
|
|
|
$version->getVersionString(), |
|
48
|
|
|
$mostRecentVersion->getVersionString() |
|
49
|
|
|
)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function findMostRecentVersion(Version $version): ?string |
|
53
|
|
|
{ |
|
54
|
|
|
$tags = []; |
|
55
|
|
|
foreach ($this->parser->parse($this->gitDirectory) as $tag) { |
|
|
|
|
|
|
56
|
|
|
$tag = new Version(\strtolower($tag)); |
|
57
|
|
|
|
|
58
|
|
|
// all previous major versions |
|
59
|
|
|
if ($version->getMajor()->getValue() > $tag->getMajor()->getValue()) { |
|
60
|
|
|
$tags[] = $tag; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// all minor versions up to the requested in the requested major version |
|
64
|
|
|
if ($version->getMajor()->getValue() === $tag->getMajor()->getValue()) { |
|
65
|
|
|
if ($version->getMinor()->getValue() >= $tag->getMinor()->getValue()) { |
|
66
|
|
|
$tags[] = $tag; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($tags === []) { |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
\usort($tags, static fn (Version $a, Version $b) => $a->isGreaterThan($b) ? -1 : 1); |
|
76
|
|
|
|
|
77
|
|
|
return $tags[0]->getVersionString(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|