|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ChangelogGenerator\Command; |
|
6
|
|
|
|
|
7
|
|
|
use ChangelogGenerator\ChangelogGenerator; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
|
14
|
|
|
use function fopen; |
|
15
|
|
|
use function getcwd; |
|
16
|
|
|
use function sprintf; |
|
17
|
|
|
|
|
18
|
|
|
class GenerateChangelogCommand extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ChangelogGenerator */ |
|
21
|
|
|
private $changelogGenerator; |
|
22
|
|
|
|
|
23
|
6 |
|
public function __construct(ChangelogGenerator $changelogGenerator) |
|
24
|
|
|
{ |
|
25
|
6 |
|
$this->changelogGenerator = $changelogGenerator; |
|
26
|
|
|
|
|
27
|
6 |
|
parent::__construct(); |
|
28
|
6 |
|
} |
|
29
|
|
|
|
|
30
|
6 |
|
protected function configure() : void |
|
31
|
|
|
{ |
|
32
|
|
|
$this |
|
33
|
6 |
|
->setName('generate') |
|
34
|
6 |
|
->setDescription('Generate a changelog markdown document from a GitHub milestone.') |
|
35
|
6 |
|
->setHelp(<<<EOT |
|
36
|
6 |
|
The <info>%command.name%</info> command generates a changelog markdown document from a GitHub milestone: |
|
37
|
|
|
|
|
38
|
|
|
<info>%command.full_name% --user=doctrine --repository=migrations --milestone=2.0</info> |
|
39
|
|
|
EOT |
|
40
|
|
|
) |
|
41
|
6 |
|
->addOption( |
|
42
|
6 |
|
'user', |
|
43
|
6 |
|
null, |
|
44
|
6 |
|
InputOption::VALUE_REQUIRED, |
|
45
|
6 |
|
'User that owns the repository.' |
|
46
|
|
|
) |
|
47
|
6 |
|
->addOption( |
|
48
|
6 |
|
'repository', |
|
49
|
6 |
|
null, |
|
50
|
6 |
|
InputOption::VALUE_REQUIRED, |
|
51
|
6 |
|
'The repository owned by the user.' |
|
52
|
|
|
) |
|
53
|
6 |
|
->addOption( |
|
54
|
6 |
|
'milestone', |
|
55
|
6 |
|
null, |
|
56
|
6 |
|
InputOption::VALUE_REQUIRED, |
|
57
|
6 |
|
'The milestone to build the changelog for.' |
|
58
|
|
|
) |
|
59
|
6 |
|
->addOption( |
|
60
|
6 |
|
'file', |
|
61
|
6 |
|
null, |
|
62
|
6 |
|
InputOption::VALUE_OPTIONAL, |
|
63
|
6 |
|
'Write the changelog to a file.', |
|
64
|
6 |
|
false |
|
65
|
|
|
) |
|
66
|
6 |
|
->addOption( |
|
67
|
6 |
|
'append', |
|
68
|
6 |
|
null, |
|
69
|
6 |
|
InputOption::VALUE_NONE, |
|
70
|
6 |
|
'Append the changelog to the file.' |
|
71
|
|
|
) |
|
72
|
|
|
; |
|
73
|
6 |
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) : void |
|
76
|
|
|
{ |
|
77
|
4 |
|
$user = $input->getOption('user'); |
|
78
|
4 |
|
$repository = $input->getOption('repository'); |
|
79
|
4 |
|
$milestone = $input->getOption('milestone'); |
|
80
|
|
|
|
|
81
|
4 |
|
$changelogOutput = $this->getChangelogOutput($input, $output); |
|
82
|
|
|
|
|
83
|
4 |
|
$this->changelogGenerator->generate($user, $repository, $milestone, $changelogOutput); |
|
84
|
4 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return false|resource |
|
88
|
|
|
*/ |
|
89
|
1 |
|
protected function fopen(string $file, string $mode) |
|
90
|
|
|
{ |
|
91
|
1 |
|
return fopen($file, $mode); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @throws InvalidArgumentException |
|
96
|
|
|
*/ |
|
97
|
2 |
|
protected function createStreamOutput(string $file, bool $append) : StreamOutput |
|
98
|
|
|
{ |
|
99
|
2 |
|
$handle = $this->fopen($file, $this->getFileHandleMode($append)); |
|
100
|
|
|
|
|
101
|
2 |
|
if ($handle === false) { |
|
102
|
1 |
|
throw new InvalidArgumentException(sprintf('Could not open handle for %s', $file)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return new StreamOutput($handle); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
private function getFileHandleMode(bool $append) : string |
|
109
|
|
|
{ |
|
110
|
2 |
|
return $append ? 'a+' : 'w+'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
4 |
|
private function getChangelogOutput(InputInterface $input, OutputInterface $output) : OutputInterface |
|
114
|
|
|
{ |
|
115
|
4 |
|
$file = $input->getOption('file'); |
|
116
|
4 |
|
$append = (bool) $input->getOption('append'); |
|
117
|
|
|
|
|
118
|
4 |
|
$changelogOutput = $output; |
|
119
|
|
|
|
|
120
|
4 |
|
if ($file !== false) { |
|
121
|
3 |
|
$changelogOutput = $this->createStreamOutput($this->getChangelogFilePath($file), $append); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
4 |
|
return $changelogOutput; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
3 |
|
private function getChangelogFilePath(?string $file) : string |
|
128
|
|
|
{ |
|
129
|
3 |
|
return $file === null ? sprintf('%s/CHANGELOG.md', getcwd()) : $file; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|