|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +function replace_file_content(string $path, string $pattern, string $replace): void |
| 6 | +{ |
| 7 | + $file = file_get_contents($path); |
| 8 | + $output = preg_replace($pattern, $replace, $file); |
| 9 | + file_put_contents($path, $output); |
| 10 | +} |
| 11 | + |
| 12 | +// Main. |
| 13 | +chdir(__DIR__ . '/..'); |
| 14 | + |
| 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; |
| 18 | + |
| 19 | + exit(1); |
| 20 | +} |
| 21 | + |
| 22 | +// 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 | +system('git switch develop'); |
| 33 | +system('git switch -c docs-changelog-' . $version); |
| 34 | +system('git switch docs-changelog-' . $version); |
| 35 | + |
| 36 | +// Copy changelog |
| 37 | +$changelog = "./user_guide_src/source/changelogs/v{$version}.rst"; |
| 38 | +$changelogIndex = './user_guide_src/source/changelogs/index.rst'; |
| 39 | +if ($isMinorUpdate) { |
| 40 | + copy('./admin/next-changelog-minor.rst', $changelog); |
| 41 | +} else { |
| 42 | + copy('./admin/next-changelog-patch.rst', $changelog); |
| 43 | +} |
| 44 | +// Add changelog to index.rst. |
| 45 | +replace_file_content( |
| 46 | + $changelogIndex, |
| 47 | + '/\.\. toctree::\n :titlesonly:\n/u', |
| 48 | + ".. toctree::\n :titlesonly:\n\n v{$version}" |
| 49 | +); |
| 50 | +// Replace {version} |
| 51 | +$length = mb_strlen("Version {$version}"); |
| 52 | +$underline = str_repeat('#', $length); |
| 53 | +replace_file_content( |
| 54 | + $changelog, |
| 55 | + '/#################\nVersion {version}\n#################/u', |
| 56 | + "{$underline}\nVersion {$version}\n{$underline}" |
| 57 | +); |
| 58 | +replace_file_content( |
| 59 | + $changelog, |
| 60 | + '/{version}/u', |
| 61 | + "{$version}" |
| 62 | +); |
| 63 | + |
| 64 | +// Copy upgrading |
| 65 | +$versionWithoutDots = str_replace('.', '', $version); |
| 66 | +$upgrading = "./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst"; |
| 67 | +$upgradingIndex = './user_guide_src/source/installation/upgrading.rst'; |
| 68 | +copy('./admin/next-upgrading-guide.rst', $upgrading); |
| 69 | +// Add upgrading to upgrading.rst. |
| 70 | +replace_file_content( |
| 71 | + $upgradingIndex, |
| 72 | + '/ backward_compatibility_notes\n/u', |
| 73 | + " backward_compatibility_notes\n\n upgrade_{$versionWithoutDots}" |
| 74 | +); |
| 75 | +// Replace {version} |
| 76 | +$length = mb_strlen("Upgrading from {$versionCurrent} to {$version}"); |
| 77 | +$underline = str_repeat('#', $length); |
| 78 | +replace_file_content( |
| 79 | + $upgrading, |
| 80 | + '/##############################\nUpgrading from {version} to {version}\n##############################/u', |
| 81 | + "{$underline}\nUpgrading from {$versionCurrent} to {$version}\n{$underline}" |
| 82 | +); |
| 83 | + |
| 84 | +// Commits |
| 85 | +system("git add {$changelog} {$changelogIndex}"); |
| 86 | +system("git add {$upgrading} {$upgradingIndex}"); |
| 87 | +system('git commit -m "docs: add changelog and upgrade for v' . $version . '"'); |
0 commit comments