-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrease-version.php
More file actions
88 lines (71 loc) · 2.41 KB
/
decrease-version.php
File metadata and controls
88 lines (71 loc) · 2.41 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
<?php
declare(strict_types=1);
define('WORK_DIR', dirname(__DIR__));
function decrement_major_version(string $version): string
{
if (!preg_match('/^(\d+)\.(\d+)\.(\d+)$/', $version, $matches)) {
throw new RuntimeException(sprintf('Invalid version string: %s', $version));
}
$major = (int) $matches[1];
$minor = $matches[2];
$patch = $matches[3];
if ($major < 1) {
throw new RuntimeException(sprintf('Cannot decrease major version for: %s', $version));
}
return sprintf('%d.%s.%s', $major - 1, $minor, $patch);
}
function update_file(string $relativePath, string $pattern, callable $callback, bool $requireMatch = true): void
{
$path = WORK_DIR . '/' . $relativePath;
$content = file_get_contents($path);
if ($content === false) {
throw new RuntimeException(sprintf('Unable to read file: %s', $relativePath));
}
$count = 0;
$updated = preg_replace_callback($pattern, $callback, $content, -1, $count);
if ($updated === null) {
throw new RuntimeException(sprintf('Regex error while updating: %s', $relativePath));
}
if ($requireMatch && $count === 0) {
throw new RuntimeException(sprintf('Pattern not found in file: %s', $relativePath));
}
if (file_put_contents($path, $updated) === false) {
throw new RuntimeException(sprintf('Unable to write file: %s', $relativePath));
}
}
update_file(
'constant.php',
"/public const VERSION = '(\d+\.\d+\.\d+)';/",
static function (array $matches): string {
return "public const VERSION = '" . decrement_major_version($matches[1]) . "';";
}
);
update_file(
'yabe-webfont.php',
'/\* Version:\s+(\d+\.\d+\.\d+)/',
static function (array $matches): string {
return '* Version: ' . decrement_major_version($matches[1]);
}
);
update_file(
'readme.txt',
'/Stable tag: (\d+\.\d+\.\d+)/',
static function (array $matches): string {
return 'Stable tag: ' . decrement_major_version($matches[1]);
}
);
update_file(
'readme.txt',
'/= (\d+\.\d+\.\d+) - ([0-9-]+) =/',
static function (array $matches): string {
return '= ' . decrement_major_version($matches[1]) . ' - ' . $matches[2] . ' =';
}
);
update_file(
'readme.txt',
'/= (\d+\.\d+\.\d+) =/',
static function (array $matches): string {
return '= ' . decrement_major_version($matches[1]) . ' =';
},
false
);