-
Notifications
You must be signed in to change notification settings - Fork 2
✨ Abstract Out Version Logic Into Library #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,31 @@ | ||
| #! /usr/bin/php | ||
| <?php | ||
|
|
||
| $major = 0; | ||
| $minor = 0; | ||
| $patch = 0; | ||
|
|
||
| if (empty($argv[1]) || !in_array($argv[1], ['major', 'minor', 'patch'])) { | ||
| echo 'Please supply a bump type: major, minor, patch.' . PHP_EOL; | ||
| exit; | ||
| echo 'Please supply a bump type: major, minor, patch.' . PHP_EOL; | ||
| exit; | ||
| } | ||
|
|
||
| $bump = $argv[1]; | ||
|
|
||
| $tags = explode(PHP_EOL, shell_exec('git tag')); | ||
| foreach ($tags as $tag) { | ||
| $parts = explode('.', $tag); | ||
| if (count($parts) !== 3) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($parts[0] > $major) { | ||
| $major = $parts[0]; | ||
| $minor = $parts[1]; | ||
| $patch = $parts[2]; | ||
| } elseif ($parts[0] == $major) { | ||
| if ($parts[1] > $minor) { | ||
| $minor = $parts[1]; | ||
| $patch = $parts[2]; | ||
| } elseif ($parts[1] == $minor && $parts[2] > $patch) { | ||
| $patch = $parts[2]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $current_version = implode('.', [$major, $minor, $patch]); | ||
| $comparison = new \Bump\Compare(function ($tag) { | ||
| return new \Bump\Version\SemVer($tag); | ||
| }); | ||
|
|
||
| switch ($bump) { | ||
| case 'major': | ||
| $major++; | ||
| $minor = 0; | ||
| $patch = 0; | ||
| break; | ||
|
|
||
| case 'minor': | ||
| $minor++; | ||
| $patch = 0; | ||
| break; | ||
|
|
||
| case 'patch': | ||
| $patch++; | ||
| break; | ||
| } | ||
| $current_tag = $comparison->getLatestTag(explode(PHP_EOL, shell_exec('git tag'))); | ||
| $next_tag = $current_tag->bump($bump); | ||
|
|
||
| $bumped_version = implode('.', [$major, $minor, $patch]); | ||
|
|
||
| $cmd = 'git shortlog ' . $current_version . '..'; | ||
| echo PHP_EOL . 'git shortlog ' . $current_version . '..' . PHP_EOL; | ||
| $cmd = 'git shortlog ' . $current_tag->formatTag() . '..'; | ||
| echo PHP_EOL . 'git shortlog ' . $current_tag->formatTag() . '..' . PHP_EOL; | ||
| passthru($cmd); | ||
|
|
||
| $tagit = readline('Do you want to create and push a new tag (' . $bumped_version . ') with the above changes? (y/n):'); | ||
| $tagit = readline('Do you want to create and push a new tag (' . $next_tag->formatTag() . ') with the above changes? (y/n):'); | ||
| if (strtolower($tagit) !== 'y') { | ||
| echo 'It\'s cool. I understand.' . PHP_EOL; | ||
| exit; | ||
| echo 'It\'s cool. I understand.' . PHP_EOL; | ||
| exit; | ||
| } | ||
|
|
||
| passthru('git tag ' . $bumped_version); | ||
| passthru('git push origin ' . $bumped_version); | ||
| passthru('git tag ' . $next_tag->formatTag()); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method, |
||
| passthru('git push origin ' . $next_tag->formatTag()); | ||
| echo $bumped_version . ' has been tagged.' . PHP_EOL; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <phpunit backupGlobals="false" | ||
| backupStaticAttributes="false" | ||
| bootstrap="vendor/autoload.php" | ||
| colors="true" | ||
| convertErrorsToExceptions="true" | ||
| convertNoticesToExceptions="true" | ||
| convertWarningsToExceptions="true" | ||
| processIsolation="false" | ||
| stopOnFailure="false" | ||
| syntaxCheck="false" | ||
| > | ||
| <testsuites> | ||
| <testsuite name="Package Test Suite"> | ||
| <directory suffix=".php">test/unit</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| </phpunit> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| namespace Bump; | ||
|
|
||
| class Compare | ||
| { | ||
| /** @var callable */ | ||
| private $tag_factory; | ||
|
|
||
| public function __construct(callable $tag_factory) | ||
| { | ||
| $this->tag_factory = $tag_factory; | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $repository_tags The list of tags from your source code repository. | ||
| * @param string $starting_tag The earliest possible tag for your tagging system, 0.0.0 for SemVer. | ||
| * | ||
| * @return Version The current tag. | ||
| */ | ||
| public function getLatestTag(array $repository_tags, $starting_tag = '0.0.0') | ||
| { | ||
| $current = call_user_func($this->tag_factory, $starting_tag); | ||
| foreach ($repository_tags as $repository_tag) { | ||
| /** @var Version $tag */ | ||
| $tag = call_user_func($this->tag_factory, $repository_tag); | ||
|
|
||
| if ($tag->exclude()) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($current->compare($tag) === Version::LESSER) { | ||
| $current = $tag; | ||
| } | ||
| } | ||
|
|
||
| return $current; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| namespace Bump; | ||
|
|
||
| interface Version | ||
| { | ||
| const EQUAL = 0; | ||
| const LESSER = -1; | ||
| const GREATER = 1; | ||
|
|
||
| /** | ||
| * Determine if the tag should be included in the set of computed tags. | ||
| * | ||
| * Check's to make sure that the tag conforms to whatever tagging format you we specify. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function exclude(); | ||
|
|
||
| /** | ||
| * @param string $touple The portion of the tag to bump, for sem ver should be major, minor, or patch | ||
| * | ||
| * @return Self A new instance of Version with the correctly bumped touple | ||
| */ | ||
| public function bump($touple); | ||
|
|
||
| /** | ||
| * Compares the version against another version. | ||
| * | ||
| * @param Version $version | ||
| * | ||
| * @return int -1 When the instance version is lower than the parameter version, 0 when they're equal, and 1 | ||
| * when the instance version is greater than the parameter version. | ||
| */ | ||
| public function compare(Version $version); | ||
|
|
||
| /** | ||
| * @return string THe human readable version of the tag. | ||
| */ | ||
| public function getTag(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?php | ||
|
|
||
| namespace Bump\Version; | ||
|
|
||
| use Bump\Version; | ||
|
|
||
| class SemVer implements Version | ||
| { | ||
| /** @var string The major version */ | ||
| private $major; | ||
|
|
||
| /** @var string The minor version */ | ||
| private $minor; | ||
|
|
||
| /** @var string The patch version */ | ||
| private $patch; | ||
|
|
||
| /** @var bool Determine if the tag is in a valid format. */ | ||
| private $is_valid_format = false; | ||
|
|
||
| public function __construct($tag) | ||
| { | ||
| if ($this->init($tag)) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be able to remove the init method entirely. |
||
| $this->is_valid_format = true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function exclude() | ||
| { | ||
| return !$this->is_valid_format; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function bump($touple) | ||
| { | ||
| $touple_map = [ | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. method_map makes more sense here. |
||
| 'patch' => 'bumpPatch', | ||
| 'minor' => 'bumpMinor', | ||
| 'major' => 'bumpMajor' | ||
| ]; | ||
|
|
||
| if (!isset($touple_map[$touple])) { | ||
| throw new \InvalidArgumentException($touple . ' is not an allowed touple'); | ||
| } | ||
|
|
||
| return $this->{$touple_map[$touple]}(); | ||
| } | ||
|
|
||
| /** | ||
| * Generates the patch bump for the given tag. | ||
| * | ||
| * @return self | ||
| */ | ||
| protected function bumpPatch() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move protected methods below other public functions maybe? |
||
| { | ||
| $version = clone $this; | ||
| $version->patch++; | ||
| return $version; | ||
| } | ||
|
|
||
| /** | ||
| * Generates the minor bump for the given tag. | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function bumpMinor() | ||
| { | ||
| $version = clone $this; | ||
| $version->minor++; | ||
| $version->patch = 0; | ||
| return $version; | ||
| } | ||
|
|
||
| /** | ||
| * Generates the major bump for the given tag. | ||
| * | ||
| * @return string | ||
| */ | ||
| protected function bumpMajor() | ||
| { | ||
| $version = clone $this; | ||
| $version->major++; | ||
| $version->patch = 0; | ||
| $version->minor = 0; | ||
| return $version; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function getTag() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe move these public functions before |
||
| { | ||
| return sprintf('%d.%d.%d', $this->major, $this->minor, $this->patch); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function compare(Version $version) | ||
| { | ||
| $major = $this->spaceShip($this->major, $version->major); | ||
| if ($major !== VERSION::EQUAL) { | ||
| return $major; | ||
| } | ||
|
|
||
| $minor = $this->spaceShip($this->minor, $version->minor); | ||
| if ($minor !== Version::EQUAL) { | ||
| return $minor; | ||
| } | ||
|
|
||
| return $this->spaceShip($this->patch, $version->patch); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the major, minor, and patch properties. | ||
| * | ||
| * @param string $tag | ||
| * | ||
| * @return bool True if the raw_tag is formatted correctly, false if the format is incorrect. | ||
| */ | ||
| protected function init($tag) | ||
| { | ||
| $parts = explode('.', $tag); | ||
|
|
||
| if (count($parts) !== 3) { | ||
| return false; | ||
| } | ||
|
|
||
| list($this->major, $this->minor, $this->patch) = $parts; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Performs the same action as `<=>` operation in php 7. | ||
| * | ||
| * @param string|int $instance | ||
| * @param string|int $parameter | ||
| * | ||
| * @return int -1 when instance is less than parameter, 0 if they're equal and 1 if instance is greater than | ||
| * parameter. | ||
| */ | ||
| private function spaceShip($instance, $parameter) | ||
| { | ||
| if ($instance == $parameter) { | ||
| return 0; | ||
| } | ||
|
|
||
| return $instance > $parameter ? 1 : -1; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice if there was some way to set this up(service provider) out side of bump, oh well though :(.