Skip to content

Commit 9c95a0f

Browse files
authored
Add gt command (#3)
1 parent f3402d6 commit 9c95a0f

11 files changed

Lines changed: 139 additions & 10 deletions

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ jobs:
6868
max-parallel: 3
6969
matrix:
7070
php: ["7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
71+
stability: [prefer-lowest, prefer-stable]
72+
machine: [windows-latest, ubuntu-latest]
7173

7274
steps:
7375
- name: Checkout code

app/Commander.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Symfony\Component\Console\Application;
88
use Symfony\Component\Console\Command\Command;
9+
use Syntatis\Version\CLI\Commands\GreaterThanCommand;
910
use Syntatis\Version\CLI\Commands\IncrementCommand;
1011
use Syntatis\Version\CLI\Commands\ValidateCommand;
1112

@@ -15,7 +16,7 @@ final class Commander extends Application
1516

1617
public function __construct()
1718
{
18-
parent::__construct('Ver', self::VERSION);
19+
parent::__construct('Version', self::VERSION);
1920

2021
$this->addCommands($this->getCommands());
2122
}
@@ -29,6 +30,7 @@ private function getCommands(): array
2930
return [
3031
new IncrementCommand(),
3132
new ValidateCommand(),
33+
new GreaterThanCommand(),
3234
];
3335
}
3436
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Syntatis\Version\CLI\Commands;
6+
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Console\Style\SymfonyStyle;
12+
use Syntatis\Version\CLI\Exceptions\InvalidArgumentType;
13+
use Throwable;
14+
use Version\Version;
15+
16+
use function is_string;
17+
use function sprintf;
18+
19+
final class GreaterThanCommand extends Command
20+
{
21+
/**
22+
* Configure the command options and arguments.
23+
*/
24+
protected function configure(): void
25+
{
26+
$this->setName('gt');
27+
$this->setDescription('Compare if a version is greater than another');
28+
$this->addArgument('version-a', InputArgument::REQUIRED, 'First version to compare');
29+
$this->addArgument('version-b', InputArgument::REQUIRED, 'Second version to compare against the first');
30+
$this->setHelp(<<<'HELP'
31+
This command compares two versions and checks if the first version is greater than the second.
32+
33+
Usage:
34+
<info>version gt 1.0.0 0.9.0</info>
35+
<info>version gt 2.1.0 2.1.0</info>
36+
HELP);
37+
}
38+
39+
protected function execute(InputInterface $input, OutputInterface $output): int
40+
{
41+
$style = new SymfonyStyle($input, $output);
42+
$versionA = $input->getArgument('version-a');
43+
$versionB = $input->getArgument('version-b');
44+
45+
try {
46+
if (! is_string($versionA)) {
47+
throw new InvalidArgumentType($versionA);
48+
}
49+
50+
if (! is_string($versionB)) {
51+
throw new InvalidArgumentType($versionB);
52+
}
53+
54+
/** @var Version $a */
55+
$a = Version::fromString($versionA);
56+
57+
/** @var Version $b */
58+
$b = Version::fromString($versionB);
59+
60+
if ($a->isGreaterThan($b)) {
61+
$style->success(sprintf("Version '%s' is greater than '%s'.", $a, $b));
62+
63+
return Command::SUCCESS;
64+
}
65+
66+
$style->error(sprintf("Version '%s' is not greater than '%s'.", $a, $b));
67+
68+
return Command::FAILURE;
69+
} catch (Throwable $th) {
70+
$style->error($th->getMessage());
71+
72+
return Command::FAILURE;
73+
}
74+
}
75+
}

app/Commands/IncrementCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ final class IncrementCommand extends Command
2525
protected function configure(): void
2626
{
2727
$this->setName('increment');
28-
$this->setDescription('Increment a version.');
29-
$this->setHelp('This command increments the provided version by the specified part (major, minor, patch).');
30-
$this->setAliases(['incr', 'bump']);
28+
$this->setDescription('Increment a version');
29+
$this->setAliases(['incr']);
3130
$this->addArgument('version', InputArgument::REQUIRED, 'Version to increment');
3231
$this->addOption('part', 'p', InputArgument::OPTIONAL, 'Part to increment (major, minor, patch)', 'patch');
3332
$this->addOption('build', 'b', InputArgument::OPTIONAL, 'Build metadata to append to the version');

app/Commands/ValidateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class ValidateCommand extends Command
2424
protected function configure(): void
2525
{
2626
$this->setName('validate');
27-
$this->setDescription('Validate a version.');
27+
$this->setDescription('Validate a version');
2828
$this->setHelp('This command checks if the provided value is a valid Semantic Version (SemVer) version format.');
2929
$this->setAliases(['val']);
3030
$this->addArgument('version', InputArgument::REQUIRED, 'Version to validate');

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"name": "syntatis/version-cli",
33
"description": "Validate, increment, and compare SemVer-compliant version number with CLI",
4-
"keywords": ["version", "cli", "semver"],
4+
"keywords": [
5+
"version",
6+
"cli",
7+
"semver"
8+
],
59
"authors": [
610
{
711
"name": "Thoriq Firdaus"
@@ -57,5 +61,8 @@
5761
},
5862
"preferred-install": "dist",
5963
"sort-packages": true
60-
}
64+
},
65+
"non-feature-branches": [
66+
"dependabot/*"
67+
]
6168
}

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ parameters:
22
level: 10
33
paths:
44
- app
5-
- tests/app
5+
- tests/phpunit
66
ignoreErrors:
77
-
88
identifier: missingType.iterableValue

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"
1212
>
1313
<testsuites>
14-
<testsuite name="app">
15-
<directory suffix="Test.php">tests/app/</directory>
14+
<testsuite name="phpunit">
15+
<directory suffix="Test.php">tests/phpunit/</directory>
1616
</testsuite>
1717
</testsuites>
1818
</phpunit>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Syntatis\Tests\Commands;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Tester\CommandTester;
9+
use Syntatis\Version\CLI\Commander;
10+
11+
use function sprintf;
12+
13+
class GreaterThanCommandTest extends TestCase
14+
{
15+
private Commander $commander;
16+
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->commander = new Commander();
22+
}
23+
24+
/** @dataProvider dataComparison */
25+
public function testComparison(string $versionA, string $versionB, string $expect): void
26+
{
27+
$tester = new CommandTester($this->commander->get('gt'));
28+
$tester->execute(['version-a' => $versionA, 'version-b' => $versionB]);
29+
30+
self::assertStringContainsString(
31+
$expect,
32+
$tester->getDisplay(),
33+
);
34+
}
35+
36+
public static function dataComparison(): iterable
37+
{
38+
yield ['1.0.0', '2.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '2.0.0')];
39+
yield ['2.0.0', '1.0.0', sprintf("[OK] Version '%s' is greater than '%s'.", '2.0.0', '1.0.0')];
40+
yield ['1.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '1.0.0')];
41+
yield ['1.0.0-alpha.1', '1.0.0-alpha.2', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0-alpha.1', '1.0.0-alpha.2')];
42+
yield ['1.0.0-alpha.2', '1.0.0-alpha.1', sprintf("[OK] Version '%s' is greater than '%s'.", '1.0.0-alpha.2', '1.0.0-alpha.1')];
43+
}
44+
}
File renamed without changes.

0 commit comments

Comments
 (0)