/** * Sort a set of SemVer versions * * This method is a catch-all and accepts: * * - A single array of strings or Versions * - A variable number of strings and Versions * * Strings will be parsed into Version instances * * The result will be a sorted array of Versions, in ascending order * * @param array|string|Version $v1 * @param string|Version $v1 * ... * @param string|Version $vn * @return array[int]SemVer **/ public static function sort() { $arguments = func_get_args(); if (count($arguments) === 0) { return array(); } // Versions array $versions = array(); // If the first argument is an array, use that if (is_array($arguments[0])) { $versions = $arguments[0]; } else { $versions = $arguments; } // Parse into Version isntances foreach ($versions as $key => $version) { if ($version instanceof Version) { $versions[$key] = $version; } else { if (is_string($version)) { $versions[$key] = Parser::parse($version); } else { throw new InvalidArgumentException('Invalid version given, pass either Version instances or strings'); } } } // Use the array sorter return self::sortArray($versions); }
/** * Test sort of strings * * @return void **/ public function testStoreOriginalVersion() { $version = Parser::parse('0.0.0'); $version->setMinor(1); $this->assertEquals('0.0.0', $version->getOriginalVersion()); $this->assertEquals('0.1.0', (string) $version); }
protected function versionStringToVersionArray($string) { $semVerParser = SemVerParser::parse($string); if ($semVerParser->hasBuild() || $semVerParser->hasPreRelease()) { throw new RuntimeException('Only numeric semantic version numbers in the format "0.0.0" are supported.'); } return ['major' => $semVerParser->getMajor(), 'minor' => $semVerParser->getMinor(), 'patch' => $semVerParser->getPatch()]; }
private function parseVersion() { $ret = array(0, 0, 1, '', ''); $json = json_decode($this->_io->load(), true); if (!is_array($json)) { throw new HandlerException("Couldn't parse version from io"); } if (!isset($json['version'])) { return $ret; } $vString = $json['version']; $vParsed = Parser::parse($vString); return array($vParsed->getMajor(), $vParsed->getMinor(), $vParsed->getPatch(), $vParsed->hasPreRelease() ? $vParsed->getPreRelease() : '', $vParsed->hasBuild() ? $vParsed->getPreBuild() : ''); }
protected function execute(InputInterface $input, OutputInterface $output) { $file = $input->getArgument('file'); $config = json_decode(file_get_contents($file), true); $type = $input->getOption('type'); if (!in_array($type, ['major', 'minor', 'patch', 'build'])) { return $this->error($output, 'Type can only be one of major, minor, patch or build'); } if (json_last_error() == JSON_ERROR_NONE) { $key = $input->getOption('key'); if (isset($config[$key])) { $version = Parser::parse($config[$key]); switch ($type) { case 'major': $version->setMajor($version->getMajor() + 1); $version->setMinor(0); $version->setPatch(0); break; case 'minor': $version->setMinor($version->getMinor() + 1); $version->setPatch(0); break; case 'patch': $version->setPatch($version->getPatch() + 1); break; case 'build': if ($version->hasBuild()) { $version->getBuild()->setNumber($version->getBuild()->getNumber() + 1); } else { $version->setBuild(new \Naneau\SemVer\Version\Build()); $version->getBuild()->setNumber(1); } break; } $config[$key] = $version->__toString(); file_put_contents($file, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); } } else { return $this->error($output, 'Could not decode JSON file'); } $output->writeln('New version: ' . $config[$key]); return 0; }
/** * Test sort of array * * @return void **/ public function testSortArray() { $v1 = Parser::parse('2.0.2'); $v2 = Parser::parse('2.0.2'); $v3 = Parser::parse('0.0.1'); // Smallest $v4 = Parser::parse('10.0.1-rc.1+build.12345'); $v5 = Parser::parse('10.0.2-rc.1+build.12345'); // Biggest $v6 = Parser::parse('0.0.4'); $sorted = Sorter::sortArray(array($v1, $v2, $v3, $v4, $v5, $v6)); $this->assertCount(6, $sorted); $this->assertEquals($sorted[0], $v3); $this->assertEquals($sorted[1], $v6); $this->assertEquals($sorted[2], $v1); $this->assertEquals($sorted[3], $v1); $this->assertEquals($sorted[4], $v4); $this->assertEquals($sorted[5], $v5); }
protected function execute(InputInterface $input, OutputInterface $output) { $file = $input->getArgument('file'); $config = json_decode(file_get_contents($file), true); if (json_last_error() == JSON_ERROR_NONE) { $key = $input->getOption('key'); if (isset($config[$key])) { $version = Parser::parse($config[$key]); if ($version->hasBuild() && $input->getOption('no-build')) { $build = $version->getBuild()->__toString(); $version = str_replace('+' . $build, '', $version->__toString()); } else { $version = $version->__toString(); } $output->writeln($version); } } else { $output->write('<error>Could not decode JSON file</error>', true); } }
/** * Checks the remote version file against the local version file. * * @return string */ public function check() { //Parse local version file $versionFile = base_path('version.json'); if (!file_exists($versionFile)) { return view('self-updater::error')->with('error', 'No version file found.'); } $localVersionFile = file_get_contents(base_path('version.json')); $localVersionJson = json_decode($localVersionFile, true); //Parse remote version file $remoteVersionFile = @file_get_contents(config('self-updater.remote_uri') . '/remote_version.json'); if ($remoteVersionFile === false) { return view('self-updater::error')->with('error', 'Can not read remote version file.'); } $remoteVersionJson = json_decode($remoteVersionFile, true); $remoteVersion = $remoteVersionJson['version']; $localVersion = $localVersionJson['version']; $newVersion = Compare::greaterThan(Parser::parse($remoteVersion), Parser::parse($localVersion)); return view('self-updater::checker', compact('localVersion', 'remoteVersion', 'newVersion')); }
public function phpSemanticVersion() { return SemVerParser::parse($this->normalizeSemVerSuffix($this->stripCustomBuilds($this->version))); }
private function greaterOrEqual($givenAppVersion, $pluginMinAppVersion) { try { $givenAppVersionSem = Parser::parse($givenAppVersion); $pluginMinAppVersionSem = Parser::parse($pluginMinAppVersion); return Compare::greaterThan($givenAppVersionSem, $pluginMinAppVersionSem) || Compare::equals($givenAppVersionSem, $pluginMinAppVersionSem); } catch (\Exception $e) { return false; } }
public function getNextVersion($type) { if (!in_array($type, [static::VERSION_MAJOR, static::VERSION_MINOR, static::VERSION_PATCH])) { return $type; } $latestRelease = $this->getLatestRelease(); $version = $latestRelease->getName() === 'unreleased' ? '0.0.0' : $latestRelease->getName(); $semver = Parser::parse($version); $patch = $semver->getPatch(); $minor = $semver->getMinor(); $major = $semver->getMajor(); switch ($type) { case Log::VERSION_PATCH: $patch++; break; case Log::VERSION_MINOR: $minor++; break; case Log::VERSION_MAJOR: $major++; break; } return "{$major}.{$minor}.{$patch}"; }
/** * Assert that of two version strings the first is bigger than the other * * @return void **/ private function assertVersionBiggerThan($v1String, $v2String) { // Parse them $v1 = Parser::parse($v1String); $v2 = Parser::parse($v2String); // Versions should not be equal $this->assertFalse(Compare::equals($v1, $v2), 'Version "' . $v1 . '" should *not* be equal to "' . $v2 . '"'); // Greater than and not greater than $this->assertTrue(Compare::greaterThan($v1, $v2), 'Version "' . $v1 . '" should be greater than "' . $v2 . '"'); $this->assertFalse(Compare::greaterThan($v2, $v1), 'Version "' . $v2 . '" should be greater than "' . $v1 . '"'); // Smaller than and not smaller than $this->assertTrue(Compare::smallerThan($v2, $v1), 'Version "' . $v2 . '" should be smaller than "' . $v1 . '"'); $this->assertFalse(Compare::smallerThan($v1, $v2), 'Version "' . $v1 . '" should *not* be smaller than "' . $v2 . '"'); }
public function testDiscardsOriginalString() { $version = Parser::parse('1.0.0'); $this->assertEquals('', $version->cleanCopy()->getOriginalVersion()); }
/** * Test invalid version * * @expectedException InvalidArgumentException * @return void **/ public function testInvalid5() { $v1 = Parser::parse('0.0.0-build.1+!@#'); }
/** * Get the next logical version relative to the provided base version. If * no base is supplied, base will be the same as the current version. * * @param Version|string|null $base * @return Version * @throws InvalidArgumentException */ public function next($base = null) { // Ensure that $base is a Version. Parse it if we must, use ourself if // it is empty. if (empty($base)) { $base = $this; } else { if (is_string($base)) { $base = Parser::parse($base); } elseif (!$base instanceof Version) { throw new InvalidArgumentException("\$base must be of type Version"); } } // If the base is ahead of this Version then the next version will be // the base. if (Compare::greaterThan($base, $this)) { return $base->cleanCopy(); } $next = new Version(); $next->setMajor($this->getMajor()); $next->setMinor($this->getMinor()); $next->setPatch($this->getPatch()); if ($base->hasPreRelease()) { if ($this->hasPreRelease()) { // We already know that $base is less than or equal to $this // and we won't be jumping to the next greek value. So it is // safe use $this prerelease and just increment the release // number. $pre = new PreRelease(); $pre->setGreek($this->getPreRelease()->getGreek()); $pre->setReleaseNumber($this->getPreRelease()->getReleaseNumber() + 1); $next->setPreRelease($pre); } else { throw new InvalidArgumentException("This version has left prerelease without updating the base. Base should not be prerelease."); } } elseif (!$this->hasPreRelease()) { $next->setPatch($this->getPatch() + 1); } // The case of $this having a pre-release when $base does not means // that we are essentially just leaving pre-release. Nothing needs to // be done. return $next; }
/** * @dataProvider versionStringProvider */ public function test($expected, $base_str, $current_str) { $base = Parser::parse($base_str); $current = Parser::parse($current_str); $this->assertEquals($expected, (string) $current->next($base)); }