/
Release.php
185 lines (153 loc) · 5.42 KB
/
Release.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace A17\Twill\Commands;
use A17\Twill\TwillServiceProvider;
use Illuminate\Support\Facades\Artisan;
class Release extends Command
{
protected $signature = 'twill:release {version}';
protected $description = 'Create a new twill version tag';
public function handle()
{
$version = $this->argument('version');
$this->versionIsGreaterThanLatestVersion($version);
$this->checkChangelogContainsVersion($version);
$this->isOnMainBranch();
$this->isNotBehind();
$this->checkTwillServiceProviderVersion($version);
$this->checkPackageJsonVersionMatches($version);
$this->line('Building assets.');
Artisan::call('twill:build');
// Copy from dist to twill-assets.
$this->executeInTwillDir('rm -Rf twill-assets/assets && cp -Rf dist/assets twill-assets/assets');
$this->line('Force add the assets.');
$this->executeInTwillDir('git add twill-assets');
$this->line('Making new commit with assets');
$this->executeInTwillDir('git commit -m "Updating assets for release ' . $version . '"');
$this->line('Pushing last commit');
$this->executeInTwillDir('git push');
$this->line('Making the tag');
$this->executeInTwillDir("git tag $version");
$this->line('Pushing the tag');
$this->executeInTwillDir("git push origin --tags $version");
$this->line('Done!');
}
/**
* @return void
*/
private function isNotBehind()
{
$currentBranch = $this->executeInTwillDir('git rev-parse --abbrev-ref HEAD');
if (!empty($this->executeInTwillDir('git diff origin/' . $currentBranch))) {
$this->error(
'It looks like your current branch is not clean, please git pull/commit/stash the latest changes before making a release.'
);
exit(1);
}
}
/**
* @return void
*/
private function isOnMainBranch()
{
$currentBranch = $this->executeInTwillDir('git rev-parse --abbrev-ref HEAD');
if (!in_array(trim($currentBranch), ['2.x', '3.x'])) {
$this->error('Current working branch must be a releasable branch (2.x or 3.x)');
exit(1);
}
}
/**
* @param string $version
* @return void
*/
private function checkChangelogContainsVersion($version) {
if (!str_contains(file_get_contents($this->getTwillDir('CHANGELOG.md')), "## $version")) {
$this->error('The changelog is currently missing the version you are trying to tag.');
exit(1);
}
}
/**
* @param string $command
* @return false|string|null
*/
private function executeInTwillDir($command)
{
$twillDir = $this->getTwillDir();
return shell_exec("cd $twillDir && $command");
}
/**
* @param string $version
* @return void
*/
private function versionIsGreaterThanLatestVersion($version)
{
$splittedVersion = explode('.', $version);
if (count($splittedVersion) < 3) {
$this->error('Version number must be of the following format: 1.2.3 (Major, Minor, Patch)');
exit(1);
}
$tags = collect(explode(PHP_EOL, $this->executeInTwillDir('git tag --list')));
$tags = $tags->reject(function ($tag) {
return empty($tag) || str_contains($tag, '-') || count(explode('.', $tag)) < 3;
});
$tags = $tags->sort(function ($tag1, $tag2) {
return $this->newVersionIsGreaterThanOld($tag1, $tag2);
});
if ($this->newVersionIsGreaterThanOld($version, $tags->last())) {
$this->line('New version is greater than old version, continuing release.');
return;
}
$this->error('New version is lower than te last tagged one: ' . $tags->last());
exit(1);
}
/**
* @param string $newVersion
* @param string $oldVersion
* @return bool
*/
private function newVersionIsGreaterThanOld($newVersion, $oldVersion)
{
[$major1, $minor1, $patch1] = explode('.', $newVersion);
[$major2, $minor2, $patch2] = explode('.', $oldVersion);
if ($major1 === $major2) {
if ($minor1 === $minor2) {
return $patch1 > $patch2;
}
return $minor1 > $minor2;
}
return $major1 > $major2;
}
/**
* @param string $version
* @return void
*/
private function checkPackageJsonVersionMatches($version)
{
$array = json_decode(file_get_contents($this->getTwillDir('package.json')));
if ($array->version !== $version) {
$this->error("The package.json version ({$array->version}) does not match that of the release ($version)");
exit(1);
}
}
/**
* @param $version
* @return void
*/
private function checkTwillServiceProviderVersion($version)
{
if (TwillServiceProvider::VERSION !== $version) {
$twillServiceProviderVersion = TwillServiceProvider::VERSION;
$this->error(
"The TwillServiceProvider::VERSION version ($twillServiceProviderVersion) does not match that of the release ($version)"
);
exit(1);
}
}
/**
* @param string $path
* @return string
*/
private function getTwillDir($path = '')
{
return __DIR__ . '/../../' . $path;
}
}