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