Skip to content

Commit

Permalink
release script
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Jan 24, 2024
1 parent 1d6dd38 commit 025d8ed
Showing 1 changed file with 42 additions and 108 deletions.
150 changes: 42 additions & 108 deletions bin/release.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

class Build extends Console
{
/**
* Property help.
*
* @var string
*/
protected $help = <<<HELP
[Usage] php release.php <version> <next_version>
[Usage] php release.php <version>
[Arguments]
version major|minor|patch or direct a version string, keep empty will be "patch".
[Options]
h | help Show help information
Expand All @@ -24,10 +22,7 @@ class Build extends Console

protected array $scripts = [];

/**
* @return bool|mixed
*/
protected function doExecute()
protected function doExecute(): true
{
foreach ($this->scripts as $script) {
$this->exec($script);
Expand All @@ -36,14 +31,11 @@ protected function doExecute()
$currentVersion = trim(file_get_contents(__DIR__ . '/../VERSION'));
$targetVersion = $this->getArgument(0);

if (!$targetVersion) {
$targetVersion = static::versionPlus($currentVersion, 1);
}
$targetVersion = static::versionPlus($currentVersion, $targetVersion);

$this->out('Release version: ' . $targetVersion);

static::writeVersion($targetVersion);
$this->replaceDocblockTags($targetVersion);

$this->exec(sprintf('git commit -am "Release version: %s"', $targetVersion));
$this->exec(sprintf('git tag %s', $targetVersion));
Expand All @@ -54,122 +46,64 @@ protected function doExecute()
return true;
}

public function addScript(string $script): static
{
$this->scripts[] = $script;

return $this;
}

/**
* writeVersion
*
* @param string $version
*
* @return bool|int
*
* @since __DEPLOY_VERSION__
*/
protected static function writeVersion(string $version)
{
return file_put_contents(static::versionFile(), $version . "\n");
}

/**
* versionFile
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected static function versionFile(): string
{
return __DIR__ . '/../VERSION';
}

/**
* versionPlus
*
* @param string $version
* @param int $offset
* @param string $suffix
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected static function versionPlus(string $version, int $offset, string $suffix = ''): string
{
[$version] = explode('-', $version, 2);

$numbers = explode('.', $version);

if (!isset($numbers[2])) {
$numbers[2] = 0;
}

$numbers[2] += $offset;

if ($numbers[2] === 0) {
unset($numbers[2]);
protected static function versionPlus(
string $currentVersion,
string $targetVersion,
string $suffix = ''
): string {
[$currentVersion] = explode('-', $currentVersion, 2) + ['', ''];

[$major, $minor, $patch] = explode('.', $currentVersion, 3) + ['', '0', '0'];
$major = (int) $major;
$minor = (int) $minor;
$patch = (int) $patch;

switch ($targetVersion) {
case 'major':
$major++;
$minor = $patch = 0;
break;

case 'minor':
$minor++;
$patch = 0;
break;

case 'patch':
case '':
$patch++;
break;

default:
return $targetVersion . '-' . $suffix;
}

$version = implode('.', $numbers);
$currentVersion = $major . '.' . $minor . '.' . $patch;

if ($suffix) {
$version .= '-' . $suffix;
$currentVersion .= '-' . $suffix;
}

return $version;
return $currentVersion;
}

/**
* replaceDocblockTags
*
* @param string $version
*
* @return void
*/
protected function replaceDocblockTags(string $version): void
public function addScript(string $script): static
{
$this->out('Replacing Docblock...');

$files = new RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
__DIR__ . '/../src',
\FilesystemIterator::SKIP_DOTS
)
);

/** @var \SplFileInfo $file */
foreach ($files as $file) {
if ($file->isDir() || $file->getExtension() !== 'php') {
continue;
}

$content = file_get_contents($file->getPathname());

$content = str_replace(
['{DEPLOY_VERSION}', '__DEPLOY_VERSION__', '__LICENSE__', '${ORGANIZATION}', '{ORGANIZATION}', '__ORGANIZATION__'],
[$version, $version, 'MIT', 'LYRASOFT', 'LYRASOFT', 'LYRASOFT'],
$content
);

file_put_contents($file->getPathname(), $content);
}
$this->scripts[] = $script;

$this->exec('git checkout main');
$this->exec(sprintf('git commit -am "Prepare for %s release."', $version));
$this->exec('git push origin main');
return $this;
}

/**
* exec
*
* @param string $command
*
* @return static
*/
protected function exec($command)
{
$this->out('>> ' . $command);
Expand Down

0 comments on commit 025d8ed

Please sign in to comment.