function testVersionFunctions() { $t = array('1.0.0-alpha' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array('alpha'), 'b' => array()), '1.0.0-alpha.1' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array('alpha', 1), 'b' => array()), '1.0.0-0.3.7' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array(0, 3, 7), 'b' => array()), '1.0.0-x.7.z.92' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array('x', 7, 'z', 92), 'b' => array()), '1.0.0-alpha+001' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array('alpha'), 'b' => array('001')), '1.2.3-alpha.2+02' => array('M' => 1, 'm' => 2, 'p' => 3, 'pr' => array('alpha', 2), 'b' => array('02')), '1.2.3-a.3+02.5.a' => array('M' => 1, 'm' => 2, 'p' => 3, 'pr' => array('a', 3), 'b' => array('02', 5, 'a'))); foreach ($t as $version => $parts) { $v = new SemVer\version($version); $this->assertEquals($v->getMajor(), $parts['M']); $this->assertEquals($v->getMinor(), $parts['m']); $this->assertEquals($v->getPatch(), $parts['p']); $this->assertEquals($v->getPrerelease(), $parts['pr']); $this->assertEquals($v->getBuild(), $parts['b']); } }
protected function execute(InputInterface $input, OutputInterface $output) { $startTime = microtime(true); $targetDirectory = getcwd(); $tag = $this->config->get('tag'); $against = $this->config->get('against') ?: 'HEAD'; $includeBefore = $this->config->get('include-before'); $excludeBefore = $this->config->get('exclude-before'); $includeAfter = $this->config->get('include-after'); $excludeAfter = $this->config->get('exclude-after'); $client = new Client(); $repository = $client->getRepository($targetDirectory); if ($tag === null) { $tag = $this->findLatestTag($repository); } else { $tag = $this->findTag($repository, $tag); } if ($tag === null) { $output->writeln('<error>No tags to suggest against</error>'); return; } $output->writeln('<info>Testing ' . $against . ' against tag: ' . $tag . '</info>'); $finder = new Finder(); $sourceFilter = new SourceFilter(); $beforeScanner = new Scanner(); $afterScanner = new Scanner(); $modifiedFiles = $repository->getModifiedFiles($tag, $against); $modifiedFiles = array_filter($modifiedFiles, function ($modifiedFile) { return substr($modifiedFile, -4) === '.php'; }); $initialBranch = $repository->getCurrentBranch(); if (!$this->config->get('allow-detached') && !$initialBranch) { $output->writeln('<error>You are on a detached HEAD, aborting.</error>'); $output->writeln('<info>If you still wish to run against a detached HEAD, use --allow-detached.</info>'); return -1; } // Start with the against commit $repository->checkout($against . ' --'); $sourceAfter = $finder->findFromString($targetDirectory, $includeAfter, $excludeAfter); $sourceAfterMatchedCount = count($sourceAfter); $sourceAfter = $sourceFilter->filter($sourceAfter, $modifiedFiles); $progress = new ProgressBar($output, count($sourceAfter)); foreach ($sourceAfter as $file) { $afterScanner->scan($file); $progress->advance(); } $progress->clear(); // Finish with the tag commit $repository->checkout($tag . ' --'); $sourceBefore = $finder->findFromString($targetDirectory, $includeBefore, $excludeBefore); $sourceBeforeMatchedCount = count($sourceBefore); $sourceBefore = $sourceFilter->filter($sourceBefore, $modifiedFiles); $progress = new ProgressBar($output, count($sourceBefore)); foreach ($sourceBefore as $file) { $beforeScanner->scan($file); $progress->advance(); } $progress->clear(); // Reset repository to initial branch if ($initialBranch) { $repository->checkout($initialBranch); } $registryBefore = $beforeScanner->getRegistry(); $registryAfter = $afterScanner->getRegistry(); $analyzer = new Analyzer(); $report = $analyzer->analyze($registryBefore, $registryAfter); $tag = new SemanticVersion($tag); $newTag = new SemanticVersion($tag); $suggestedLevel = $report->getSuggestedLevel(); if ($suggestedLevel !== Level::NONE) { if ($newTag->getPrerelease()) { $newTag->inc('prerelease'); } else { if ($newTag->getMajor() < 1 && $suggestedLevel === Level::MAJOR) { $newTag->inc('minor'); } else { $newTag->inc(strtolower(Level::toString($suggestedLevel))); } } } $output->writeln(''); $output->writeln('<info>Initial semantic version: ' . $tag . '</info>'); $output->writeln('<info>Suggested semantic version: ' . $newTag . '</info>'); if ($this->config->get('details')) { $reporter = new Reporter($report); $reporter->output($output); } $duration = microtime(true) - $startTime; $output->writeln(''); $output->writeln('[Scanned files] Before: ' . count($sourceBefore) . ' (' . $sourceBeforeMatchedCount . ' unfiltered), After: ' . count($sourceAfter) . ' (' . $sourceAfterMatchedCount . ' unfiltered)'); $output->writeln('Time: ' . round($duration, 3) . ' seconds, Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB'); }
/** * @param version $currentVersion * @param $releases * * @return bool|array */ private function getHighestPatchReleaseOfNextMinorVersion(version $currentVersion, $releases) { $nextMinorStep = new expression($currentVersion->getMajor() . "." . ($currentVersion->getMinor() + 1) . ".*"); foreach ($releases as $release) { if ($nextMinorStep->satisfiedBy($this->getVersionFromRelease($release))) { return $release; } } return false; }
/** * Checks ifa given string is greater than another * @param string|version $v1 The first version * @param string|version $v2 The second version * @return boolean */ public static function gt($v1, $v2) { if (!$v1 instanceof version) { $v1 = new version($v1); } if (!$v2 instanceof version) { $v2 = new version($v2); } // Major version number $ma1 = $v1->getMajor(); $ma2 = $v2->getMajor(); if ($ma1 < 0 && $ma2 >= 0) { return false; } if ($ma1 >= 0 && $ma2 < 0) { return true; } if ($ma1 > $ma2) { return true; } if ($ma1 < $ma2) { return false; } // Minor version number $mi1 = $v1->getMinor(); $mi2 = $v2->getMinor(); if ($mi1 < 0 && $mi2 >= 0) { return false; } if ($mi1 >= 0 && $mi2 < 0) { return true; } if ($mi1 > $mi2) { return true; } if ($mi1 < $mi2) { return false; } // Patch level $p1 = $v1->getPatch(); $p2 = $v2->getPatch(); if ($p1 < 0 && $p2 >= 0) { return false; } if ($p1 >= 0 && $p2 < 0) { return true; } if ($p1 > $p2) { return true; } if ($p1 < $p2) { return false; } // Build number $b1 = $v1->getBuild(); $b2 = $v2->getBuild(); if ($b1 < 0 && $b2 >= 0) { return false; } if ($b1 >= 0 && $b2 < 0) { return true; } if ($b1 > $b2) { return true; } if ($b1 < $b2) { return false; } // Tag. $t1 = $v1->getTag(); $t2 = $v2->getTag(); if ($t1 === $t2) { return false; } if ($t1 === '' && $t2 !== '') { return true; //v1 has no tag, v2 has tag } if ($t1 !== '' && $t2 === '') { return false; //v1 has tag, v2 has no tag } // both have tags, sort them naturally to see which one is greater. $array = array($t1, $t2); natsort($array); // natsort() preserves array keys. $array[0] may not be the first element. return reset($array) === $t2; }