Created
October 5, 2022 12:43
-
-
Save rasteiner/6fd7661ccd86374f7c959e7dea3ce29a to your computer and use it in GitHub Desktop.
kirby cli plugin version bump command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'description' => 'Release a new version of your plugin', | |
'args' => [ | |
'bump' => [ | |
'description' => 'The type of version bump to perform', | |
], | |
], | |
'command' => static function ($cli): void { | |
// check if this is a plugin directory | |
if(!file_exists('composer.json')) { | |
$cli->error('This is not a plugin directory'); | |
exit(1); | |
} | |
$package = json_decode(file_get_contents('composer.json')); | |
if(!$package->type === 'kirby-plugin') { | |
$cli->error('This is not a plugin directory'); | |
exit(1); | |
} | |
$current = $package->version ?? false; | |
if(!$current) { | |
$cli->comment('The plugin does not have a version number in composer.json'); | |
$cli->comment('Version 0.0.0 will be assumed'); | |
$current = '0.0.0'; | |
} else { | |
$cli->comment('Current version: ' . $current); | |
} | |
// check if the version is valid | |
if(!preg_match('/^(\d+)\.(\d+)\.(\d+)$/', $current)) { | |
$cli->error('The current version is not valid semver'); | |
exit(1); | |
} | |
[$major, $minor, $patch] = array_map('intval', explode('.', $current)); | |
$whenMajor = ($major+1) . '.0.0'; | |
$whenMinor = $major . '.' . ($minor+1) . '.0'; | |
$whenPatch = $major . '.' . $minor . '.' . ($patch+1); | |
$bump = $cli->arg('bump'); | |
if(empty($bump) || !in_array($bump, ['major', 'minor', 'patch'])) { | |
$bump = $cli->radio('What type of version bump would you like to perform?', [ | |
'patch' => "Patch ($current -> $whenPatch)", | |
'minor' => "Minor ($current -> $whenMinor)", | |
'major' => "Major ($current -> $whenMajor)", | |
])->prompt(); | |
} | |
$newVersion = match($bump) { | |
'patch' => $whenPatch, | |
'minor' => $whenMinor, | |
'major' => $whenMajor, | |
}; | |
$package->version = $newVersion; | |
file_put_contents('composer.json', json_encode($package, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL); | |
$cli->success('Successfully bumped version to ' . $newVersion); | |
// check if git is installed && if this is a git repository | |
if(!exec('git --version') || !file_exists('.git')) { | |
$cli->comment('This is not a git repository - skipping git commands'); | |
return; | |
} | |
if($cli->confirm('Do you want to tag the new version?')->confirmed()) { | |
passthru('git add composer.json', $code); | |
if($code !== 0) { | |
$cli->error('Could not add composer.json to git'); | |
exit(1); | |
} | |
passthru('git commit -m "Release ' . $newVersion . '"', $code); | |
if($code !== 0) { | |
$cli->error('Could not commit composer.json'); | |
exit(1); | |
} | |
passthru('git tag -a ' . $newVersion . ' -m "Release ' . $newVersion . '"', $code); | |
if($code !== 0) { | |
$cli->error('Could not tag the new version'); | |
exit(1); | |
} | |
if($cli->confirm('Do you want to push the new tag?')->confirmed()) { | |
passthru('git push origin ' . $newVersion, $code); | |
if($code !== 0) { | |
$cli->error('Could not push the new tag'); | |
exit(1); | |
} | |
} | |
} | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment