-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrelease
executable file
·240 lines (207 loc) · 7.11 KB
/
release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env php
<?php
/**
* Release a major, minor or patch update w/ release notes from the CHANGELOG.md file.
*
* Usage:
*
* _build/release.php patch
* _build/release.php minor
* _build/release.php major
*
* Release w/o prompt confirmation:
*
* _build/release.php -q patch
* _build/release.php --no-interactive minor
*
* Release w/o checking for dirty or unpushed changes:
*
* _build/release.php --no-dirty-check minor
* _build/release.php --no-unpushed-check patch
*
* Run a dry-run test:
*
* _build/release.php --dry-run patch
*
* Update the changelog:
*
* _build/release.php --changelog-update patch
*/
namespace lucatume\tools;
$root = dirname(__DIR__);
require_once $root . '/vendor/autoload.php';
$changelogFile = $root . '/CHANGELOG.md';
if (! file_exists($changelogFile)) {
echo "\e[31mChangelog file (CHANGELOG.md, changelog.md) does not exist.\e[0m\n";
exit(1);
}
function args()
{
global $argv;
$options = getopt(
'q',
['not-interactive', 'no-diff-check', 'no-unpushed-check', 'dry-run', 'no-changelog-update'],
$optind
);
$map = [
'releaseType' => isset($argv[$optind]) ? $argv[$optind] : 'patch',
'notInteractive' => isset($options['q']) || isset($options['not-interactive']),
'noChangelogUpdate' => isset($options['no-changelog-update']),
'checkDiff' => empty($options['no-diff-check']),
'checkUnpushed' => empty($options['no-unpushed-check']),
'dryRun' => isset($options['dry-run']),
];
return static function ($key, $default = null) use ($map) {
return isset($map[$key]) ? $map[$key] : $default;
};
}
$args = args();
if (!in_array($args('releaseType'), ['major', 'minor', 'patch'], true)) {
echo "\e[31mThe release type has to be one of major, minor or patch.\e[0m\n";
exit(1);
}
$dryRun = $args('dryRun', false);
if (!$dryRun) {
$currentGitBranch = trim(shell_exec('git rev-parse --abbrev-ref HEAD'));
if ($currentGitBranch !== 'master') {
echo "\e[31mCan release only from master branch.\e[0m\n";
exit(1);
}
echo "Current git branch: \e[32m" . $currentGitBranch . "\e[0m\n";
}
/**
* Parses the changelog to get the latest notes and the latest, released, version.
*
* @param string $changelog The absolute path to the changelog file.
*
* @return array<string,mixed> The map of parsed values.
*/
function changelog($changelog)
{
$notes = '';
$latestVersion = '';
$f = fopen($changelog, 'rb');
$read = false;
while ($line = fgets($f)) {
if (preg_match('/^## \\[unreleased]/', $line)) {
$read = true;
continue;
}
if (preg_match('/^## \\[(?<version>\\d+\\.\\d\.\\d+)]/', $line, $m)) {
$latestVersion = $m['version'];
break;
}
if ($read === true) {
$notes .= $line;
}
}
fclose($f);
return [ 'notes' => trim($notes), 'latestVersion' => $latestVersion ];
}
function updateChangelog($changelogFile, $version, callable $args, $date = null)
{
$date = $date === null ? date('Y-m-d') : $date;
$changelogVersionLine = sprintf("\n\n## [%s] %s;", $version, $date);
$changelogFileName = basename($changelogFile);
$currentContents = file_get_contents($changelogFile);
$entryLine = '## [unreleased] Unreleased';
if (strpos($currentContents, $entryLine) === false) {
$message = 'Unreleased entry line not found; does the changelog file contain an entry like "' . $entryLine . '"?';
echo "\e[31m{$message}\e[0m\n";
exit(1);
}
$changelogContents = str_replace($entryLine, $entryLine . $changelogVersionLine, $currentContents);
$changelogContents = preg_replace_callback(
'/\\[(?:[Uu])nreleased]:\\s+(?<repo>.*)\\/(?<previous_version>\\d+\\.\\d+\\.\\d+)...(HEAD|head)/ium',
static function (array $matches) use ($version) {
return sprintf(
'[%1$s]: %2$s/%3$s...%1$s' . PHP_EOL . '[unreleased]: %2$s/%1$s...HEAD',
$version,
$matches['repo'],
$matches['previous_version']
);
},
$changelogContents
);
echo "Changelog updates:\n\n---\n";
echo substr($changelogContents, 0, 1024);
echo "\n\n[...]\n\n";
echo substr($changelogContents, strlen($changelogContents) - 512);
echo "---\n\n";
if (!$args('dryRun', false)
&& (
$args('notInteractive', false)
|| confirm("Would you like to proceed?")
)
) {
file_put_contents($changelogFile, $changelogContents);
passthru('git commit -m "doc(' . $changelogFileName . '.md) update to version ' . $version . '" -- ' . $changelogFile);
}
}
$changelog = changelog($changelogFile);
$releaseType = $args('releaseType', 'patch');
switch ($releaseType) {
case 'major':
$releaseVersion = preg_replace_callback('/(?<target>\\d+)\\.\\d\.\\d+/', static function ($m) {
return (++$m['target']) . '.0.0';
}, $changelog['latestVersion']);
break;
case 'minor':
$releaseVersion = preg_replace_callback('/(?<major>\\d+)\\.(?<target>\\d)\.\\d+/', static function ($m) {
return $m['major'] . '.' . (++$m['target']) . '.0';
}, $changelog['latestVersion']);
break;
case 'patch':
$releaseVersion = preg_replace_callback(
'/(?<major>\\d+)\\.(?<minor>\\d)\.(?<target>\\d+)/',
static function ($m) {
return $m['major'] . '.' . ($m['minor']) . '.' . (++$m['target']);
},
$changelog['latestVersion']
);
break;
}
$releaseNotesHeader = "{$releaseVersion}\n\n";
$fullReleaseNotes = $releaseNotesHeader . $changelog['notes'];
echo "Latest release: \e[32m" . $changelog['latestVersion'] . "\e[0m\n";
echo "Release type: \e[32m" . $releaseType . "\e[0m\n";
echo "Next release: \e[32m" . $releaseVersion . "\e[0m\n";
echo "Release notes:\n\n---\n" . $fullReleaseNotes . "\n---\n";
echo "\n\n";
if (!$args('noChangelogUpdate', false)) {
updateChangelog($changelogFile, $releaseVersion, $args);
}
if ($args('checkDiff', true) && !$dryRun) {
$gitDirty = trim(shell_exec('git diff HEAD'));
if (!empty($gitDirty)) {
echo "\e[31mYou have uncommited work.\e[0m\n";
exit(1);
}
}
function confirm($question)
{
$question = "\n{$question} ";
return preg_match('/y/i', readline($question));
}
if ($args('checkUnpushed', true) && !$dryRun) {
$gitDiff = trim(shell_exec('git log origin/master..HEAD'));
if (!empty($gitDiff)) {
echo "\e[31mYou have unpushed changes.\e[0m\n";
if (confirm('Would you like to push them now?')) {
passthru('git push');
} else {
exit(1);
}
}
}
file_put_contents($root . '/.rel', $fullReleaseNotes);
$releaseCommand = 'gh release create -F .rel ' . $releaseVersion;
echo "Releasing with command: \e[32m" . $releaseCommand . "\e[0m\n\n";
if ($dryRun || $args('notInteractive', false) || confirm('Do you want to proceed?')) {
if (!$dryRun) {
passthru($releaseCommand);
}
} else {
echo "Canceling\n";
}
unlink($root . '/.rel');