Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / consolidation / self-update / scripts / release
1 #!/usr/bin/env php
2 <?php
3
4 /**
5  * Usage:
6  *
7  * ./vendor/bin/release VERSION
8  */
9
10 $semverRegEx = '(?<version>[0-9]+\.[0-9]+\.[0-9]+)(?<prerelease>-[0-9a-zA-Z.]+)?(?<build>\+[0-9a-zA-Z.]*)?';
11
12 $optind = null;
13 $options = getopt ("dvy", [
14   'pattern:',
15   'simulate',
16   'yes',
17 ], $optind) + [
18   'pattern' => "^SEMVER$",
19 ];
20 $simulate = array_key_exists('simulate', $options);
21 $yes = array_key_exists('yes', $options) || array_key_exists('y', $options);
22
23 $pos_args = array_slice($argv, $optind);
24 $path = array_shift($pos_args);
25
26 if (empty($path)) {
27     print "Path to version file must be specified as a commandline argument\n";
28     exit(1);
29 }
30
31 if (!file_exists($path)) {
32     print "Version file not found at $path\n";
33     exit(1);
34 }
35
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";
40     exit(1);
41 }
42
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";
47     exit(1);
48 }
49 $matches += ['prerelease' => '', 'build' => ''];
50
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);
56
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);
59
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);
62
63 $composerContents = file_get_contents('composer.json');
64 $composerData = json_decode($composerContents, true);
65 $project = $composerData['name'];
66
67 $msg = "Release $project version $stable_version";
68 $dashes = str_pad('', strlen($msg) + 8, '-', STR_PAD_LEFT);
69
70 print "\n$dashes\n\n";
71 print "    $msg\n";
72 print "\n$dashes\n\n";
73
74 // Write the stable version into the version file, tag and push the release
75 if (!$simulate) {
76     file_put_contents($path, $stable_version_contents);
77 }
78 else {
79     print "Replace stable version in $path:\n> $stable_version_replacement\n";
80 }
81
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);
86
87 // Put the next version into the version file and push the result back to master
88 if (!$simulate) {
89     file_put_contents($path, $next_version_contents);
90 }
91 else {
92     print "Replace next version in $path:\n> $next_version_replacement\n";
93 }
94
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);
98
99 exit(0);
100
101 /**
102  * inflect replaces the placeholders in the command with the provided parameter values
103  * @param string $cmd
104  * @param array $parameters
105  * @return string
106  */
107 function inflect($cmd, $parameters = [])
108 {
109     if (!empty($parameters)) {
110         return str_replace(array_keys($parameters), array_values($parameters), $cmd);
111     }
112     return $cmd;
113 }
114
115 /**
116  * Run the specified command. Abort most rudely if an error is encountered
117  */
118 function run($cmd, $parameters = [], $simulate = false)
119 {
120     $cmd = inflect($cmd, $parameters);
121     if ($simulate) {
122         print "$cmd\n";
123         return;
124     }
125     passthru($cmd, $status);
126     if ($status) {
127         exit($status);
128     }
129 }
130
131 /**
132  * Determine the next version after the current release
133  */
134 function next_version($matches)
135 {
136     $version = $matches['version'];
137
138     $next_version = next_version_prerelease($matches);
139     if ($next_version !== false) {
140         return $next_version;
141     }
142     return next_version_stable($matches);
143 }
144
145 /**
146  * Determine the next version given that the current version is stable
147  */
148 function next_version_stable($matches)
149 {
150     $version_parts = explode('.', $matches['version']);
151     $last_version = array_pop($version_parts);
152     $last_version++;
153     $version_parts[] = $last_version;
154
155     return implode('.', $version_parts) . (empty($matches['prerelease']) ? '-dev' : $matches['prerelease']);
156 }
157
158 function has_prerelease($matches)
159 {
160     if (empty($matches['prerelease'])) {
161         return false;
162     }
163
164     return is_numeric(substr($matches['prerelease'], -1));
165 }
166
167 /**
168  * Determine the next version given that the current version has a pre-release
169  * (e.g. '-alpha5').
170  */
171 function next_version_prerelease($version_parts)
172 {
173     if (!preg_match('#(.*?)([0-9]+)$#', $version_parts['prerelease'], $matches)) {
174         return false;
175     }
176     $next = $matches[2] + 1;
177     return $version_parts['version'] . $matches[1] . $next . '+dev';
178 }