Skip to content

Commit 0d2482a

Browse files
committed
Refactor for better variable names
1 parent 046fddf commit 0d2482a

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

admin/create-new-changelog.php

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,83 @@ function replace_file_content(string $path, string $pattern, string $replace): v
99
file_put_contents($path, $output);
1010
}
1111

12-
// Main.
1312
chdir(__DIR__ . '/..');
1413

15-
if ($argc !== 3) {
16-
echo "Usage: php {$argv[0]} <current_version> <new_version>" . PHP_EOL;
17-
echo "E.g.,: php {$argv[0]} 4.4.3 4.4.4" . PHP_EOL;
14+
if (! isset($argv[1]) || ! isset($argv[2])) {
15+
echo "Usage: php {$argv[0]} <current_version> <new_version> [--dry-run]\n";
16+
echo "E.g. : php {$argv[0]} 4.4.3 4.4.4 --dry-run\n";
1817

1918
exit(1);
2019
}
2120

2221
// Gets version number from argument.
23-
$versionCurrent = $argv[1]; // e.g., '4.4.3'
24-
$versionCurrentParts = explode('.', $versionCurrent);
25-
$minorCurrent = $versionCurrentParts[0] . '.' . $versionCurrentParts[1];
26-
$version = $argv[2]; // e.g., '4.4.4'
27-
$versionParts = explode('.', $version);
28-
$minor = $versionParts[0] . '.' . $versionParts[1];
29-
$isMinorUpdate = ($minorCurrent !== $minor);
30-
31-
// Creates a branch for release.
32-
if (! $isMinorUpdate) {
33-
system('git switch develop');
22+
$currentVersion = $argv[1]; // e.g., '4.4.3'
23+
$currentVersionParts = explode('.', $currentVersion, 3);
24+
$currentMinorVersion = $currentVersionParts[0] . '.' . $currentVersionParts[1];
25+
$newVersion = $argv[2]; // e.g., '4.4.4'
26+
$newVersionParts = explode('.', $newVersion, 3);
27+
$newMinorVersion = $newVersionParts[0] . '.' . $newVersionParts[1];
28+
$isMinorUpdate = $currentMinorVersion !== $newMinorVersion;
29+
30+
// Creates a branch for release
31+
if (! in_array('--dry-run', $argv, true)) {
32+
if (! $isMinorUpdate) {
33+
system('git switch develop');
34+
}
35+
36+
system("git switch -c docs-changelog-{$newVersion}");
37+
system("git switch docs-changelog-{$newVersion}");
3438
}
35-
system('git switch -c docs-changelog-' . $version);
36-
system('git switch docs-changelog-' . $version);
3739

3840
// Copy changelog
39-
$changelog = "./user_guide_src/source/changelogs/v{$version}.rst";
41+
$newChangelog = "./user_guide_src/source/changelogs/v{$newVersion}.rst";
4042
$changelogIndex = './user_guide_src/source/changelogs/index.rst';
43+
4144
if ($isMinorUpdate) {
42-
copy('./admin/next-changelog-minor.rst', $changelog);
45+
copy('./admin/next-changelog-minor.rst', $newChangelog);
4346
} else {
44-
copy('./admin/next-changelog-patch.rst', $changelog);
47+
copy('./admin/next-changelog-patch.rst', $newChangelog);
4548
}
49+
4650
// Add changelog to index.rst.
4751
replace_file_content(
4852
$changelogIndex,
4953
'/\.\. toctree::\n :titlesonly:\n/u',
50-
".. toctree::\n :titlesonly:\n\n v{$version}",
54+
".. toctree::\n :titlesonly:\n\n v{$newVersion}",
5155
);
56+
5257
// Replace {version}
53-
$length = mb_strlen("Version {$version}");
54-
$underline = str_repeat('#', $length);
58+
$underline = str_repeat('#', mb_strlen("Version {$newVersion}"));
5559
replace_file_content(
56-
$changelog,
60+
$newChangelog,
5761
'/#################\nVersion {version}\n#################/u',
58-
"{$underline}\nVersion {$version}\n{$underline}",
59-
);
60-
replace_file_content(
61-
$changelog,
62-
'/{version}/u',
63-
"{$version}",
62+
"{$underline}\nVersion {$newVersion}\n{$underline}",
6463
);
64+
replace_file_content($newChangelog, '/{version}/u', $newVersion);
6565

6666
// Copy upgrading
67-
$versionWithoutDots = str_replace('.', '', $version);
68-
$upgrading = "./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst";
67+
$versionWithoutDots = str_replace('.', '', $newVersion);
68+
$newUpgrading = "./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst";
6969
$upgradingIndex = './user_guide_src/source/installation/upgrading.rst';
70-
copy('./admin/next-upgrading-guide.rst', $upgrading);
70+
copy('./admin/next-upgrading-guide.rst', $newUpgrading);
71+
7172
// Add upgrading to upgrading.rst.
7273
replace_file_content(
7374
$upgradingIndex,
7475
'/ backward_compatibility_notes\n/u',
7576
" backward_compatibility_notes\n\n upgrade_{$versionWithoutDots}",
7677
);
78+
7779
// Replace {version}
78-
$length = mb_strlen("Upgrading from {$versionCurrent} to {$version}");
79-
$underline = str_repeat('#', $length);
80+
$underline = str_repeat('#', mb_strlen("Upgrading from {$currentVersion} to {$newVersion}"));
8081
replace_file_content(
81-
$upgrading,
82+
$newUpgrading,
8283
'/##############################\nUpgrading from {version} to {version}\n##############################/u',
83-
"{$underline}\nUpgrading from {$versionCurrent} to {$version}\n{$underline}",
84+
"{$underline}\nUpgrading from {$currentVersion} to {$newVersion}\n{$underline}",
8485
);
8586

86-
// Commits
87-
system("git add {$changelog} {$changelogIndex}");
88-
system("git add {$upgrading} {$upgradingIndex}");
89-
system('git commit -m "docs: add changelog and upgrade for v' . $version . '"');
87+
if (! in_array('--dry-run', $argv, true)) {
88+
system("git add {$newChangelog} {$changelogIndex}");
89+
system("git add {$newUpgrading} {$upgradingIndex}");
90+
system("git commit -m \"docs: add changelog and upgrade for v{$newVersion}\"");
91+
}

0 commit comments

Comments
 (0)