From bdc1a6e6a5d1635a91b18cdd4d4337a97277cc1e Mon Sep 17 00:00:00 2001 From: Lucas Bickel Date: Fri, 19 Jan 2018 20:56:44 +0100 Subject: [PATCH] Validate contents of VERSION file --- .../views/helpers/VersionNotify.php | 52 +++++++++++++++---- build.sh | 9 ++-- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/airtime_mvc/application/views/helpers/VersionNotify.php b/airtime_mvc/application/views/helpers/VersionNotify.php index baf8826ad..dd6f6424b 100644 --- a/airtime_mvc/application/views/helpers/VersionNotify.php +++ b/airtime_mvc/application/views/helpers/VersionNotify.php @@ -2,6 +2,7 @@ use Composer\Semver\Comparator; use Composer\Semver\Semver; +use Composer\Semver\VersionParser; /** * This file does the following things: @@ -13,9 +14,25 @@ use Composer\Semver\Semver; * 4. Returns the current version, as HTML (stored in pair of invisible div tags) */ class Airtime_View_Helper_VersionNotify extends Zend_View_Helper_Abstract { - - public function versionNotify() - { + + /** + * @var VersionParser + */ + private $versionParser; + + public function __construct() { + $this->versionParser = new VersionParser(); + } + + /** + * Prepare data for update notifier widget + * + * Grabs the version from the VERSION file created by build.sh and compares + * it against the versions available from github using the semver code from + * the composer project. + */ + public function versionNotify() { + $config = Config::getConfig(); // retrieve and validate current and latest versions, @@ -24,15 +41,9 @@ class Airtime_View_Helper_VersionNotify extends Zend_View_Helper_Abstract { $link = Application_Model_Preference::GetLatestLink(); $isGitRelease = preg_match('/^[[:alnum:]]{7,}$/i', $current) === 1; - $currentParts = array(); - if (!$isGitRelease) { - $currentParts = preg_split("/(\.|-)/", $current); - } - if (count($currentParts) < 3) { - $currentParts = array(0, 0, 0, 0); - } - + $currentParts = $this->normalizeVersion($current, $isGitRelease); $isPreRelease = $isGitRelease || array_key_exists(4, $currentParts); + // we are always interested in a major when we pre-release, hence the isPreRelease part $majorCandidates = SemVer::satisfiedBy($latest, sprintf('>=%1$s-stable', $currentParts[0] + ($isPreRelease ? 0 : 1))); $minorCandidates = SemVer::satisfiedBy($latest, sprintf('~%1$s.%2$s', $currentParts[0], $currentParts[1] + 1)); @@ -78,4 +89,23 @@ class Airtime_View_Helper_VersionNotify extends Zend_View_Helper_Abstract { . "
"; return $result; } + + private function normalizeVersion($version, $isGit) { + try { + $this->versionParser->normalize($version); + } catch(UnexpectedValueException $e) { + // recover by assuming an unknown version + $version= '0.0.0'; + } + + $parts = array(); + if (!$isGit) { + $parts = preg_split("/(\.|-)/", $version); + } + if (count($parts) < 3) { + $parts = array(0, 0, 0); + } + + return $parts; + } } diff --git a/build.sh b/build.sh index 87ac53767..d7d389c14 100755 --- a/build.sh +++ b/build.sh @@ -17,11 +17,12 @@ if [ "${git_build}" = "y" ]; then echo ${git_version} > VERSION else # if no file was in tarball we create one letting the user know - # travis should release tarballs with a pre-written VERSION file - # at some stage + # if you run in to this you should grab an enriched tarball built + # by travis. It already contains the VERSION file and also bundles + # all the PHP you vendors files making the install much faster on + # your part. if [ ! -f VERSION ]; then - folder_name=$(basename `pwd`) - echo "tarball install from folder ${folder_name}" > VERSION + echo "could not detect version for VERSION file" > VERSION fi fi