Skip to content
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
66 changes: 14 additions & 52 deletions bin/bump
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) {
Copy link
Author

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 :(.

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());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method, formatTag, was renamed to getTag.

passthru('git push origin ' . $next_tag->formatTag());
echo $bumped_version . ' has been tagged.' . PHP_EOL;
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@
}
],
"require": {},
"require-dev": {
"phpunit/phpunit": "4.8.*"
},
"autoload": {
"psr-4": { "Bump\\": "src/" }
},
"bin": ["bin/bump"]
}
18 changes: 18 additions & 0 deletions phpunit.xml.dist
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>
39 changes: 39 additions & 0 deletions src/Compare.php
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;
}
}
41 changes: 41 additions & 0 deletions src/Version.php
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();
}
156 changes: 156 additions & 0 deletions src/Version/SemVer.php
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)) {
Copy link
Author

Choose a reason for hiding this comment

The 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 = [
Copy link
Author

Choose a reason for hiding this comment

The 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()
Copy link
Author

Choose a reason for hiding this comment

The 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()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move these public functions before bump, that way importance of the protected methods isn't lost.

{
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;
}
}
Loading