|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ChangelogGenerator\Command; |
|
6
|
|
|
|
|
7
|
|
|
use ChangelogGenerator\ChangelogConfig; |
|
8
|
|
|
use ChangelogGenerator\ChangelogGenerator; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
use Symfony\Component\Console\Command\Command; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
14
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
|
17
|
|
|
|
|
18
|
|
|
use function assert; |
|
19
|
|
|
use function count; |
|
20
|
|
|
use function current; |
|
21
|
|
|
use function file_exists; |
|
22
|
|
|
use function file_get_contents; |
|
23
|
|
|
use function file_put_contents; |
|
24
|
|
|
use function fopen; |
|
25
|
|
|
use function getcwd; |
|
26
|
|
|
use function gettype; |
|
27
|
|
|
use function in_array; |
|
28
|
|
|
use function is_array; |
|
29
|
|
|
use function is_string; |
|
30
|
|
|
use function sprintf; |
|
31
|
|
|
use function touch; |
|
32
|
|
|
|
|
33
|
|
|
class GenerateChangelogCommand extends Command |
|
34
|
|
|
{ |
|
35
|
|
|
public const WRITE_STRATEGY_REPLACE = 'replace'; |
|
36
|
|
|
public const WRITE_STRATEGY_APPEND = 'append'; |
|
37
|
|
|
public const WRITE_STRATEGY_PREPEND = 'prepend'; |
|
38
|
|
|
|
|
39
|
|
|
private ChangelogGenerator $changelogGenerator; |
|
40
|
|
|
|
|
41
|
21 |
|
public function __construct(ChangelogGenerator $changelogGenerator) |
|
42
|
|
|
{ |
|
43
|
21 |
|
$this->changelogGenerator = $changelogGenerator; |
|
44
|
|
|
|
|
45
|
21 |
|
parent::__construct(); |
|
46
|
21 |
|
} |
|
47
|
|
|
|
|
48
|
21 |
|
protected function configure(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this |
|
51
|
21 |
|
->setName('generate') |
|
52
|
21 |
|
->setDescription('Generate a changelog markdown document from a GitHub milestone.') |
|
53
|
21 |
|
->setHelp(<<<EOT |
|
54
|
21 |
|
The <info>%command.name%</info> command generates a changelog markdown document from a GitHub milestone: |
|
55
|
|
|
|
|
56
|
|
|
<info>%command.full_name% --user=doctrine --repository=migrations --milestone=2.0</info> |
|
57
|
|
|
|
|
58
|
|
|
You can filter the changelog by label names using the --label option: |
|
59
|
|
|
|
|
60
|
|
|
<info>%command.full_name% --user=doctrine --repository=migrations --milestone=2.0 --label=Enhancement --label=Bug</info> |
|
61
|
|
|
EOT |
|
62
|
|
|
) |
|
63
|
21 |
|
->addOption( |
|
64
|
21 |
|
'user', |
|
65
|
21 |
|
null, |
|
66
|
21 |
|
InputOption::VALUE_REQUIRED, |
|
67
|
21 |
|
'User that owns the repository.' |
|
68
|
|
|
) |
|
69
|
21 |
|
->addOption( |
|
70
|
21 |
|
'repository', |
|
71
|
21 |
|
null, |
|
72
|
21 |
|
InputOption::VALUE_REQUIRED, |
|
73
|
21 |
|
'The repository owned by the user.' |
|
74
|
|
|
) |
|
75
|
21 |
|
->addOption( |
|
76
|
21 |
|
'milestone', |
|
77
|
21 |
|
null, |
|
78
|
21 |
|
InputOption::VALUE_REQUIRED, |
|
79
|
21 |
|
'The milestone to build the changelog for.' |
|
80
|
|
|
) |
|
81
|
21 |
|
->addOption( |
|
82
|
21 |
|
'file', |
|
83
|
21 |
|
null, |
|
84
|
21 |
|
InputOption::VALUE_OPTIONAL, |
|
85
|
21 |
|
'Write the changelog to a file.', |
|
86
|
21 |
|
false |
|
87
|
|
|
) |
|
88
|
21 |
|
->addOption( |
|
89
|
21 |
|
'append', |
|
90
|
21 |
|
null, |
|
91
|
21 |
|
InputOption::VALUE_NONE, |
|
92
|
21 |
|
'Append the changelog to the file.' |
|
93
|
|
|
) |
|
94
|
21 |
|
->addOption( |
|
95
|
21 |
|
'prepend', |
|
96
|
21 |
|
null, |
|
97
|
21 |
|
InputOption::VALUE_NONE, |
|
98
|
21 |
|
'Prepend the changelog to the file.' |
|
99
|
|
|
) |
|
100
|
21 |
|
->addOption( |
|
101
|
21 |
|
'label', |
|
102
|
21 |
|
null, |
|
103
|
21 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
104
|
21 |
|
'The labels to generate a changelog for.' |
|
105
|
|
|
) |
|
106
|
21 |
|
->addOption( |
|
107
|
21 |
|
'config', |
|
108
|
21 |
|
'c', |
|
109
|
21 |
|
InputOption::VALUE_REQUIRED, |
|
110
|
21 |
|
'The path to a configuration file.' |
|
111
|
|
|
) |
|
112
|
21 |
|
->addOption( |
|
113
|
21 |
|
'project', |
|
114
|
21 |
|
'p', |
|
115
|
21 |
|
InputOption::VALUE_REQUIRED, |
|
116
|
21 |
|
'The project from the configuration to generate a changelog for.' |
|
117
|
|
|
) |
|
118
|
21 |
|
->addOption( |
|
119
|
21 |
|
'include-open', |
|
120
|
21 |
|
'a', |
|
121
|
21 |
|
InputOption::VALUE_OPTIONAL, |
|
122
|
21 |
|
'Whether to also include open issues.', |
|
123
|
21 |
|
'' |
|
124
|
|
|
) |
|
125
|
21 |
|
->addOption( |
|
126
|
21 |
|
'show-contributors', |
|
127
|
21 |
|
'', |
|
128
|
21 |
|
InputOption::VALUE_OPTIONAL, |
|
129
|
21 |
|
'Whether to include a section with a list of contributors.', |
|
130
|
21 |
|
'' |
|
131
|
|
|
); |
|
132
|
21 |
|
} |
|
133
|
|
|
|
|
134
|
19 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
135
|
|
|
{ |
|
136
|
19 |
|
$changelogConfig = $this->getChangelogConfig($input); |
|
137
|
|
|
|
|
138
|
16 |
|
if (! $changelogConfig->isValid()) { |
|
139
|
1 |
|
throw new InvalidArgumentException('You must pass a config file with the --config option or manually specify the --user --repository and --milestone options.'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
15 |
|
$changelogOutput = $this->getChangelogOutput($input, $output); |
|
143
|
|
|
|
|
144
|
15 |
|
$this->changelogGenerator->generate( |
|
145
|
15 |
|
$changelogConfig, |
|
146
|
|
|
$changelogOutput |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
15 |
|
if ($this->getFileWriteStrategy($input) !== self::WRITE_STRATEGY_PREPEND) { |
|
150
|
12 |
|
return 0; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
3 |
|
$file = $input->getOption('file'); |
|
154
|
|
|
|
|
155
|
3 |
|
if ($file === null) { |
|
156
|
|
|
$file = $this->getChangelogFilePath(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
3 |
|
if (! ($changelogOutput instanceof BufferedOutput)) { |
|
160
|
1 |
|
return 0; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
assert(is_string($file)); |
|
164
|
|
|
|
|
165
|
2 |
|
if (! file_exists($file)) { |
|
166
|
1 |
|
touch($file); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
2 |
|
file_put_contents($file, $changelogOutput->fetch() . file_get_contents($file)); |
|
170
|
|
|
|
|
171
|
2 |
|
return 0; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
19 |
|
private function getChangelogConfig(InputInterface $input): ChangelogConfig |
|
175
|
|
|
{ |
|
176
|
19 |
|
$changelogConfig = $this->loadConfigFile($input); |
|
177
|
|
|
|
|
178
|
16 |
|
if ($changelogConfig !== null) { |
|
179
|
6 |
|
return $changelogConfig; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
10 |
|
$changelogConfig = (new ChangelogConfig()); |
|
183
|
|
|
|
|
184
|
10 |
|
$this->loadChangelogConfigFromInput($input, $changelogConfig); |
|
185
|
|
|
|
|
186
|
10 |
|
return $changelogConfig; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @throws RuntimeException |
|
191
|
|
|
*/ |
|
192
|
15 |
|
private function getStringOption(InputInterface $input, string $name): string |
|
193
|
|
|
{ |
|
194
|
15 |
|
$value = $input->getOption($name); |
|
195
|
|
|
|
|
196
|
15 |
|
if ($value === null) { |
|
197
|
|
|
return ''; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
15 |
|
if (is_string($value)) { |
|
201
|
15 |
|
return $value; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
throw new RuntimeException(sprintf('Invalid option value type: %s', gettype($value))); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
4 |
|
private function getBooleanOption(InputInterface $input, string $name): bool |
|
208
|
|
|
{ |
|
209
|
4 |
|
$value = $input->getOption($name); |
|
210
|
|
|
|
|
211
|
|
|
// option not provided, default to false |
|
212
|
4 |
|
if ($value === '') { |
|
213
|
|
|
return false; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
// option provided, but no value was given, default to true |
|
217
|
4 |
|
if ($value === null) { |
|
218
|
2 |
|
return true; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
// option provided and value was provided |
|
222
|
2 |
|
return is_string($value) && in_array($value, ['1', 'true'], true); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @return string[] |
|
227
|
|
|
*/ |
|
228
|
6 |
|
private function getArrayOption(InputInterface $input, string $name): array |
|
229
|
|
|
{ |
|
230
|
|
|
/** @var string[] $value */ |
|
231
|
6 |
|
$value = $input->getOption($name); |
|
232
|
|
|
|
|
233
|
6 |
|
return $value; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return false|resource |
|
238
|
|
|
*/ |
|
239
|
1 |
|
protected function fopen(string $file, string $mode) |
|
240
|
|
|
{ |
|
241
|
1 |
|
return fopen($file, $mode); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @throws InvalidArgumentException |
|
246
|
|
|
*/ |
|
247
|
2 |
|
protected function createOutput(string $file, string $fileWriteStrategy): OutputInterface |
|
248
|
|
|
{ |
|
249
|
2 |
|
if ($fileWriteStrategy === self::WRITE_STRATEGY_PREPEND) { |
|
250
|
1 |
|
return new BufferedOutput(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
2 |
|
$handle = $this->fopen($file, $this->getFileHandleMode($fileWriteStrategy)); |
|
254
|
|
|
|
|
255
|
2 |
|
if ($handle === false) { |
|
256
|
1 |
|
throw new InvalidArgumentException(sprintf('Could not open handle for %s', $file)); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
1 |
|
return new StreamOutput($handle); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
2 |
|
private function getFileHandleMode(string $fileWriteStrategy): string |
|
263
|
|
|
{ |
|
264
|
2 |
|
if ($fileWriteStrategy === self::WRITE_STRATEGY_APPEND) { |
|
265
|
2 |
|
return 'a+'; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
1 |
|
return 'w+'; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
15 |
|
private function getChangelogOutput(InputInterface $input, OutputInterface $output): OutputInterface |
|
272
|
|
|
{ |
|
273
|
15 |
|
$file = $input->getOption('file'); |
|
274
|
15 |
|
$fileWriteStrategy = $this->getFileWriteStrategy($input); |
|
275
|
|
|
|
|
276
|
15 |
|
$changelogOutput = $output; |
|
277
|
|
|
|
|
278
|
15 |
|
if ($file !== false) { |
|
279
|
6 |
|
if (is_string($file)) { |
|
280
|
5 |
|
$changelogOutput = $this->createOutput($file, $fileWriteStrategy); |
|
281
|
1 |
|
} elseif ($file === null) { |
|
282
|
1 |
|
$changelogOutput = $this->createOutput($this->getChangelogFilePath(), $fileWriteStrategy); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
15 |
|
return $changelogOutput; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
15 |
|
private function getFileWriteStrategy(InputInterface $input): string |
|
290
|
|
|
{ |
|
291
|
15 |
|
$append = (bool) $input->getOption('append'); |
|
292
|
15 |
|
$prepend = (bool) $input->getOption('prepend'); |
|
293
|
|
|
|
|
294
|
15 |
|
if ($append) { |
|
295
|
1 |
|
return self::WRITE_STRATEGY_APPEND; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
14 |
|
if ($prepend) { |
|
299
|
3 |
|
return self::WRITE_STRATEGY_PREPEND; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
11 |
|
return self::WRITE_STRATEGY_REPLACE; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
1 |
|
private function getChangelogFilePath(): string |
|
306
|
|
|
{ |
|
307
|
1 |
|
return sprintf('%s/CHANGELOG.md', getcwd()); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @throws InvalidArgumentException |
|
312
|
|
|
*/ |
|
313
|
19 |
|
private function loadConfigFile(InputInterface $input): ?ChangelogConfig |
|
314
|
|
|
{ |
|
315
|
19 |
|
$config = $input->getOption('config'); |
|
316
|
|
|
|
|
317
|
19 |
|
if ($config === null) { |
|
318
|
10 |
|
$config = 'changelog-generator-config.php'; |
|
319
|
|
|
|
|
320
|
10 |
|
if (! file_exists($config)) { |
|
321
|
10 |
|
return null; |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
assert(is_string($config)); |
|
326
|
|
|
|
|
327
|
9 |
|
if (! file_exists($config)) { |
|
328
|
1 |
|
throw new InvalidArgumentException(sprintf('Configuration file "%s" does not exist.', $config)); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
8 |
|
$configReturn = include $config; |
|
332
|
|
|
|
|
333
|
8 |
|
if (! is_array($configReturn) || count($configReturn) === 0) { |
|
334
|
1 |
|
throw new InvalidArgumentException(sprintf('Configuration file "%s" did not return anything.', $config)); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** @var ChangelogConfig[] $changelogConfigs */ |
|
338
|
7 |
|
$changelogConfigs = $configReturn; |
|
339
|
|
|
|
|
340
|
7 |
|
$changelogConfig = $this->findChangelogConfig($input, $changelogConfigs); |
|
341
|
|
|
|
|
342
|
6 |
|
$this->loadChangelogConfigFromInput($input, $changelogConfig); |
|
343
|
|
|
|
|
344
|
6 |
|
return $changelogConfig; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param ChangelogConfig[] $changelogConfigs |
|
349
|
|
|
*/ |
|
350
|
7 |
|
private function findChangelogConfig(InputInterface $input, array $changelogConfigs): ChangelogConfig |
|
351
|
|
|
{ |
|
352
|
7 |
|
$project = $input->getOption('project'); |
|
353
|
|
|
|
|
354
|
7 |
|
$changelogConfig = current($changelogConfigs); |
|
355
|
|
|
|
|
356
|
|
|
assert($changelogConfig !== false); |
|
357
|
|
|
|
|
358
|
7 |
|
if ($project !== null) { |
|
359
|
|
|
assert(is_string($project)); |
|
360
|
|
|
|
|
361
|
7 |
|
if (! isset($changelogConfigs[$project])) { |
|
362
|
1 |
|
throw new InvalidArgumentException(sprintf('Could not find project named "%s" configured', $project)); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
6 |
|
$changelogConfig = $changelogConfigs[$project]; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
6 |
|
return $changelogConfig; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
16 |
|
private function loadChangelogConfigFromInput(InputInterface $input, ChangelogConfig $changelogConfig): void |
|
372
|
|
|
{ |
|
373
|
16 |
|
if ($input->getOption('user') !== null) { |
|
374
|
15 |
|
$changelogConfig->setUser($this->getStringOption($input, 'user')); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
16 |
|
if ($input->getOption('repository') !== null) { |
|
378
|
15 |
|
$changelogConfig->setRepository($this->getStringOption($input, 'repository')); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
16 |
|
if ($input->getOption('milestone') !== null) { |
|
382
|
15 |
|
$changelogConfig->setMilestone($this->getStringOption($input, 'milestone')); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
16 |
|
if ($input->getOption('label') !== []) { |
|
386
|
6 |
|
$changelogConfig->setLabels($this->getArrayOption($input, 'label')); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
16 |
|
if ($input->getOption('include-open') !== '') { |
|
390
|
3 |
|
$changelogConfig->setIncludeOpen($this->getBooleanOption($input, 'include-open')); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
16 |
|
if ($input->getOption('show-contributors') === '') { |
|
394
|
15 |
|
return; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
1 |
|
$changelogConfig->setShowContributors($this->getBooleanOption($input, 'show-contributors')); |
|
398
|
1 |
|
} |
|
399
|
|
|
} |
|
400
|
|
|
|