7 * ./vendor/bin/release VERSION
10 $semverRegEx = '(?<version>[0-9]+\.[0-9]+\.[0-9]+)(?<prerelease>-[0-9a-zA-Z.]+)?(?<build>\+[0-9a-zA-Z.]*)?';
13 $options = getopt ("dvy", [
18 'pattern' => "^SEMVER$",
20 $simulate = array_key_exists('simulate', $options);
21 $yes = array_key_exists('yes', $options) || array_key_exists('y', $options);
23 $pos_args = array_slice($argv, $optind);
24 $path = array_shift($pos_args);
27 print "Path to version file must be specified as a commandline argument\n";
31 if (!file_exists($path)) {
32 print "Version file not found at $path\n";
36 // The --pattern option is expected to contain the string SEMVER
37 $regex = str_replace('SEMVER', "$semverRegEx", $options['pattern']);
38 if ($regex == $options['pattern']) {
39 print "Pattern '$regex' must contain the string 'SEMVER'.\n";
43 // Read the contents of the version file and find the version string
44 $contents = file_get_contents($path);
45 if (!preg_match("#$regex#m", $contents, $matches)) {
46 print "A semver version not found in $path\n";
49 $matches += ['prerelease' => '', 'build' => ''];
51 // Calculate the stable and next version strings
52 $original_version_match = $matches[0];
53 $original_version = $matches['version'] . $matches['prerelease'] . $matches['build'];
54 $stable_version = $matches['version'] . (has_prerelease($matches) ? $matches['prerelease'] : '');
55 $next_version = next_version($matches);
57 $stable_version_replacement = str_replace($original_version, $stable_version, $original_version_match);
58 $next_version_replacement = str_replace($original_version, $next_version, $original_version_match);
60 $stable_version_contents = str_replace($original_version_match, $stable_version_replacement, $contents);
61 $next_version_contents = str_replace($original_version_match, $next_version_replacement, $contents);
63 $composerContents = file_get_contents('composer.json');
64 $composerData = json_decode($composerContents, true);
65 $project = $composerData['name'];
67 $msg = "Release $project version $stable_version";
68 $dashes = str_pad('', strlen($msg) + 8, '-', STR_PAD_LEFT);
70 print "\n$dashes\n\n";
72 print "\n$dashes\n\n";
74 // Write the stable version into the version file, tag and push the release
76 file_put_contents($path, $stable_version_contents);
79 print "Replace stable version in $path:\n> $stable_version_replacement\n";
82 run('git add {path}', ['{path}' => $path], $simulate);
83 run('git commit -m "Version {version}"', ['{version}' => $stable_version], $simulate);
84 run('git tag {version}', ['{version}' => $stable_version], $simulate);
85 run('git push origin {version}', ['{version}' => $stable_version], $simulate);
87 // Put the next version into the version file and push the result back to master
89 file_put_contents($path, $next_version_contents);
92 print "Replace next version in $path:\n> $next_version_replacement\n";
95 run('git add {path}', ['{path}' => $path], $simulate);
96 run('git commit -m "[ci skip] Back to {version}"', ['{version}' => $next_version], $simulate);
97 run('git push origin master', [], $simulate);
102 * inflect replaces the placeholders in the command with the provided parameter values
104 * @param array $parameters
107 function inflect($cmd, $parameters = [])
109 if (!empty($parameters)) {
110 return str_replace(array_keys($parameters), array_values($parameters), $cmd);
116 * Run the specified command. Abort most rudely if an error is encountered
118 function run($cmd, $parameters = [], $simulate = false)
120 $cmd = inflect($cmd, $parameters);
125 passthru($cmd, $status);
132 * Determine the next version after the current release
134 function next_version($matches)
136 $version = $matches['version'];
138 $next_version = next_version_prerelease($matches);
139 if ($next_version !== false) {
140 return $next_version;
142 return next_version_stable($matches);
146 * Determine the next version given that the current version is stable
148 function next_version_stable($matches)
150 $version_parts = explode('.', $matches['version']);
151 $last_version = array_pop($version_parts);
153 $version_parts[] = $last_version;
155 return implode('.', $version_parts) . (empty($matches['prerelease']) ? '-dev' : $matches['prerelease']);
158 function has_prerelease($matches)
160 if (empty($matches['prerelease'])) {
164 return is_numeric(substr($matches['prerelease'], -1));
168 * Determine the next version given that the current version has a pre-release
171 function next_version_prerelease($version_parts)
173 if (!preg_match('#(.*?)([0-9]+)$#', $version_parts['prerelease'], $matches)) {
176 $next = $matches[2] + 1;
177 return $version_parts['version'] . $matches[1] . $next . '+dev';