|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\LabStation\Trigger; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\LabStation\{ |
|
7
|
|
|
Trigger, |
|
8
|
|
|
Activity, |
|
9
|
|
|
Activity\Type, |
|
10
|
|
|
Exception\DontRelease, |
|
11
|
|
|
}; |
|
12
|
|
|
use Innmind\CLI\{ |
|
13
|
|
|
Environment, |
|
14
|
|
|
Question\Question, |
|
15
|
|
|
Question\ChoiceQuestion, |
|
16
|
|
|
}; |
|
17
|
|
|
use Innmind\Git\{ |
|
18
|
|
|
Git, |
|
19
|
|
|
Message, |
|
20
|
|
|
Exception\DomainException, |
|
21
|
|
|
}; |
|
22
|
|
|
use Innmind\GitRelease\{ |
|
23
|
|
|
Version, |
|
24
|
|
|
Release, |
|
25
|
|
|
LatestVersion, |
|
26
|
|
|
}; |
|
27
|
|
|
use Innmind\Immutable\{ |
|
28
|
|
|
Map, |
|
29
|
|
|
Str, |
|
30
|
|
|
}; |
|
31
|
|
|
|
|
32
|
|
|
final class GitRelease implements Trigger |
|
33
|
|
|
{ |
|
34
|
|
|
private $git; |
|
35
|
|
|
private $release; |
|
36
|
|
|
private $latestVersion; |
|
37
|
|
|
|
|
38
|
20 |
|
public function __construct(Git $git, Release $release, LatestVersion $latestVersion) |
|
39
|
|
|
{ |
|
40
|
20 |
|
$this->git = $git; |
|
41
|
20 |
|
$this->release = $release; |
|
42
|
20 |
|
$this->latestVersion = $latestVersion; |
|
43
|
20 |
|
} |
|
44
|
|
|
|
|
45
|
16 |
|
public function __invoke(Activity $activity, Environment $env): void |
|
46
|
|
|
{ |
|
47
|
16 |
|
if (!$activity->is(Type::gitBranchChanged())) { |
|
48
|
2 |
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
14 |
|
if ($activity->data()['branch'] !== 'master') { |
|
52
|
2 |
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
12 |
|
$repository = $this->git->repository($env->workingDirectory()); |
|
56
|
12 |
|
$version = ($this->latestVersion)($repository); |
|
57
|
|
|
|
|
58
|
12 |
|
$env->output()->write(Str::of("$version\n")); |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
12 |
|
$newVersion = $this->askKind($env, $version); |
|
63
|
4 |
|
} catch (DontRelease $e) { |
|
64
|
4 |
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
8 |
|
$message = $this->askMessage($env); |
|
69
|
2 |
|
} catch (DontRelease $e) { |
|
70
|
2 |
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
6 |
|
($this->release)($repository, $newVersion, $message); |
|
74
|
6 |
|
} |
|
75
|
|
|
|
|
76
|
12 |
|
private function askKind(Environment $env, Version $version): Version |
|
77
|
|
|
{ |
|
78
|
12 |
|
$ask = new ChoiceQuestion( |
|
79
|
12 |
|
'Kind of release:', |
|
80
|
12 |
|
Map::of('scalar', 'scalar') |
|
81
|
12 |
|
(1, 'major') |
|
82
|
12 |
|
(2, 'minor') |
|
83
|
12 |
|
(3, 'bugfix') |
|
84
|
12 |
|
(4, 'none') |
|
85
|
|
|
); |
|
86
|
12 |
|
$response = $ask($env->input(), $env->output()); |
|
87
|
|
|
|
|
88
|
12 |
|
if ($response->empty()) { |
|
89
|
2 |
|
throw new DontRelease; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
10 |
|
switch ($response->current()) { |
|
93
|
10 |
|
case 'major': |
|
94
|
4 |
|
return $version->increaseMajor(); |
|
95
|
|
|
|
|
96
|
6 |
|
case 'minor': |
|
97
|
2 |
|
return $version->increaseMinor(); |
|
98
|
|
|
|
|
99
|
4 |
|
case 'bugfix': |
|
100
|
2 |
|
return $version->increaseBugfix(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
throw new DontRelease; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
8 |
|
private function askMessage(Environment $env): Message |
|
107
|
|
|
{ |
|
108
|
8 |
|
$message = (new Question('message:'))($env->input(), $env->output()); |
|
109
|
|
|
|
|
110
|
|
|
try { |
|
111
|
8 |
|
return new Message((string) $message); |
|
112
|
2 |
|
} catch (DomainException $e) { |
|
113
|
2 |
|
$env->error()->write(Str::of("Invalid message\n")); |
|
114
|
|
|
|
|
115
|
2 |
|
throw new DontRelease; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|