Skip to content

Commit

Permalink
Add semver color output representation
Browse files Browse the repository at this point in the history
  • Loading branch information
noniagriconomie committed Sep 6, 2021
1 parent c3bc160 commit baeec42
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
26 changes: 24 additions & 2 deletions src/OperationHandler/UpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ public function getOutput(OperationInterface $operation, UrlGenerator $urlGenera
}

$output[] = sprintf(
' - <fg=green>%s</fg=green> %s from <fg=yellow>%s</fg=yellow> to <fg=yellow>%s</fg=yellow>',
' - <fg=green>%s</fg=green> %s from <fg=yellow>%s</fg=yellow> to <fg=yellow>%s</fg=yellow>%s',
$initialPackage->getName(),
$action,
$versionFrom->getCliOutput(),
$versionTo->getCliOutput()
$versionTo->getCliOutput(),
$this->getSemverOutput($versionFrom->getName(), $versionTo->getName())
);

if ($urlGenerator) {
Expand Down Expand Up @@ -113,4 +114,25 @@ public function getOutput(OperationInterface $operation, UrlGenerator $urlGenera

return $output;
}

/** @return string */
private function getSemverOutput(string $versionFrom, string $versionTo)
{
if (false === strpos($versionFrom, '.') && false === strpos($versionTo, '.')) {
return '';
}

$versionsFrom = \explode('.', $versionFrom);
$versionsTo = \explode('.', $versionTo);

if (version_compare($versionsFrom[0], $versionsTo[0], '!=')) {
return ' <bg=red>major</>';
}

if (version_compare($versionsFrom[0], $versionsTo[0], '==') && version_compare($versionsFrom[1], $versionsTo[1], '!=')) {
return ' <bg=yellow>minor</>';
}

return ' <bg=green>patch</>';
}
}
4 changes: 2 additions & 2 deletions tests/ChangelogsPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testItReceivesEvent()
$expectedOutput = <<<OUTPUT
Changelogs summary:
- foo/bar updated from v1.0.0 to v1.0.1
- foo/bar updated from v1.0.0 to v1.0.1 patch
See changes: https://github.com/foo/bar/compare/v1.0.0...v1.0.1
Release notes: https://github.com/foo/bar/releases/tag/v1.0.1
Expand All @@ -141,7 +141,7 @@ public function testEventsAreHandled()
$expectedOutput = <<<OUTPUT
Changelogs summary:
- foo/bar updated from v1.0.0 to v1.0.1
- foo/bar updated from v1.0.0 to v1.0.1 patch
See changes: https://github.com/foo/bar/compare/v1.0.0...v1.0.1
Release notes: https://github.com/foo/bar/releases/tag/v1.0.1
Expand Down
14 changes: 7 additions & 7 deletions tests/OperationHandler/UpdateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public function testItGetsOutputWithoutUrlGenerator()
$package1 = new Package('acme/my-project1', 'v1.0.0.0', 'v1.0.0');
$package1->setSourceUrl('https://example.com/acme/my-project1.git');

$package2 = new Package('acme/my-project2', 'v1.0.1.0', 'v1.0.1');
$package2 = new Package('acme/my-project2', 'v1.1.1.0', 'v1.1.1');
$package2->setSourceUrl('https://example.com/acme/my-project2.git');

$operation = new UpdateOperation($package1, $package2);

$expectedOutput = [
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow>',
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.1.1</fg=yellow> <bg=yellow>minor</>',
];

$this->assertSame(
Expand All @@ -102,7 +102,7 @@ public function testItGetsOutputWithUrlGeneratorNoSupportingCompareUrl()
);

$expectedOutput = [
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow>',
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow> <bg=green>patch</>',
' Release notes: https://example.com/acme/my-project/release/v1.0.1',
];

Expand All @@ -126,7 +126,7 @@ public function testItGetsOutputWithUrlGeneratorNoSupportingReleaseUrl()
);

$expectedOutput = [
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow>',
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow> <bg=green>patch</>',
' See changes: https://example.com/acme/my-project/compare/v1.0.0/v1.0.1',
];

Expand All @@ -150,7 +150,7 @@ public function testItGetsOutputWithUrlGeneratorSupportingAllUrls()
);

$expectedOutput = [
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow>',
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow> <bg=green>patch</>',
' See changes: https://example.com/acme/my-project/compare/v1.0.0/v1.0.1',
' Release notes: https://example.com/acme/my-project/release/v1.0.1',
];
Expand All @@ -177,7 +177,7 @@ public function testItUsesCorrectActionName()
$operationUpdate = new UpdateOperation($package1, $package2);

$expectedOutput = [
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow>',
' - <fg=green>acme/my-project1</fg=green> updated from <fg=yellow>v1.0.0</fg=yellow> to <fg=yellow>v1.0.1</fg=yellow> <bg=green>patch</>',
];

$this->assertSame(
Expand All @@ -188,7 +188,7 @@ public function testItUsesCorrectActionName()
$operationDowngrade = new UpdateOperation($package2, $package1);

$expectedOutput = [
' - <fg=green>acme/my-project2</fg=green> downgraded from <fg=yellow>v1.0.1</fg=yellow> to <fg=yellow>v1.0.0</fg=yellow>',
' - <fg=green>acme/my-project2</fg=green> downgraded from <fg=yellow>v1.0.1</fg=yellow> to <fg=yellow>v1.0.0</fg=yellow> <bg=green>patch</>',
];

$this->assertSame(
Expand Down

0 comments on commit baeec42

Please sign in to comment.