Skip to content
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

Add version:* commands #53

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
71 changes: 71 additions & 0 deletions app/Concerns/HandleVersioning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Concerns;

use Syntatis\Utils\Val;
use Version\Extension\Build;
use Version\Extension\PreRelease;
use Version\Version;

use function is_string;
use function ltrim;
use function preg_match;
use function preg_replace;

trait HandleVersioning
{
/**
* Normalize version before it's validated.
*
* @return Version|false
*/
protected static function normalizeVersion(string $version, ?string $errorMessage = null)
{
self::isVersion(ltrim($version, 'v'), $matches);

$major = $matches['major'] ?? null;
$minor = $matches['minor'] ?? null;

if (Val::isBlank($major) || Val::isBlank($minor)) {
return false;
}

$patch = $matches['patch'] ?? 0;
$prerelease = $matches['prerelease'] ?? null;
$buildmetadata = $matches['buildmetadata'] ?? null;

$version = Version::from(
(int) $major,
(int) $minor,
(int) $patch,
is_string($prerelease) ? PreRelease::fromString($prerelease) : null,
is_string($buildmetadata) ? Build::fromString($buildmetadata) : null,
);

return $version;
}

/** @param array<mixed>|null $matches */
private static function isVersion(string $version, ?array &$matches = null): bool
{
/**
* Match version without the patch, pre-release, and build metadata parts
* since WordPress only accepts stable version.
*
* @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
*/
$matched = preg_match('/^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)(?:\.(?P<patch>0|[1-9]\d*))?$/', $version, $matches);

return $matched === 1;
}

/**
* Some version in WordPress does not require the patch part.
*/
private static function removePatchPart(string $version): string
{
return (string) preg_replace('/\.\d+$/', '', $version);
}
}
2 changes: 2 additions & 0 deletions app/Console/Commander.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ private function getCommands(): array
new ProjectInitCommand($this->projectPath),
new ScoperInitCommand($this->projectPath),
new ScoperPurgeCommand($this->projectPath),
new VersionBumpCommand($this->projectPath),
new VersionInfoCommand($this->projectPath),
];
}
}
99 changes: 99 additions & 0 deletions app/Console/Processes/WPPluginVersionBumpProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Console\Processes;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Syntatis\Codex\Companion\Codex;
use Syntatis\Codex\Companion\Concerns\RunProcess;
use Syntatis\Codex\Companion\Contracts\Executable;
use Syntatis\Codex\Companion\Contracts\Versionable;
use Syntatis\Codex\Companion\Helpers\Versions\WPPluginVersion;
use Syntatis\Codex\Companion\Helpers\WPPluginProps;
use Version\Version;

use function sprintf;

class WPPluginVersionBumpProcess implements Executable
{
use RunProcess;

private Codex $codex;

/**
* // phpcs:ignore
* @param StyleInterface&OutputInterface $output
*/
public function __construct(Codex $codex, $output)
{
$this->codex = $codex;
$this->output = $output;
}

public function execute(): int
{
/**
* @var string $versionLabel
* @phpstan-var "Stable tag"|"Requires at least"|"Requires PHP" $versionLabel
*/
$versionLabel = $this->output->choice('Which version would you like to bump?', [
'Stable tag',
'Requires at least',
'Requires PHP',
], 'Stable tag');

$versionPart = $this->output->choice(
sprintf('Select "%s" version part to bump', $versionLabel),
$versionLabel === 'Stable tag' ? [
'major',
'minor',
'patch',
] : [
'major',
'minor',
],
);

$wpPluginProps = new WPPluginProps($this->codex);
$currentVersion = $wpPluginProps->getVersion($this->getVersionKey($versionLabel));

$this->output->confirm(
sprintf(
'Bump % version from %s. Would you like to bump it?',
$versionLabel,
$currentVersion,
(string) Version::fromString($currentVersion)->incrementPatch(),
),
);

return 0;
}

private function getVersionKey(string $label): string
{
if ($label === 'Stable tag') {
return 'wp_plugin_version';
}

return 'version';
}

private function getIncrementedVersion(string $label, string $currentVersion, string $versionParth): string
{
switch ($label) {
case 'Stable tag':
$versionable = new WPPluginVersion($currentVersion);
break;
}

$versionable = Version::fromString($currentVersion);

if (! ($versionable instanceof Versionable)) {
return;
}

$versionable->incrementPatch();
}
}
52 changes: 52 additions & 0 deletions app/Console/Processes/WPPluginVersionInfoProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Console\Processes;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Syntatis\Codex\Companion\Codex;
use Syntatis\Codex\Companion\Concerns\RunProcess;
use Syntatis\Codex\Companion\Contracts\Executable;
use Syntatis\Codex\Companion\Helpers\WPPluginProps;
use Throwable;

use function sprintf;

class WPPluginVersionInfoProcess implements Executable
{
use RunProcess;

private Codex $codex;

/**
* // phpcs:ignore
* @param StyleInterface&OutputInterface $output
*/
public function __construct(Codex $codex, $output)
{
$this->codex = $codex;
$this->output = $output;
}

public function execute(): int
{
try {
$props = new WPPluginProps($this->codex);

$this->output->listing([
sprintf('<info>Version (Stable tag):</info> <comment>%s</comment>', (string) $props->getVersion('wp_plugin_version')),
sprintf('<info>Tested up to:</info> <comment>%s</comment>', (string) $props->getVersion('wp_plugin_tested_up_to')),
sprintf('<info>Requires at least:</info> <comment>%s</comment>', (string) $props->getVersion('wp_plugin_requires_at_least')),
sprintf('<info>Requires PHP:</info> <comment>%s</comment>', (string) $props->getVersion('wp_plugin_requires_php')),
]);
} catch (Throwable $th) {
$this->output->error($th->getMessage());

return 1;
}

return 0;
}
}
2 changes: 1 addition & 1 deletion app/Console/ProjectInitCommand/Howdy.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function execute(): int
}

try {
$userInputs = new UserInputPrompts($projectProps->get(), $this->style);
$userInputs = new UserInputPrompts($projectProps->getAll(), $this->style);
$userInputs->execute();

$projectFiles = new ProjectFiles($this->codex);
Expand Down
10 changes: 5 additions & 5 deletions app/Console/ProjectInitCommand/Howdy/UserInputPrompts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Symfony\Component\Console\Style\StyleInterface;
use Syntatis\Codex\Companion\Contracts\Executable;
use Syntatis\Codex\Companion\Exceptions\MissingRequiredInfo;
use Syntatis\Codex\Companion\Helpers\PHPNamespace;
use Syntatis\Codex\Companion\Helpers\PHPVendorPrefix;
use Syntatis\Codex\Companion\Helpers\WPPluginDescription;
use Syntatis\Codex\Companion\Helpers\WPPluginName;
use Syntatis\Codex\Companion\Helpers\WPPluginSlug;
use Syntatis\Codex\Companion\Helpers\Strings\PHPNamespace;
use Syntatis\Codex\Companion\Helpers\Strings\PHPVendorPrefix;
use Syntatis\Codex\Companion\Helpers\Strings\WPPluginDescription;
use Syntatis\Codex\Companion\Helpers\Strings\WPPluginName;
use Syntatis\Codex\Companion\Helpers\Strings\WPPluginSlug;
use Syntatis\Codex\Companion\Projects\Howdy\InitializeFiles;
use Syntatis\Utils\Str;
use Syntatis\Utils\Val;
Expand Down
36 changes: 36 additions & 0 deletions app/Console/VersionBumpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Syntatis\Codex\Companion\Codex;
use Syntatis\Codex\Companion\Console\Processes\WPPluginVersionBumpProcess;

class VersionBumpCommand extends BaseCommand
{
protected function configure(): void
{
$this->setName('version:bump');
$this->setAliases(['v:bump', 'ver:bump']);
$this->setDescription('Bump versions in the project.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$codex = new Codex($this->projectPath);
$style = new SymfonyStyle($input, $output);
$type = $codex->getComposer('type');

if ($type === 'wordpress-plugin') {
return (new WPPluginVersionBumpProcess($codex, $style))->execute();
}

$style->warning('Unsupported project type.');

return 0;
}
}
36 changes: 36 additions & 0 deletions app/Console/VersionInfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Syntatis\Codex\Companion\Codex;
use Syntatis\Codex\Companion\Console\Processes\WPPluginVersionInfoProcess;

class VersionInfoCommand extends BaseCommand
{
protected function configure(): void
{
$this->setName('version:info');
$this->setAliases(['v:info', 'ver:info']);
$this->setDescription('Retrieve versions of the current project.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$codex = new Codex($this->projectPath);
$style = new SymfonyStyle($input, $output);
$type = $codex->getComposer('type');

if ($type === 'wordpress-plugin') {
return (new WPPluginVersionInfoProcess($codex, $style))->execute();
}

$style->warning('Unsupported project type.');

return 0;
}
}
10 changes: 10 additions & 0 deletions app/Contracts/VersionPatchIncrementable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Contracts;

interface VersionPatchIncrementable
{
public function incrementPatch(): Versionable;
}
14 changes: 14 additions & 0 deletions app/Contracts/Versionable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Contracts;

use Stringable;

interface Versionable extends Stringable
{
public function incrementMinor(): Versionable;

public function incrementMajor(): Versionable;
}
11 changes: 11 additions & 0 deletions app/Exceptions/InvalidVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Exceptions;

use InvalidArgumentException;

class InvalidVersion extends InvalidArgumentException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Helpers;
namespace Syntatis\Codex\Companion\Helpers\Strings;

use InvalidArgumentException;
use Stringable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Helpers;
namespace Syntatis\Codex\Companion\Helpers\Strings;

use InvalidArgumentException;
use Stringable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Syntatis\Codex\Companion\Helpers;
namespace Syntatis\Codex\Companion\Helpers\Strings;

use InvalidArgumentException;
use Stringable;
Expand Down
Loading
Loading