forked from SonsOfPHP/sonsofphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReleaseCommand.php
More file actions
343 lines (293 loc) · 13.6 KB
/
ReleaseCommand.php
File metadata and controls
343 lines (293 loc) · 13.6 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
declare(strict_types=1);
namespace SonsOfPHP\Bard\Console\Command;
use RuntimeException;
use SonsOfPHP\Bard\JsonFile;
use SonsOfPHP\Bard\Operation\Bard\UpdateVersionOperation;
use SonsOfPHP\Bard\Operation\Composer\Package\CopyBranchAliasValueFromRootToPackageOperation;
use SonsOfPHP\Component\Version\Version;
use SonsOfPHP\Component\Version\VersionInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
/**
* Release command will take in user input and call the other release commands.
*
* @author Joshua Estes <joshua@sonsofphp.com>
*/
final class ReleaseCommand extends AbstractCommand
{
private ?VersionInterface $currentVersion = null;
private ?VersionInterface $releaseVersion = null;
private bool $isDryRun = true;
private JsonFile $rootComposerJsonFile;
protected function configure(): void
{
$this
->setName('release')
->setDescription('Create a new release')
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute a dry-run with nothing being updated or released')
->addOption('branch', null, InputOption::VALUE_REQUIRED, 'What branch we working with?', 'main')
->addArgument('release', InputArgument::REQUIRED, 'Next Release you want to start? Use format <major>.<minor>.<patch>-<PreRelease>+<BuildMetadata> or "major", "minor", "patch"')
->setHelp(
<<<'HELP'
This command allows you to create a new release and will update the various
repos that have been configured. The current version can be found in the
`bard.json` file. This will will update the version based on the type of release
that you are doing.
<comment>%command.full_name%</comment>
Read more at https://docs.SonsOfPHP.com
HELP
);
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);
$version = $input->getArgument('release');
if (null === $version) {
return;
}
// Last version that was released
$this->currentVersion = new Version($this->bardConfig->getSection('version'));
if (\in_array($version, ['major', 'minor', 'patch'])) {
switch ($version) {
case 'major':
$this->releaseVersion = $this->currentVersion->nextMajor();
break;
case 'minor':
$this->releaseVersion = $this->currentVersion->nextMinor();
break;
case 'patch':
$this->releaseVersion = $this->currentVersion->nextPatch();
break;
}
} else {
$this->releaseVersion = new Version($version);
}
if ($this->currentVersion->isGreaterThan($this->releaseVersion)) {
throw new RuntimeException('Cannot release a lower version');
}
$this->isDryRun = $input->getOption('dry-run');
$this->rootComposerJsonFile = new JsonFile($input->getOption('working-dir') . '/composer.json');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->bardStyle->title(sprintf(
'Releasing "%s"',
$this->releaseVersion->toString()
));
if ($this->isDryRun) {
$this->bardStyle->note('dry-run enabled no changes will be made');
if (!$this->bardStyle->confirm('[dry-run] Continue?', true)) {
return self::SUCCESS;
}
}
// Make sure we are on the correct branch
$this->bardStyle->text(sprintf(
'Making sure we are on branch "%s"...',
$input->getOption('branch'),
));
if (!$this->isDryRun) {
$process = new Process(['git', 'checkout', $input->getOption('branch')]);
$this->getProcessHelper()->mustRun(
$output,
$process,
sprintf(
'There was and error running command: %s',
$process->getCommandLine()
)
);
}
$this->bardStyle->text('...OK');
if ($this->isDryRun && !$this->bardStyle->confirm('[dry-run] Continue to next step?', true)) {
return self::SUCCESS;
}
// 0. Make sure we have the latest changes
$this->pullLatestChanges($input, $output);
if ($this->isDryRun && !$this->bardStyle->confirm('[dry-run] Continue to next step?', true)) {
return self::SUCCESS;
}
// 1. Update "replace" in main composer.json with the Package Names
// "package/name": "self.version"
$this->bardStyle->section('Merging composer.json files');
$cmdInput = new ArrayInput([
'command' => 'merge',
'--dry-run' => $this->isDryRun,
]);
$this->getApplication()->doRun($cmdInput, $output);
if ($this->isDryRun && !$this->bardStyle->confirm('[dry-run] Continue to next step?', true)) {
return self::SUCCESS;
}
// @todo
// 2. Update Changelog
// Changes the "Unreleased" headline to the version we are releaseing
// Adds new headline at top for unreleased features
// 3. Tag Release and push
$this->tagReleaseAndPushMonorepo($input, $output);
if ($this->isDryRun && !$this->bardStyle->confirm('[dry-run] Continue to next step?', true)) {
return self::SUCCESS;
}
// 4. Subtree Split for each package
$this->tagReleaseAndPushPackages($input, $output);
if ($this->isDryRun && !$this->bardStyle->confirm('[dry-run] Continue to next step?', true)) {
return self::SUCCESS;
}
// 5. Update branch alias in all composer.json files
$this->updateBranchAliasForPackages($input);
if ($this->isDryRun && !$this->bardStyle->confirm('[dry-run] Continue to next step?', true)) {
return self::SUCCESS;
}
// 6. Update bard.json with current version
$this->updateBardConfigVersion();
if ($this->isDryRun && !$this->bardStyle->confirm('[dry-run] Continue to next step?', true)) {
return self::SUCCESS;
}
// 7. Commit and push updates
$this->commitAndPushNewChanges($input, $output);
$this->bardStyle->success(sprintf(
'Version "%s" has been released',
$this->releaseVersion->toString(),
));
if ($this->isDryRun) {
$this->bardStyle->note([
'dry-run was enabled so no files were changed and no code was pushed',
]);
}
return self::SUCCESS;
}
private function pullLatestChanges(InputInterface $input, OutputInterface $output): void
{
$this->bardStyle->text(sprintf(
'Pulling latest changes from "%s"...',
$input->getOption('branch'),
));
if (!$this->isDryRun) {
$process = new Process(['git', 'pull', 'origin', $input->getOption('branch')]);
if ($output->isDebug()) {
$this->bardStyle->text($process->getCommandLine());
}
$this->getProcessHelper()->mustRun($output, $process, sprintf('There was and error running command: %s', $process->getCommandLine()));
}
$this->bardStyle->text('...OK');
}
private function tagReleaseAndPushMonorepo(InputInterface $input, OutputInterface $output): void
{
$this->bardStyle->section(sprintf(
'Releasing Monorepo "%s"',
$this->releaseVersion->toString()
));
$processCommands = [
['git', 'add', '.'],
['git', 'commit', '--allow-empty', '-m', sprintf('"Preparing for Release v%s"', $this->releaseVersion->toString())],
['git', 'push', 'origin', $input->getOption('branch')],
['git', 'tag', 'v' . $this->releaseVersion->toString()],
['git', 'push', 'origin', 'v' . $this->releaseVersion->toString()],
];
foreach ($processCommands as $cmd) {
$process = new Process($cmd);
$this->bardStyle->text($process->getCommandLine());
if (!$this->isDryRun) {
$this->getProcessHelper()->mustRun($output, $process, sprintf('There was and error running command: %s', $process->getCommandLine()));
}
}
$this->bardStyle->success('Monorepo released');
}
private function tagReleaseAndPushPackages(InputInterface $input, OutputInterface $output): void
{
$this->bardStyle->section(sprintf(
'Releasing packages "%s"',
$this->releaseVersion->toString()
));
foreach ($this->bardConfig->getSection('packages') as $pkg) {
$pkgComposerJsonFile = new JsonFile(realpath($input->getOption('working-dir') . '/' . $pkg['path'] . '/composer.json'));
$pkgName = $pkgComposerJsonFile->getSection('name');
$this->bardStyle->text($this->getFormatterHelper()->formatSection($pkgName, 'Releasing...'));
$processCommands = [
['git', 'subtree', 'split', '-P', $pkg['path'], '-b', $pkgName],
['git', 'checkout', $pkgName],
['git', 'push', $pkg['repository'], sprintf('%s:%s', $pkgName, $input->getOption('branch'))],
['git', 'tag', sprintf('%s_%s', $pkgName, $this->releaseVersion->toString())],
['git', 'push', $pkg['repository'], sprintf('%s_%s:v%s', $pkgName, $this->releaseVersion->toString(), $this->releaseVersion->toString())],
['git', 'checkout', $input->getOption('branch')],
['git', 'tag', '-d', sprintf('%s_%s', $pkgName, $this->releaseVersion->toString())],
['git', 'branch', '-D', $pkgName],
];
foreach ($processCommands as $cmd) {
$process = new Process($cmd);
$this->bardStyle->text($process->getCommandLine());
if (!$this->isDryRun) {
$this->getProcessHelper()->mustRun($output, $process, sprintf('There was and error running command: %s', $process->getCommandLine()));
}
}
$this->bardStyle->text($this->getFormatterHelper()->formatSection($pkgName, '...Done'));
$this->bardStyle->newLine();
}
$this->bardStyle->success('All Packages have been Released');
}
/**
* Foreach package, this will update the extra.branch-alias to the same
* value as the root composer.json value
*/
private function updateBranchAliasForPackages(InputInterface $input): void
{
$this->bardStyle->section('Updating the Branch Alias for root and packages');
// Update the branch alias for root composer.json
$extra = $this->rootComposerJsonFile->getSection('extra');
$branchAlias = explode('.', $this->releaseVersion->toString());
$branchAlias[2] = 'x-dev';
$branchAlias = implode('.', $branchAlias);
$extra['branch-alias'] = $branchAlias;
$this->rootComposerJsonFile->setSection('extra', $extra);
$this->bardStyle->text('root composer.json updated to ' . $branchAlias);
if (!$this->isDryRun) {
$this->rootComposerJsonFile->save();
}
// Update all packages branch alias based on the root composer.json
foreach ($this->bardConfig->getSection('packages') as $pkg) {
$pkgComposerJsonFile = new JsonFile(realpath($input->getOption('working-dir') . '/' . $pkg['path'] . '/composer.json'));
$pkgComposerJsonFile = $pkgComposerJsonFile->with(new CopyBranchAliasValueFromRootToPackageOperation($this->rootComposerJsonFile));
$this->bardStyle->text($this->getFormatterHelper()->formatSection($pkgComposerJsonFile->getSection('name'), 'Updated branch alias to "' . $branchAlias . '"'));
if (!$this->isDryRun) {
$pkgComposerJsonFile->save();
}
}
$this->bardStyle->success('Done');
}
private function updateBardConfigVersion(): void
{
$this->bardStyle->text(sprintf(
'Updating version to "%s" in bard config...',
$this->releaseVersion->toString()
));
$this->bardConfig = $this->bardConfig->with(new UpdateVersionOperation($this->releaseVersion));
if (!$this->isDryRun) {
$this->bardConfig->save();
}
$this->bardStyle->text('...OK');
}
private function commitAndPushNewChanges(InputInterface $input, OutputInterface $output): void
{
$this->bardStyle->section('Commiting and new Pushing Changes');
$processCommands = [
['git', 'add', '.'],
['git', 'commit', '-m', '"starting next release"'],
['git', 'push', 'origin', $input->getOption('branch')],
];
foreach ($processCommands as $cmd) {
$process = new Process($cmd);
$this->bardStyle->text($process->getCommandLine());
if (!$this->isDryRun) {
$this->getProcessHelper()->mustRun($output, $process, sprintf('There was and error running command: %s', $process->getCommandLine()));
}
}
$cmdInput = new ArrayInput([
'command' => 'push',
'--dry-run' => $this->isDryRun,
'--branch' => $input->getOption('branch'),
]);
$this->getApplication()->doRun($cmdInput, $output);
}
}