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
47 changes: 34 additions & 13 deletions bin/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

use Composer\Semver\Semver;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Finder\Finder;
Expand All @@ -24,6 +24,20 @@ function runCmd($commandLine, $cwd): string
return rtrim($output);
}

function isValidStableSemver(string $tag): bool
{
$versionParser = new \Composer\Semver\VersionParser;
try {
$version = $versionParser->normalize($tag);
if ($versionParser::parseStability($version) === 'stable') {
return true;
}
} catch (Throwable) {
// ignore
}
return false;
}

(new SingleCommandApplication())
->setName('Tag me')
->setCode(code: function (InputInterface $input, OutputInterface $output): int {
Expand All @@ -46,31 +60,38 @@ function runCmd($commandLine, $cwd): string
$package = $helper->ask($input, $output, $question);
$output->writeln(sprintf('You have just selected: "%s"', basename($package)));

$tagPrefix = sprintf("%s/%s-", basename($package), $branch);
$packagePrefix = basename($package) . '/';
$tags = runCmd(sprintf(
'git tag --sort=refname -l \'%s*\'',
$packagePrefix
),
$cwd
);
$tags = explode(PHP_EOL, $tags);
$tags = array_map(fn($i) => str_replace($packagePrefix, '', $i), $tags);
$tags = array_filter($tags, fn($i) => isValidStableSemver($i));
$tags = Semver::rsort($tags);
$latest = '1';
if ($tags !== []) {
$latest = $tags[0];
}

$tagSuffix = '-dev';
$tagPrefix = sprintf("%s/%s-%s-", basename($package), $latest, $branch);
$tags = runCmd(sprintf('git tag --sort=refname -l \'%s*\'', $tagPrefix), $cwd);
$tags = explode(PHP_EOL, $tags);

$lastNumericTagId = 0;
if ($tags !== []) {
foreach ($tags as $tag) {
$tagId = str_replace($tagPrefix, '', $tag);
$tagId = str_replace([$tagPrefix, $tagSuffix], '', $tag);
if (is_numeric($tagId)) {
$lastNumericTagId = (int) $tagId;
}
}
}
$newTagId = $lastNumericTagId + 1;
$question = new Question(
sprintf(
'Add tag suffix "%s{suffix}" manually or ignore this by pressing <enter> which will set "%s"',
$tagPrefix,
$tagPrefix . $newTagId
),
$newTagId
);
$suffix = $helper->ask($input, $output, $question);
$newTag = $tagPrefix . $suffix;
$newTag = $tagPrefix . $newTagId . $tagSuffix;

$output->writeln(sprintf('New tag will be "%s"', $newTag));

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"php": "^8.1",
"symfony/console": "^5.2|^6",
"symfony/filesystem": "^5.2|^6",
"symfony/process": "^4.4|^5.0|^6.0"
"symfony/process": "^4.4|^5.0|^6.0",
"composer/semver": "^3.4"
},
"require-dev": {
"keboola/coding-standard": "^14",
Expand Down