1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement; |
6
|
|
|
|
7
|
|
|
use Vasoft\VersionIncrement\Commits\CommitCollection; |
8
|
|
|
use Vasoft\VersionIncrement\Commits\Section; |
9
|
|
|
use Vasoft\VersionIncrement\Contract\ChangelogFormatterInterface; |
10
|
|
|
use Vasoft\VersionIncrement\SectionRules\DefaultRule; |
11
|
|
|
use Vasoft\VersionIncrement\Contract\SectionRuleInterface; |
12
|
|
|
|
13
|
|
|
final class Config |
14
|
|
|
{ |
15
|
|
|
private string $squashedCommitMessage = 'Squashed commit of the following:'; |
16
|
|
|
private bool $processDefaultSquashedCommit = false; |
17
|
|
|
private array $minorTypes = [ |
18
|
|
|
'feat', |
19
|
|
|
]; |
20
|
|
|
private array $majorTypes = []; |
21
|
|
|
private array $sectionRules = []; |
22
|
|
|
public const DEFAULT_SECTION = 'other'; |
23
|
|
|
|
24
|
|
|
private int $defaultOrder = 500; |
25
|
|
|
private bool $sored = true; |
26
|
|
|
private string $masterBranch = 'master'; |
27
|
|
|
private string $releaseSection = 'chore'; |
28
|
|
|
|
29
|
|
|
private string $releaseScope = 'release'; |
30
|
|
|
|
31
|
|
|
private string $aggregateSection = ''; |
32
|
|
|
|
33
|
|
|
private ?ChangelogFormatterInterface $changelogFormatter = null; |
34
|
|
|
|
35
|
|
|
private bool $ignoreUntrackedFiles = false; |
36
|
|
|
private array $sections = [ |
37
|
|
|
'feat' => [ |
38
|
|
|
'title' => 'New features', |
39
|
|
|
'order' => 10, |
40
|
|
|
'hidden' => false, |
41
|
|
|
], |
42
|
|
|
'fix' => [ |
43
|
|
|
'title' => 'Fixes', |
44
|
|
|
'order' => 20, |
45
|
|
|
'hidden' => false, |
46
|
|
|
], |
47
|
|
|
'chore' => [ |
48
|
|
|
'title' => 'Other changes', |
49
|
|
|
'order' => 30, |
50
|
|
|
'hidden' => false, |
51
|
|
|
], |
52
|
|
|
'docs' => [ |
53
|
|
|
'title' => 'Documentation', |
54
|
|
|
'order' => 40, |
55
|
|
|
'hidden' => false, |
56
|
|
|
], |
57
|
|
|
'style' => [ |
58
|
|
|
'title' => 'Styling', |
59
|
|
|
'order' => 50, |
60
|
|
|
'hidden' => false, |
61
|
|
|
], |
62
|
|
|
'refactor' => [ |
63
|
|
|
'title' => 'Refactoring', |
64
|
|
|
'order' => 60, |
65
|
|
|
'hidden' => false, |
66
|
|
|
], |
67
|
|
|
'test' => [ |
68
|
|
|
'title' => 'Tests', |
69
|
|
|
'order' => 70, |
70
|
|
|
'hidden' => false, |
71
|
|
|
], |
72
|
|
|
'perf' => [ |
73
|
|
|
'title' => 'Performance', |
74
|
|
|
'order' => 80, |
75
|
|
|
'hidden' => false, |
76
|
|
|
], |
77
|
|
|
'ci' => [ |
78
|
|
|
'title' => 'Configure CI', |
79
|
|
|
'order' => 90, |
80
|
|
|
'hidden' => false, |
81
|
|
|
], |
82
|
|
|
'build' => [ |
83
|
|
|
'title' => 'Change build system', |
84
|
|
|
'order' => 100, |
85
|
|
|
'hidden' => false, |
86
|
|
|
], |
87
|
|
|
'other' => [ |
88
|
|
|
'title' => 'Other', |
89
|
|
|
'order' => 110, |
90
|
|
|
'hidden' => false, |
91
|
|
|
], |
92
|
|
|
]; |
93
|
|
|
|
94
|
1 |
|
public function setSections(array $sections): self |
95
|
|
|
{ |
96
|
1 |
|
$this->sored = false; |
97
|
1 |
|
$this->sections = []; |
98
|
1 |
|
foreach ($sections as $key => $value) { |
99
|
1 |
|
$this->sections[$key] = [ |
100
|
1 |
|
'title' => $value['title'] ?? $key, |
101
|
1 |
|
'order' => $value['order'] ?? ++$this->defaultOrder, |
102
|
1 |
|
'hidden' => $value['hidden'] ?? false, |
103
|
1 |
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
public function setSection( |
110
|
|
|
string $key, |
111
|
|
|
string $title, |
112
|
|
|
int $order = -1, |
113
|
|
|
?bool $hidden = null, |
114
|
|
|
): self { |
115
|
3 |
|
$this->sored = false; |
116
|
3 |
|
$exists = $this->sections[$key] ?? null; |
117
|
3 |
|
$this->sections[$key] = [ |
118
|
3 |
|
'title' => $title, |
119
|
3 |
|
'order' => -1 === $order ? ($exists ? $exists['order'] : ++$this->defaultOrder) : $order, |
120
|
3 |
|
'hidden' => null !== $hidden ? $hidden : ($exists ? $exists['hidden'] : false), |
121
|
3 |
|
]; |
122
|
|
|
|
123
|
3 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
public function getSectionIndex(): array |
127
|
|
|
{ |
128
|
3 |
|
$this->sortSections(); |
129
|
|
|
|
130
|
3 |
|
return array_fill_keys(array_keys($this->sections), []); |
131
|
|
|
} |
132
|
|
|
|
133
|
15 |
|
public function getCommitCollection(): CommitCollection |
134
|
|
|
{ |
135
|
15 |
|
$this->sortSections(); |
136
|
15 |
|
$sections = []; |
137
|
15 |
|
$default = null; |
138
|
15 |
|
foreach ($this->sections as $key => $section) { |
139
|
15 |
|
$section = new Section( |
140
|
15 |
|
$key, |
141
|
15 |
|
$section['title'], |
142
|
15 |
|
$section['hidden'], |
143
|
15 |
|
$this->getSectionRules($key), |
144
|
15 |
|
in_array($key, $this->majorTypes, true), |
145
|
15 |
|
in_array($key, $this->minorTypes, true), |
146
|
15 |
|
); |
147
|
15 |
|
if (self::DEFAULT_SECTION === $key) { |
148
|
15 |
|
$default = $section; |
149
|
|
|
} |
150
|
15 |
|
$sections[$key] = $section; |
151
|
|
|
} |
152
|
|
|
|
153
|
15 |
|
return new CommitCollection($sections, $default); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
20 |
|
private function sortSections(): void |
157
|
|
|
{ |
158
|
20 |
|
if (!$this->sored) { |
159
|
4 |
|
$this->checkDefaultSection(); |
160
|
4 |
|
uasort($this->sections, static fn($a, $b) => $a['order'] <=> $b['order']); |
161
|
4 |
|
$this->sored = true; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
public function getSectionDescriptions(): array |
166
|
|
|
{ |
167
|
2 |
|
$this->sortSections(); |
168
|
2 |
|
$result = []; |
169
|
2 |
|
foreach ($this->sections as $key => $section) { |
170
|
2 |
|
$result[] = sprintf('%s - %s', $key, $section['title']); |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
return $result; |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
public function getSectionTitle(string $key): string |
177
|
|
|
{ |
178
|
2 |
|
return $this->sections[$key]['title'] ?? $key; |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
public function isSectionHidden(string $key): bool |
182
|
|
|
{ |
183
|
2 |
|
return $this->sections[$key]['hidden'] ?? false; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
public function setReleaseSection(string $section): self |
187
|
|
|
{ |
188
|
1 |
|
$this->releaseSection = $section; |
189
|
|
|
|
190
|
1 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
15 |
|
public function getReleaseSection(): string |
194
|
|
|
{ |
195
|
15 |
|
return isset($this->sections[$this->releaseSection]) ? $this->releaseSection : self::DEFAULT_SECTION; |
196
|
|
|
} |
197
|
|
|
|
198
|
4 |
|
private function checkDefaultSection(): void |
199
|
|
|
{ |
200
|
4 |
|
if (!isset($this->sections[self::DEFAULT_SECTION])) { |
201
|
1 |
|
$maxOrder = 0; |
202
|
1 |
|
foreach ($this->sections as $section) { |
203
|
1 |
|
$maxOrder = max($maxOrder, $section['order']); |
204
|
|
|
} |
205
|
1 |
|
$this->sections[self::DEFAULT_SECTION] = [ |
206
|
1 |
|
'title' => 'Other', |
207
|
1 |
|
'order' => $maxOrder + 1000, |
208
|
1 |
|
'hidden' => false, |
209
|
1 |
|
]; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
19 |
|
public function getMasterBranch(): string |
214
|
|
|
{ |
215
|
19 |
|
return $this->masterBranch; |
216
|
|
|
} |
217
|
|
|
|
218
|
3 |
|
public function setMasterBranch(string $masterBranch): self |
219
|
|
|
{ |
220
|
3 |
|
$this->masterBranch = $masterBranch; |
221
|
|
|
|
222
|
3 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
1 |
|
public function setMinorTypes(array $minorTypes): self |
226
|
|
|
{ |
227
|
1 |
|
$this->minorTypes = $minorTypes; |
228
|
|
|
|
229
|
1 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
public function setMajorTypes(array $majorTypes): self |
233
|
|
|
{ |
234
|
1 |
|
$this->majorTypes = $majorTypes; |
235
|
|
|
|
236
|
1 |
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
2 |
|
public function setReleaseScope(string $releaseScope): self |
240
|
|
|
{ |
241
|
2 |
|
$this->releaseScope = $releaseScope; |
242
|
|
|
|
243
|
2 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
14 |
|
public function getReleaseScope(): string |
247
|
|
|
{ |
248
|
14 |
|
return $this->releaseScope; |
249
|
|
|
} |
250
|
|
|
|
251
|
2 |
|
public function setIgnoreUntrackedFiles(bool $ignoreUntrackedFiles): self |
252
|
|
|
{ |
253
|
2 |
|
$this->ignoreUntrackedFiles = $ignoreUntrackedFiles; |
254
|
|
|
|
255
|
2 |
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
18 |
|
public function mastIgnoreUntrackedFiles(): bool |
259
|
|
|
{ |
260
|
18 |
|
return $this->ignoreUntrackedFiles; |
261
|
|
|
} |
262
|
|
|
|
263
|
2 |
|
public function addSectionRule(string $key, SectionRuleInterface $rule): self |
264
|
|
|
{ |
265
|
2 |
|
$this->sectionRules[$key][] = $rule; |
266
|
|
|
|
267
|
2 |
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return SectionRuleInterface[] |
272
|
|
|
*/ |
273
|
15 |
|
public function getSectionRules(string $key): array |
274
|
|
|
{ |
275
|
15 |
|
$this->sectionRules[$key]['default'] = new DefaultRule($key); |
276
|
|
|
|
277
|
15 |
|
return $this->sectionRules[$key]; |
278
|
|
|
} |
279
|
|
|
|
280
|
1 |
|
public function setAggregateSection(string $aggregateSection): self |
281
|
|
|
{ |
282
|
1 |
|
$this->aggregateSection = $aggregateSection; |
283
|
|
|
|
284
|
1 |
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
15 |
|
public function getAggregateSection(): string |
288
|
|
|
{ |
289
|
15 |
|
return $this->aggregateSection; |
290
|
|
|
} |
291
|
|
|
|
292
|
1 |
|
public function setSquashedCommitMessage(string $squashedCommitMessage): self |
293
|
|
|
{ |
294
|
1 |
|
$this->squashedCommitMessage = $squashedCommitMessage; |
295
|
|
|
|
296
|
1 |
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
15 |
|
public function getSquashedCommitMessage(): string |
300
|
|
|
{ |
301
|
15 |
|
return $this->squashedCommitMessage; |
302
|
|
|
} |
303
|
|
|
|
304
|
2 |
|
public function setProcessDefaultSquashedCommit(bool $processDefaultSquashedCommit): self |
305
|
|
|
{ |
306
|
2 |
|
$this->processDefaultSquashedCommit = $processDefaultSquashedCommit; |
307
|
|
|
|
308
|
2 |
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
15 |
|
public function shouldProcessDefaultSquashedCommit(): bool |
312
|
|
|
{ |
313
|
15 |
|
return $this->processDefaultSquashedCommit; |
314
|
|
|
} |
315
|
|
|
|
316
|
15 |
|
public function getChangelogFormatter(): ChangelogFormatterInterface |
317
|
|
|
{ |
318
|
15 |
|
if (!$this->changelogFormatter) { |
319
|
14 |
|
$this->changelogFormatter = new Changelog\DefaultFormatter(); |
320
|
|
|
} |
321
|
|
|
|
322
|
15 |
|
return $this->changelogFormatter; |
323
|
|
|
} |
324
|
|
|
|
325
|
1 |
|
public function setChangelogFormatter(ChangelogFormatterInterface $changelogFormatter): void |
326
|
|
|
{ |
327
|
1 |
|
$this->changelogFormatter = $changelogFormatter; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|