-
Notifications
You must be signed in to change notification settings - Fork 115
/
release.php
executable file
·150 lines (121 loc) · 3.26 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
#!/usr/bin/env php
<?php
/**
* @author Chris Corbyn <chris@w3style.co.uk>
*
* Copyright © 2013-2014 Chris Corbyn.
*/
/* Generate releases in Github */
namespace Boris;
require __DIR__ . '/lib/autoload.php';
$args = getopt('hv:', array(
'help',
'version:'
));
if (count($args) != 1) {
help();
exit(1);
}
foreach ($args as $opt => $value) {
switch ($opt) {
case 'v':
case 'version':
version($value);
exit(0);
case 'h':
case 'help':
help();
exit(0);
default:
unknown($opt);
exit(1);
}
}
function help()
{
echo <<<HELP
Boris release generator script.
Usage:
./release.php --version 1.2 Create a release for v1.2
./release.php --help Display this help message
HELP;
}
function version($newVersion)
{
$token = get_token();
$user = get_user();
$repo = get_repo();
$oldVersion = Boris::VERSION;
$phar = "boris.phar";
printf("Building version v%s...\n", $newVersion);
printf(" Updating Boris::VERSION (%s) to %s...\n", $oldVersion, $newVersion);
`perl -pi -e 's/$oldVersion/$newVersion/' lib/Boris/Boris.php README.md`;
printf(" Committing changes...\n");
`git commit -am "Version bump to $newVersion"`;
printf(" Pushing changes upstream...\n");
`git push`;
printf(" Creating tag v%s...\n", $newVersion);
`git tag -a "v$newVersion" -m "Auto-generated tag"`;
printf(" Pushing tags upstream...\n");
`git push --tags`;
printf(" Creating release on github...\n");
$response = `curl \
-sL \
-XPOST \
-H "Authorization: token $token" \
--data-binary '{"tag_name":"v$newVersion"}' \
https://api.github.com/repos/$user/$repo/releases`;
$json = json_decode($response, true);
$id = $json['id'];
if (empty($id)) {
printf("Failed.\n");
printf("%s\n", $response);
exit(1);
}
printf(" Building phar...\n");
`box build`;
printf("Uploading phar to GitHub...\n");
`curl -XPOST \
-sL \
-H "Authorization: token $token" \
-H "Content-Type: application/octet-stream" \
--data-binary @$phar \
https://uploads.github.com/repos/$user/$repo/releases/$id/assets?name=$phar`;
printf("Done.\n");
}
function get_token()
{
if (getenv('GITHUB_TOKEN')) {
return getenv('GITHUB_TOKEN');
} else {
printf("Missing environment variable \$GITHUB_TOKEN\n");
exit(1);
}
}
function get_origin()
{
$remotes = `git remote -v`;
if (!preg_match('/^origin\s+(\S*?.git)\s+\(push\)/m', $remotes, $matches)) {
printf("Unable to find origin in $remotes\n");
exit(1);
}
return $matches[1];
}
function get_user()
{
$origin = get_origin();
if (!preg_match('#^.*?[/:]([^/]+)/([^/]+)\.git$#', $origin, $matches)) {
printf("Don't know how to parse $origin\n");
exit(1);
}
return $matches[1];
}
function get_repo()
{
$origin = get_origin();
if (!preg_match('#^.*?[/:]([^/]+)/([^/]+)\.git$#', $origin, $matches)) {
printf("Don't know how to parse $origin\n");
exit(1);
}
return $matches[2];
}