|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the egabor/composer-release-plugin package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Gábor Egyed <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace egabor\Composer\ReleasePlugin; |
|
13
|
|
|
|
|
14
|
|
|
use egabor\Composer\ReleasePlugin\Exception\UnstableVersionException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Opinionated version manipulations. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Gábor Egyed <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class VersionManipulator |
|
22
|
|
|
{ |
|
23
|
|
|
private static $supportedLevels = [ |
|
24
|
|
|
'major', |
|
25
|
|
|
'minor', |
|
26
|
|
|
'patch', |
|
27
|
|
|
'stable', |
|
28
|
|
|
'alpha', |
|
29
|
|
|
'beta', |
|
30
|
|
|
'rc', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
public static function bump($level, Version $version) |
|
34
|
|
|
{ |
|
35
|
|
|
switch ($level) { |
|
36
|
|
|
case 'major': |
|
37
|
|
|
return self::bumpMajor($version); |
|
38
|
|
|
case 'minor': |
|
39
|
|
|
return self::bumpMinor($version); |
|
40
|
|
|
case 'patch': |
|
41
|
|
|
return self::bumpPatch($version); |
|
42
|
|
|
case 'stable': |
|
43
|
|
|
return self::toStable($version); |
|
44
|
|
|
case 'alpha': |
|
45
|
|
|
case 'beta': |
|
46
|
|
|
case 'rc': |
|
47
|
|
|
return self::bumpPre($version, $level); |
|
48
|
|
|
default: |
|
49
|
|
|
throw new \InvalidArgumentException(sprintf('Bumping "%s" version is not supported.', $level)); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function supportsLevel($level) |
|
54
|
|
|
{ |
|
55
|
|
|
return in_array((string) $level, self::$supportedLevels, true); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private static function bumpMajor(Version $current) |
|
59
|
|
|
{ |
|
60
|
|
|
if ('' !== $current->getPre() && '0' !== $current->getMinor()) { |
|
61
|
|
|
throw new UnstableVersionException('Need to release a stable version first.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ('' !== $current->getPre()) { |
|
65
|
|
|
return self::toStable($current); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new Version((int) $current->getMajor() + 1, 0, 0); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private static function bumpMinor(Version $current) |
|
72
|
|
|
{ |
|
73
|
|
|
if ('' !== $current->getPre() && '0' === $current->getMinor()) { |
|
74
|
|
|
throw new UnstableVersionException('Need to release a stable version first.'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ('' !== $current->getPre()) { |
|
78
|
|
|
return self::toStable($current); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return new Version($current->getMajor(), (int) $current->getMinor() + 1, 0); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private static function bumpPatch(Version $current) |
|
85
|
|
|
{ |
|
86
|
|
|
if ('' !== $current->getPre()) { |
|
87
|
|
|
throw new UnstableVersionException('Need to release a stable version first.'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return new Version($current->getMajor(), $current->getMinor(), (int) $current->getPatch() + 1); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private static function bumpPre(Version $current, $level) |
|
94
|
|
|
{ |
|
95
|
|
|
if ('' === $current->getPre() && '0' === $current->getMajor()) { |
|
96
|
|
|
return new Version(1, 0, 0, $level.'1'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ('' === $current->getPre()) { |
|
100
|
|
|
return new Version($current->getMajor(), (int) $current->getMinor() + 1, 0, $level.'1'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ($level === $current->getPreLevel()) { |
|
104
|
|
|
$pre = $level.$current->getPreSeparator().((int) $current->getPreNumber() + 1); |
|
105
|
|
|
} else { |
|
106
|
|
|
$pre = $level.$current->getPreSeparator().'1'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return new Version($current->getMajor(), $current->getMinor(), $current->getPatch(), $pre); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private static function toStable(Version $current) |
|
113
|
|
|
{ |
|
114
|
|
|
if ($current->isStable()) { |
|
115
|
|
|
throw new \LogicException(sprintf('"%s" version is already stable.', $current)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ('' !== $current->getPre()) { |
|
119
|
|
|
return new Version($current->getMajor(), $current->getMinor(), $current->getPatch()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return new Version((int) $current->getMajor() + 1, 0, 0); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|