|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +/* |
| 4 | + * This file is part of the Moodle Plugin CI package. |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + * |
| 9 | + * @copyright 2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com} |
| 10 | + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 11 | + * |
| 12 | + * Verify that the version in the moodle-plugin-ci app matches the |
| 13 | + * most recent version in the docs/CHANGELOG.md file. That way we |
| 14 | + * will enforce both to be always changed together, as part of the |
| 15 | + * documented release process. |
| 16 | + */ |
| 17 | + |
| 18 | +// We need the moodle-plugin-ci binary, error if not found. |
| 19 | +$binary = __DIR__.'/moodle-plugin-ci'; |
| 20 | +if (!is_readable($binary)) { |
| 21 | + fwrite(STDERR, 'Failed to find moodle-plugin-ci binary (usually under the bin directory).'.PHP_EOL); |
| 22 | + exit(1); |
| 23 | +} |
| 24 | + |
| 25 | +// We need the docs/CHANGELOG.md, error if not found. |
| 26 | +$changelog = dirname(__DIR__).'/docs/CHANGELOG.md'; |
| 27 | +if (!is_readable($changelog)) { |
| 28 | + fwrite(STDERR, 'Failed to find CHANGELOG.md (usually under the docs directory).'.PHP_EOL); |
| 29 | + exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +// Extract the version from the binary, error if not found. |
| 33 | +$regexp = '~^define\(\'MOODLE_PLUGIN_CI_VERSION\', *[\'"](\d+\.\d+\.\d+)[\'"].*$~m'; |
| 34 | +if (!preg_match($regexp, file_get_contents($binary), $binaryMatches)) { |
| 35 | + fwrite(STDERR, 'Failed to parse moodle-plugin-ci looking for a version.'.PHP_EOL); |
| 36 | + exit(1); |
| 37 | +} |
| 38 | +$binaryVersion = $binaryMatches[1]; |
| 39 | + |
| 40 | +// Extract the version from the change log, error if not found. |
| 41 | +$regexp = '~^## *\[(\d+\.\d+\.\d+)\] *\-* *\d{4}\-\d{1,2}\-\d{1,2}$~m'; |
| 42 | +if (!preg_match($regexp, file_get_contents($changelog), $changelogMatches)) { |
| 43 | + fwrite(STDERR, 'Failed to parse CHANGELOG.md looking for a version.'.PHP_EOL); |
| 44 | + exit(1); |
| 45 | +} |
| 46 | +$changelogVersion = $changelogMatches[1]; |
| 47 | + |
| 48 | +// Version in change log > binary, error. |
| 49 | +if (version_compare($changelogVersion, $binaryVersion, '>')) { |
| 50 | + fwrite(STDERR, 'Version in docs/CHANGELOG.md ('. |
| 51 | + $changelogVersion.') is newer than version in bin/moodle-plugin-ci ('. |
| 52 | + $binaryVersion.'). Please, check!'.PHP_EOL); |
| 53 | + exit(1); |
| 54 | +} |
| 55 | + |
| 56 | +// Version in change log < binary, error. |
| 57 | +if (version_compare($changelogVersion, $binaryVersion, '<')) { |
| 58 | + fwrite(STDERR, 'Version in docs/CHANGELOG.md ('. |
| 59 | + $changelogVersion.') is older than version in bin/moodle-plugin-ci ('. |
| 60 | + $binaryVersion.'). Please, check!'.PHP_EOL); |
| 61 | + exit(1); |
| 62 | +} |
| 63 | + |
| 64 | +// Arrived here, versions match, all ok. |
| 65 | +fwrite(STDOUT, 'Matching version found: '.$changelogVersion.PHP_EOL); |
| 66 | +exit(0); |
0 commit comments