Skip to content
Merged
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: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ jobs:
max-parallel: 3
matrix:
php: ["7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
stability: [prefer-lowest, prefer-stable]
machine: [windows-latest, ubuntu-latest]

steps:
- name: Checkout code
Expand Down
4 changes: 3 additions & 1 deletion app/Commander.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Syntatis\Version\CLI\Commands\GreaterThanCommand;
use Syntatis\Version\CLI\Commands\IncrementCommand;
use Syntatis\Version\CLI\Commands\ValidateCommand;

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

public function __construct()
{
parent::__construct('Ver', self::VERSION);
parent::__construct('Version', self::VERSION);

$this->addCommands($this->getCommands());
}
Expand All @@ -29,6 +30,7 @@ private function getCommands(): array
return [
new IncrementCommand(),
new ValidateCommand(),
new GreaterThanCommand(),
];
}
}
75 changes: 75 additions & 0 deletions app/Commands/GreaterThanCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Syntatis\Version\CLI\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Syntatis\Version\CLI\Exceptions\InvalidArgumentType;
use Throwable;
use Version\Version;

use function is_string;
use function sprintf;

final class GreaterThanCommand extends Command
{
/**
* Configure the command options and arguments.
*/
protected function configure(): void
{
$this->setName('gt');
$this->setDescription('Compare if a version is greater than another');
$this->addArgument('version-a', InputArgument::REQUIRED, 'First version to compare');
$this->addArgument('version-b', InputArgument::REQUIRED, 'Second version to compare against the first');
$this->setHelp(<<<'HELP'
This command compares two versions and checks if the first version is greater than the second.

Usage:
<info>version gt 1.0.0 0.9.0</info>
<info>version gt 2.1.0 2.1.0</info>
HELP);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle($input, $output);
$versionA = $input->getArgument('version-a');
$versionB = $input->getArgument('version-b');

try {
if (! is_string($versionA)) {
throw new InvalidArgumentType($versionA);
}

if (! is_string($versionB)) {
throw new InvalidArgumentType($versionB);
}

/** @var Version $a */
$a = Version::fromString($versionA);

/** @var Version $b */
$b = Version::fromString($versionB);

if ($a->isGreaterThan($b)) {
$style->success(sprintf("Version '%s' is greater than '%s'.", $a, $b));

return Command::SUCCESS;
}

$style->error(sprintf("Version '%s' is not greater than '%s'.", $a, $b));

return Command::FAILURE;
} catch (Throwable $th) {
$style->error($th->getMessage());

return Command::FAILURE;
}
}
}
5 changes: 2 additions & 3 deletions app/Commands/IncrementCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ final class IncrementCommand extends Command
protected function configure(): void
{
$this->setName('increment');
$this->setDescription('Increment a version.');
$this->setHelp('This command increments the provided version by the specified part (major, minor, patch).');
$this->setAliases(['incr', 'bump']);
$this->setDescription('Increment a version');
$this->setAliases(['incr']);
$this->addArgument('version', InputArgument::REQUIRED, 'Version to increment');
$this->addOption('part', 'p', InputArgument::OPTIONAL, 'Part to increment (major, minor, patch)', 'patch');
$this->addOption('build', 'b', InputArgument::OPTIONAL, 'Build metadata to append to the version');
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ValidateCommand extends Command
protected function configure(): void
{
$this->setName('validate');
$this->setDescription('Validate a version.');
$this->setDescription('Validate a version');
$this->setHelp('This command checks if the provided value is a valid Semantic Version (SemVer) version format.');
$this->setAliases(['val']);
$this->addArgument('version', InputArgument::REQUIRED, 'Version to validate');
Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"name": "syntatis/version-cli",
"description": "Validate, increment, and compare SemVer-compliant version number with CLI",
"keywords": ["version", "cli", "semver"],
"keywords": [
"version",
"cli",
"semver"
],
"authors": [
{
"name": "Thoriq Firdaus"
Expand Down Expand Up @@ -57,5 +61,8 @@
},
"preferred-install": "dist",
"sort-packages": true
}
},
"non-feature-branches": [
"dependabot/*"
]
}
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ parameters:
level: 10
paths:
- app
- tests/app
- tests/phpunit
ignoreErrors:
-
identifier: missingType.iterableValue
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"
>
<testsuites>
<testsuite name="app">
<directory suffix="Test.php">tests/app/</directory>
<testsuite name="phpunit">
<directory suffix="Test.php">tests/phpunit/</directory>
</testsuite>
</testsuites>
</phpunit>
44 changes: 44 additions & 0 deletions tests/phpunit/Commands/GreaterThanCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Syntatis\Tests\Commands;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Syntatis\Version\CLI\Commander;

use function sprintf;

class GreaterThanCommandTest extends TestCase
{
private Commander $commander;

public function setUp(): void
{
parent::setUp();

$this->commander = new Commander();
}

/** @dataProvider dataComparison */
public function testComparison(string $versionA, string $versionB, string $expect): void
{
$tester = new CommandTester($this->commander->get('gt'));
$tester->execute(['version-a' => $versionA, 'version-b' => $versionB]);

self::assertStringContainsString(
$expect,
$tester->getDisplay(),
);
}

public static function dataComparison(): iterable
{
yield ['1.0.0', '2.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '2.0.0')];
yield ['2.0.0', '1.0.0', sprintf("[OK] Version '%s' is greater than '%s'.", '2.0.0', '1.0.0')];
yield ['1.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '1.0.0')];
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')];
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')];
}
}