Skip to content

Commit

Permalink
Add release task to RoboFile.php
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinmo committed Mar 22, 2024
1 parent 0152817 commit fec69ba
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 5 deletions.
91 changes: 90 additions & 1 deletion RoboFile.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

use Robo\Tasks as RoboTasks;
use Robo\Symfony\ConsoleIO;
use Symfony\Component\Finder\Finder;
use PHLAK\SemVer\Version;

/**
* This is project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
class RoboFile extends \Robo\Tasks {
class RoboFile extends RoboTasks {
protected function checkPharReadonly() {
if (ini_get('phar.readonly')) {
throw new \Exception('Must set "phar.readonly = Off" in php.ini to build phars.');
Expand Down Expand Up @@ -71,4 +74,90 @@ public function update_copyright() {
->run();
}
}

/**
* Prepares a release.
*
* @param string $type one of major, minor, patch, pre-release
* @param array $opts
* @option $dry-run Dry-run, do not make changes
* @option $ignore-worktree Ignore unstaged and uncommitted changes
* @option $prefix Prefix to be prepended to version number
* @option $update-changelog Update the Changelog
* @option $changelog File name to changelog file
* @option $push Push git changes
*/
public function release(ConsoleIO $io, $type = 'patch', $opts = [ 'prefix' => 'v', 'dry-run' => false, 'update-changelog' => true, 'changelog' => 'CHANGELOG.md', 'push' => true, 'ignore-worktree' => false ]) {
// 1. Check parameters and unstage/uncommitted changes
if (!in_array($type, ['major', 'minor', 'patch', 'pre-release'])) {
$io->error("type is not one of major, minor, patch, pre-release");
return 1;
}

if (!$opts['ignore-worktree']) {
$clean_worktree = $this->taskGitStack()
->exec('update-index -q --ignore-submodules --refresh')
->exec('diff-files --quiet --ignore-submodules')
->exec('diff-index --cached --quiet HEAD --ignore-submodules')
->run();
if (!$clean_worktree->wasSuccessful()) {
$io->error('You have unstaged or uncommitted changes');
return $clean_worktree;
}
}

// 2. Get the current tag
$version_task = $this->taskExec('git describe --tags --abbrev=0 HEAD')->run();
if (!$version_task->wasSuccessful()) {
return $version_task;
}

// 3. Get the current version from tag
$tag = trim($version_task->getMessage());
$version = Version::parse($tag);
$io->say('Current version: ' . (string) $version);

// 4. Get bumped version
$new_version = clone $version;
switch ($type) {
case 'major': $new_version->incrementMajor(); break;
case 'minor': $new_version->incrementMinor(); break;
case 'patch': $new_version->incrementPatch(); break;
case 'pre-release': $new_version->incrementPrerelease(); break;
}
$io->say('New version: ' . (string) $new_version);

$col = $this->collectionBuilder($io);
if ($opts['dry-run']) $col->simulated();

// 5. Update changelog
if ($opts['update-changelog']) {
$col->taskReplaceInFile($opts['changelog'])
->regex('/^## \[unreleased\]/mi')
->to('## [' . (string) $new_version . ']')
->limit(1);
$col->taskReplaceInFile($opts['changelog'])
->regex('/^\[unreleased\]:(\s?(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:\/?#[\]@!$&\'()*+,;=.]+)\.\.\.HEAD/mi')
->to('[' . (string) $new_version . ']:$1...' . $new_version->prefix($opts['prefix']));
}

// 6. Add and commit CHANGELOG.md
$git = $col->taskGitStack();

if ($opts['update-changelog']) {
$git->add($opts['changelog']);
$git->commit('Version ' . (string) $new_version);
}

// 7. Tag
$git->tag($new_version->prefix($opts['prefix']));

// 8. Push
if ($opts['push']) {
if ($opts['update-changelog']) $git->push();
$git->push('--tags');
}

return $col->run();
}
}
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"phpunit/phpunit": "^8.0 || ^9.3 || ^10.0",
"consolidation/robo": "^3.0 || ^4.0",
"phpstan/phpstan": "^1.3.0",
"bamarni/composer-bin-plugin": "^1.8"
"bamarni/composer-bin-plugin": "^1.8",
"phlak/semver": "^4.1"
},
"suggest": {
"ext-sodium": "Provides support for OKP (X25519/Ed25519) keys"
Expand All @@ -37,7 +38,11 @@
"test": [ "@composer install", "phpunit" ],
"coverage": [ "@composer install", "phpdbg -qrr vendor/bin/phpunit --coverage-text" ],
"phpstan": [ "@composer install", "phpstan analyse" ],
"update-copyright": [ "@composer install", "robo update_copyright" ]
"update-copyright": [ "@composer install", "robo update_copyright" ],
"release:major": [ "@composer install", "robo release major" ],
"release:minor": [ "@composer install", "robo release minor" ],
"release:patch": [ "@composer install", "robo release patch" ],
"release:pre-release": [ "@composer install", "robo release pre-release" ]
},
"config": {
"allow-plugins": {
Expand Down
63 changes: 61 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fec69ba

Please sign in to comment.