Repository navigation
Expand file tree
/
Copy pathSelfUpdate.php
More file actions
225 lines (194 loc) · 6.02 KB
/
Copy pathSelfUpdate.php
File metadata and controls
225 lines (194 loc) · 6.02 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace Codeception\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Auto-updates phar archive from official site: 'http://codeception.com/codecept.phar' .
*
* * `php codecept.phar self-update`
*
* @author Franck Cassedanne <franck@cassedanne.com>
*/
class SelfUpdate extends Command
{
/**
* Class constants
*/
const NAME = 'Codeception';
const GITHUB = 'Codeception/Codeception';
const SOURCE = 'http://codeception.com/codecept.phar';
/**
* Holds the current script filename.
* @var string
*/
protected $filename;
/**
* Holds the live version string.
* @var string
*/
protected $liveVersion;
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->filename = $_SERVER['argv'][0];
$this
// ->setAliases(array('selfupdate'))
->setDescription(sprintf(
'Upgrade <comment>%s</comment> to the latest version',
$this->filename
));
parent::configure();
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$version = \Codeception\Codecept::VERSION;
$output->writeln(
sprintf(
'<info>%s</info> version <comment>%s</comment>',
self::NAME, $version
)
);
$output->writeln("\n<info>Checking for a new version...</info>\n");
try {
if ($this->isOutOfDate($version)) {
$output->writeln(
sprintf(
'A newer version is available: <comment>%s</comment>',
$this->liveVersion
)
);
if (!$input->getOption('no-interaction')) {
$dialog = $this->getHelperSet()->get('question');
$question = new ConfirmationQuestion("\n<question>Do you want to update?</question> ", false);
if (!$dialog->ask($input, $output, $question)) {
$output->writeln("\n<info>Bye-bye!</info>\n");
return;
}
}
$output->writeln("\n<info>Updating...</info>");
$this->retrieveLatestPharFile($output);
} else {
$output->writeln('You are already using the latest version.');
}
} catch (\Exception $e) {
$output->writeln(
sprintf(
"<error>\n%s\n</error>",
$e->getMessage()
)
);
}
}
/**
* Checks whether the provided version is current.
*
* @param string $version The version number to check.
* @return boolean Returns True if a new version is available.
*/
private function isOutOfDate($version)
{
$tags = $this->getGithubTags(self::GITHUB);
$this->liveVersion = array_reduce(
$tags,
function ($a, $b) {
return version_compare($a, $b, '>') ? $a : $b;
}
);
return -1 != version_compare($version, $this->liveVersion, '>=');
}
/**
* Returns an array of tags from a github repo.
*
* @param string $repo The repository name to check upon.
* @return array
*/
private function getGithubTags($repo)
{
$jsonTags = $this->retrieveContentFromUrl(
'https://api.github.com/repos/' . $repo . '/tags'
);
return array_map(
function ($tag) {
return $tag['name'];
},
json_decode($jsonTags, true)
);
}
/**
* Retrieves the body-content from the provided URL.
*
* @param string $url
* @return string
* @throw Exception if status code is above 300
*/
private function retrieveContentFromUrl($url)
{
$opts = [
'http' => [
'follow_location' => 1,
'max_redirects' => 20,
'timeout' => 60,
'user_agent' => self::NAME
]
];
$ctx = stream_context_create($opts);
$body = file_get_contents($url, 0, $ctx);
if (isset($http_response_header)) {
$code = substr($http_response_header[0], 9, 3);
if (floor($code / 100) > 3) {
throw new \Exception($http_response_header[0]);
}
} else {
throw new \Exception('Request failed.');
}
return $body;
}
/**
* Retrieves the latest phar file.
*
* @param OutputInterface $output
*/
protected function retrieveLatestPharFile(OutputInterface $output)
{
$temp = basename($this->filename, '.phar') . '-temp.phar';
try {
if (@copy(self::SOURCE, $temp)) {
chmod($temp, 0777 & ~umask());
// test the phar validity
$phar = new \Phar($temp);
// free the variable to unlock the file
unset($phar);
rename($temp, $this->filename);
} else {
throw new \Exception('Request failed.');
}
} catch (\Exception $e) {
if (
!$e instanceof \UnexpectedValueException
&& !$e instanceof \PharException
) {
throw $e;
}
unlink($temp);
$output->writeln(
sprintf(
"<error>\nSomething went wrong (%s).\nPlease re-run this again.</error>\n",
$e->getMessage()
)
);
}
$output->writeln(
sprintf(
"\n<comment>%s</comment> has been updated.\n",
$this->filename
)
);
}
}