Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bumpversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
$rc = Helper::getOption($options, 'r', 'rc');
$date = Helper::getOption($options, 'd', 'date');
$isdevbranch = (bool) Helper::getOption($options, 'i', 'isdevbranch');
$path = rtrim($path, '/') . '/version.php';

// Find the version.php.
$path = Helper::getVersionPath($path);

$release = Helper::bumpVersion($path, $branch, $type, $rc, $date, $isdevbranch);
$result = 0;
Expand Down
4 changes: 3 additions & 1 deletion get_next_version_number.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
$rc = Helper::getOption($options, 'r', 'rc');
$date = Helper::getOption($options, 'd', 'date');
$isdevbranch = (bool) Helper::getOption($options, 'i', 'isdevbranch');
$path = rtrim($path, '/') . '/version.php';

// Find the version.php.
$path = Helper::getVersionPath($path);

$currentVersion = VersionInfo::fromVersionFile($path);
$nextVersion = $currentVersion->getNextVersion(
Expand Down
7 changes: 6 additions & 1 deletion prerelease.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ bump_version() {
output " - ${R}Failed to bump version file [$outcome].${N}"
_pushup=false
else
git add version.php
versionpath=version.php
if [ ! -f $versionpath ]; then
versionpath=public/version.php
fi

git add $versionpath
if git_staged_changes ; then
if $weekly ; then
git commit --quiet -m "weekly release $release"
Expand Down
7 changes: 6 additions & 1 deletion release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ skippedbranches=()
output
output "${normal}You are about to push:"
for b in "${allbranches[@]}" ; do
versionpath=version.php
if [ ! -f $versionpath ]; then
versionpath=public/version.php
fi

# Search for a 'real' release by ensuring the top commit on version.php changes $release and was recent.
releasebumped=$(git show --since='8 hours ago' -n1 origin/${b} version.php | grep "+\$release\s*=\s*")
releasebumped=$(git show --since='8 hours ago' -n1 origin/${b} $versionpath | grep "+\$release\s*=\s*")
if [[ -n ${releasebumped} || $_skip_version_check ]]; then
output "${G}$b: ${normal}$(git log -n1 --pretty=format:"%s (%an %ar)" origin/$b)"
# Check if between the last commit in the branch and now, it has been PUBLISHING_TIME (UTC)
Expand Down
26 changes: 25 additions & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Helper
/**
* Bump the version.
*
* @param string $path The path to the versio file
* @param string $path The path to the version file
* @param string $branch The branch name to set
* @param string $type The type of release
* @param string $rc If a release candidate, the RC number
Expand Down Expand Up @@ -60,6 +60,30 @@ public static function bumpVersion(
return $newVersionInfo->release;
}

/**
* Get the path to the version file.
*
* @param string $path Path to the Moodle root
* @return string Path to version.php
* @throws \ValueError if no version.php could be found
*/
public static function getVersionPath(
string $path,
): string {
$possibles = [
rtrim($path, '/') . '/public/version.php',
rtrim($path, '/') . '/version.php',
];

foreach ($possibles as $possible) {
if (file_exists($possible)) {
return $possible;
}
}

throw new \ValueError("Unable to find a version.php in {$path}");
}

/**
* Determine the next branch number based on the current one.
*
Expand Down
70 changes: 70 additions & 0 deletions tests/unit/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,74 @@ public static function invalidOptionProvider(): array
],
];
}

#[DataProvider('getVersionPathProvider')]
public function testGetVersionPath(
array $structure,
string $path,
?string $expected,
): void {
$root = vfsStream::setup('root', structure: $structure);

if ($expected === null) {
$this->expectException(\ValueError::class);
$this->expectExceptionMessage("Unable to find a version.php in {$path}");
Helper::getVersionPath($path);
} else {
$this->assertSame($expected, Helper::getVersionPath($path));
}
}

public static function getVersionPathProvider(): \Generator
{
yield 'Valid path' => [
'structure' => [
'version.php' => '<?php // Version file content.',
],
'path' => vfsStream::url('root'),
'expected' => vfsStream::url('root/version.php'),
];

yield 'Valid path with trailing slash' => [
'structure' => [
'version.php' => '<?php // Version file content.',
],
'path' => vfsStream::url('root/'),
'expected' => vfsStream::url('root/version.php'),
];

yield 'Valid path in public' => [
'structure' => [
'public' => [
'version.php' => '<?php // Version file content.',
],
],
'path' => vfsStream::url('root'),
'expected' => vfsStream::url('root/public/version.php'),
];

yield 'Valid path in public with trailing slash' => [
'structure' => [
'public' => [
'version.php' => '<?php // Version file content.',
],
],
'path' => vfsStream::url('root/'),
'expected' => vfsStream::url('root/public/version.php'),
];

yield 'Invalid path' => [
'structure' => [],
'path' => vfsStream::url('root'),
'expected' => null,
];

yield 'Invalid path in public' => [
'structure' => [
'public' => [],
],
'path' => vfsStream::url('root'),
'expected' => null,
];
}
}