Skip to content

Instantly share code, notes, and snippets.

@mikey179
Last active May 19, 2017 08:39
Show Gist options
  • Save mikey179/1989568 to your computer and use it in GitHub Desktop.
Save mikey179/1989568 to your computer and use it in GitHub Desktop.
Release script
#!/usr/bin/env php
<?php
echo "Release script\n";
echo " (c) 2013 Frank Kleine\n";
echo " This software is Drinkware. If you happen to meet the author spend him a drink.\n\n";
$pwd = getcwd();
if (false === $pwd) {
error("Can't retrieve current directory - please check file permissions.");
exit(1);
}
$svnTrunkUrl = getSvnTrunkUrl();
echo "Create release from " . $svnTrunkUrl . "\n";
$output = execute('svn st 2>&1 | tail -n1', 'Failure while checking svn status', 5);
if (count($output) > 0) {
error("Can't create release, working directory not clean.\n");
passthru('svn st');
exit(6);
}
passthru('svn up'); // definately a problem as it can pull new untested stuff, but we want to be up to date herecd s
if (!file_exists($pwd . DIRECTORY_SEPARATOR . 'composer.json')) {
error("No composer.json found - are you sure this is a composer package?");
exit(7);
}
echo "Last 5 releases:\n";
passthrough('svn list ' . getSvnTagUrl($svnTrunkUrl) . ' | grep "v" | sort -r | head -5', 8);
echo "\nPlease name the version to release (leave empty to abort): ";
$newVersion = trim(rtrim(fgets(STDIN), "\r\n"));
if (empty($newVersion)) {
error("You must provide a version.\n");
exit(9);
}
passthrough('svn cp . ' . getSvnTagUrl($svnTrunkUrl) . '/' . $newVersion . ' -m "tag release ' . $newVersion . '"', 10);
echo "Successfully created release " . $newVersion . "\n";
exit(0);
/**
* retrieves SVN URL of project
*
* In case checkout is not trunk it will exit with an error;
*
* @return string
*/
function getSvnTrunkUrl()
{
$svnInfo = execute('svn info 2>&1', 'Failure while checking svn info', 2);
if (!isset($svnInfo[1])) {
error('Command svn info failed');
exit(3);
}
$svnTrunkUrl = str_replace('URL: ', '', $svnInfo[1]);
if (substr($svnTrunkUrl, -5) !== 'trunk') {
error('Current checkout is not trunk, can create releases from trunk only');
exit(4);
}
return $svnTrunkUrl;
}
/**
* creates tag url from svn url which is supposed to be trunk
*
* @param string $svnUrl
* @return string
*/
function getSvnTagUrl($svnTrunkUrl)
{
return substr($svnTrunkUrl, 0, strlen($svnTrunkUrl) - 5) . 'tags';
}
/**
* calls passthru() and exits with given exit code if return code is not 0
*
* @param string $command
* @param int $exitCode
*/
function passthrough($command, $exitCode)
{
$return = 0;
passthru($command, $return);
if (0 !== $return) {
exit($exitCode);
}
}
/**
* execute given command
*
* If the command fails it prints the given error message and calls exit() with
* the given exit code.
*
* Returns the output of the command if successful.
*
* @param string $command
* @param string $errorMsg
* @param int $exitCode
* @return string[]
*/
function execute($command, $errorMsg, $exitCode)
{
$output = array();
$return = 0;
exec($command, $output, $return);
if (0 !== $return) {
error($errorMsg . ": " . join("\n", $output));
exit($exitCode);
}
return $output;
}
/**
* prints out given error message
*
* @param string $message
*/
function error($message)
{
echo 'ERROR: ' . $message . "\n";
}
#!/usr/bin/env php
<?php
echo "Release script\n";
echo " (c) 2012 Frank Kleine\n";
echo " This software is Drinkware. If you happen to meet the author spend him a drink.\n\n";
$pwd = getcwd();
if (false === $pwd) {
error("Can't retrieve current directory - please check file permissions.");
exit(1);
}
$output = execute('git status 2> /dev/null | tail -n1', 'Failure while checking git status', 2);
if (!isset($output[0])) {
error("Current directory is not a git repository.\n");
exit(3);
} elseif ('nothing to commit (working directory clean)' !== $output[0]) {
error("Can't create release, working directory not clean.\n");
passthru('git status');
exit(4);
}
if (!file_exists($pwd . DIRECTORY_SEPARATOR . 'composer.json')) {
error("No composer.json found - are you sure this is a composer package?");
exit(5);
}
echo "Last 5 releases:\n";
passthrough('git tag -l | grep "v" | sort -r | head -5', 6);
echo "\nPlease name the version to release (leave empty to abort): ";
$newVersion = trim(rtrim(fgets(STDIN), "\r\n"));
if (empty($newVersion)) {
error("You must provide a version.\n");
exit(7);
}
passthrough('git tag -a ' . $newVersion . ' -m "tag release ' . $newVersion . '"', 8);
passthrough('git push --tags', 9);
echo "Successfully created release " . $newVersion . "\n";
exit(0);
/**
* calls passthru() and exits with given exit code if return code is not 0
*
* @param string $command
* @param int $exitCode
*/
function passthrough($command, $exitCode)
{
$return = 0;
passthru($command, $return);
if (0 !== $return) {
exit($exitCode);
}
}
/**
* execute given command
*
* If the command fails it prints the given error message and calls exit() with
* the given exit code.
*
* Returns the output of the command if successful.
*
* @param string $command
* @param string $errorMsg
* @param int $exitCode
* @return string[]
*/
function execute($command, $errorMsg, $exitCode)
{
$output = array();
$return = 0;
exec($command, $output, $return);
if (0 !== $return) {
error($errorMsg . ": " . join("\n", $output));
exit($exitCode);
}
return $output;
}
/**
* prints out given error message
*
* @param string $message
*/
function error($message)
{
echo 'ERROR: ' . $message . "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment