Created
October 21, 2014 15:27
-
-
Save kostajh/71c2ce90ccd2f8bd2fdf to your computer and use it in GitHub Desktop.
git flow bump versions script
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 | |
/** | |
* @file | |
* Drush script for bumping versions according to a git release. | |
*/ | |
// Get the context. | |
$context = drush_get_context(); | |
if (!$context['alias']) { | |
return drush_set_error('You must run this command with an alias. Example: drush @myproject.local scr /path/to/bump-versions.php --prefix=myproject'); | |
} | |
$docroot = $context['alias']['root']; | |
if (!file_exists($docroot)) { | |
return drush_set_error('Docroot does not exist!'); | |
} | |
if (!drush_shell_cd_and_exec($docroot, 'git rev-parse --symbolic-full-name --abbrev-ref HEAD')) { | |
return drush_set_error('Failed to get git checked out branch.'); | |
} | |
if (!$custom_package_prefix = drush_get_option('prefix')) { | |
return drush_set_error('You must specify the prefix for your custom packages, e.g. `myproject`'); | |
} | |
// Allow user to pass in an arbitrary release string. | |
if (!$release = drush_get_option('release')) { | |
$branch = current(drush_shell_exec_output()); | |
// Parse the release. | |
if (strpos($branch, 'release') !== 0 || !strpos($branch, '/')) { | |
return drush_set_error('You are not on a release branch.'); | |
} | |
$release = substr($branch, strpos($branch, '/') + 1); | |
} | |
// Convert the version string to a Drupal compatible version string. | |
$version = explode('.', $release); | |
$major = $version[0]; | |
$minor = $version[1]; | |
$patch = $version[2]; | |
$drupal_version = sprintf('7.x-%d.%d-rc%d', $major, $minor, $patch); | |
if (!drush_confirm(dt('Set the version string to !version?', array('!version' => $drupal_version)))) { | |
return drush_user_abort(); | |
} | |
drush_log(dt('Setting the Drupal version string to !version', array('!version' => $drupal_version)), 'ok'); | |
// Get list of projects. | |
$ret = drush_invoke_process($context['alias'], 'pml', array(), array('pipe'), FALSE); | |
$modules = array_keys($ret['object']); | |
$custom_package_names = array(); | |
foreach ($modules as $module) { | |
if (strpos($module, $custom_package_prefix) === 0) { | |
$custom_package_names[] = $module; | |
} | |
} | |
// Get package info. | |
drush_log(dt('Getting package info for !count projects...', array('!count' => count($custom_package_names))), 'ok'); | |
$package_info = drush_invoke_process($context['alias'], 'pmi', $custom_package_names, array(), FALSE); | |
$custom_packages = array(); | |
foreach ($package_info['object'] as $package) { | |
$custom_packages[$package['extension']] = $package['path']; | |
} | |
// Update the release string. | |
foreach ($custom_packages as $package => $path) { | |
$info_file = file($docroot . '/' . $path . '/' . $package . '.info'); | |
$new_info_file = array(); | |
foreach ($info_file as $key => $line) { | |
if (strpos($line, 'version') === 0) { | |
$new_info_file[] = 'version = ' . $drupal_version . "\n"; | |
} | |
else { | |
$new_info_file[] = $line; | |
} | |
} | |
// Write the file. | |
if (file_put_contents($docroot . '/' . $path . '/' . $package . '.info', $new_info_file)) { | |
drush_log(dt('Successfully incremented version for project !project', array('!project' => $package)), 'ok'); | |
} | |
else { | |
drush_set_error('Failed to increment version for project !project', array('!project' => $project)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment