Skip to content

Commit 66ddb8b

Browse files
committed
Release version: 2.0.0
1 parent 0503f70 commit 66ddb8b

File tree

2 files changed

+185
-1
lines changed

2 files changed

+185
-1
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.0-beta3
1+
2.0.0

bin/release.php

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
use Asika\SimpleConsole\Console;
8+
9+
require __DIR__ . '/../vendor/autoload.php';
10+
11+
$app = new class () extends Console
12+
{
13+
protected array $scripts = [];
14+
15+
protected bool $isDryRun {
16+
get {
17+
return (bool) $this->get('dry-run');
18+
}
19+
}
20+
21+
protected function configure(): void
22+
{
23+
$this->addParameter('version', type: static::STRING)
24+
->description('Can be <version> name or major|minor|patch|alpha|beta|rc')
25+
->default('patch');
26+
27+
$this->addParameter('suffix', type: static::STRING)
28+
->description('The suffix type. Can be alpha|beta|rc');
29+
30+
$this->addParameter('--dry-run|-d', type: static::BOOLEAN)
31+
->description('Run process but do not execute any commands.');
32+
33+
$this->addParameter('--from', type: static::STRING)
34+
->description('The version to release from. Default is the current version.')
35+
->required(true);
36+
}
37+
38+
protected function doExecute(): int
39+
{
40+
foreach ($this->scripts as $script) {
41+
$this->exec($script);
42+
}
43+
44+
$currentVersion = $this->get('from') ?: trim(file_get_contents(__DIR__ . '/../VERSION'));
45+
$targetVersion = (string) $this->get('version');
46+
$targetSuffix = (string) $this->get('suffix');
47+
48+
if (in_array($targetVersion, ['alpha', 'beta', 'rc'])) {
49+
$targetSuffix = $targetVersion;
50+
$targetVersion = 'patch';
51+
}
52+
53+
$targetVersion = static::versionPush($currentVersion, $targetVersion, $targetSuffix);
54+
55+
$this->writeln('Release version: ' . $targetVersion);
56+
57+
if (!$this->isDryRun) {
58+
static::writeVersion($targetVersion);
59+
}
60+
61+
$this->exec(sprintf('git commit -am "Release version: %s"', $targetVersion));
62+
$this->exec(sprintf('git tag %s', $targetVersion));
63+
64+
$this->exec('git push');
65+
$this->exec('git push --tags');
66+
67+
return static::SUCCESS;
68+
}
69+
70+
protected static function writeVersion(string $version): false|int
71+
{
72+
return file_put_contents(static::versionFile(), $version . "\n");
73+
}
74+
75+
protected static function versionFile(): string
76+
{
77+
return __DIR__ . '/../VERSION';
78+
}
79+
80+
protected static function versionPush(
81+
string $currentVersion,
82+
string $targetVersion,
83+
string $targetSuffix,
84+
): string {
85+
[$major, $minor, $patch, $suffixType, $suffixVersion] = static::parseVersion($currentVersion);
86+
87+
switch ($targetVersion) {
88+
case 'major':
89+
$major++;
90+
$minor = $patch = 0;
91+
if ($targetSuffix) {
92+
$suffixType = $targetSuffix;
93+
$suffixVersion = 1;
94+
} else {
95+
$suffixType = '';
96+
$suffixVersion = 0;
97+
}
98+
break;
99+
100+
case 'minor':
101+
$minor++;
102+
$patch = 0;
103+
if ($targetSuffix) {
104+
$suffixType = $targetSuffix;
105+
$suffixVersion = 1;
106+
} else {
107+
$suffixType = '';
108+
$suffixVersion = 0;
109+
}
110+
break;
111+
112+
case 'patch':
113+
if (!$suffixType) {
114+
$patch++;
115+
}
116+
if ($targetSuffix) {
117+
if ($suffixType === $targetSuffix) {
118+
$suffixVersion++;
119+
} else {
120+
$suffixType = $targetSuffix;
121+
$suffixVersion = 1;
122+
}
123+
} else {
124+
$suffixType = '';
125+
$suffixVersion = 0;
126+
}
127+
break;
128+
129+
default:
130+
return $targetVersion;
131+
}
132+
133+
$currentVersion = $major . '.' . $minor . '.' . $patch;
134+
135+
if ($suffixType) {
136+
$currentVersion .= '-' . $suffixType . '.' . $suffixVersion;
137+
}
138+
139+
return $currentVersion;
140+
}
141+
142+
public static function parseVersion(string $currentVersion): array
143+
{
144+
[$currentVersion, $prerelease] = explode('-', $currentVersion, 2) + ['', ''];
145+
146+
[$major, $minor, $patch] = explode('.', $currentVersion, 3) + ['', '0', '0'];
147+
$major = (int) $major;
148+
$minor = (int) $minor;
149+
$patch = (int) $patch;
150+
$prereleaseType = '';
151+
$prereleaseVersion = 0;
152+
153+
if ($prerelease) {
154+
$matched = preg_match('/(rc|beta|alpha)[.-]?(\d+)/i', $prerelease, $matches);
155+
156+
if ($matched) {
157+
$prereleaseType = strtolower($matches[1]);
158+
$prereleaseVersion = (int) $matches[2];
159+
}
160+
}
161+
162+
return [$major, $minor, $patch, $prereleaseType, $prereleaseVersion];
163+
}
164+
165+
public function exec(string $cmd, ?\Closure $output = null, bool $showCmd = true): int
166+
{
167+
$this->writeln('>> ' . ($this->isDryRun ? '(Dry Run) ' : '') . $cmd);
168+
169+
if (!$this->isDryRun) {
170+
return parent::exec($cmd, $output, false);
171+
}
172+
173+
return 0;
174+
}
175+
176+
public function addScript(string $script): static
177+
{
178+
$this->scripts[] = $script;
179+
180+
return $this;
181+
}
182+
};
183+
184+
$app->execute();

0 commit comments

Comments
 (0)