This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
installer.php
213 lines (171 loc) · 5.55 KB
/
installer.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
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
<?php
/*
* This file is part of the Manalize project.
*
* (c) Manala <contact@manala.io>
*
* For the full copyright and license information, please refer to the LICENSE
* file that was distributed with this source code.
*/
namespace Manala\Manalize\Installer;
class Installer
{
const DEFAULT_DIR = '/usr/local/bin';
private $repository;
private $io;
public function __construct()
{
$this->repository = new ManalizeRepository();
$this->io = new IO();
}
public function install($dir = self::DEFAULT_DIR)
{
$io = $this->io;
try {
$io->newLine();
$io->title('Manalize Installer');
if ('\\' === DIRECTORY_SEPARATOR) {
throw new \RuntimeException('Sorry, but Windows is not supported right now');
}
if (!is_dir($dir) || !is_writeable($dir)) {
throw new \RuntimeException(sprintf(
'Target directory at path "%s" should exist and be writable',
$dir
));
}
$io->newLine();
$io->note('Retrieving the latest release...');
$tag = $this->repository->getLatestTag();
$io->note("Downloading the phar for $tag...");
$phar = $this->repository->downloadPhar($tag);
$io->note('Moving executable...');
$target = "$dir/manalize";
file_put_contents($target, $phar);
$io->note('Making Manalize executable...');
@chmod($target, 0755);
$io->success("Manalize successfully installed at path \"$target\"");
$io->newLine();
} catch (\Exception $ex) {
$io->error($ex->getMessage());
$io->newLine();
exit(1);
}
exit(0);
}
}
class ManalizeRepository
{
const GITHUB_RELEASE_PHAR = 'https://github.com/%s/releases/download/%s/manalize.phar';
const GITHUB_LATEST_RELEASE_URI = 'https://api.github.com/repos/%s/releases/latest';
const GITHUB_REPOSITORY_NAME = 'manala/manalize';
private $latestTag;
public function downloadPhar($tag)
{
if (false === $phar = $this->get(sprintf(self::GITHUB_RELEASE_PHAR, self::GITHUB_REPOSITORY_NAME, $tag))) {
throw new \RuntimeException('Unable to download the phar');
}
return $phar;
}
public function getLatestTag()
{
if (null !== $this->latestTag) {
return $this->latestTag;
}
if (false === $raw = $this->get(sprintf(self::GITHUB_LATEST_RELEASE_URI, self::GITHUB_REPOSITORY_NAME))) {
throw new \RuntimeException('Unable to retrieve the latest version');
}
$release = json_decode($raw, true);
return $this->latestTag = $release['tag_name'];
}
private function get($uri)
{
return @file_get_contents(
$uri,
null,
stream_context_create(['http' => [
'method' => 'GET',
'header' => 'User-Agent: Manalize (installer)\r\n',
]])
);
}
}
class IO
{
private static $availableForegroundColors = [
'red' => ['set' => 31, 'unset' => 39],
'cyan' => ['set' => 36, 'unset' => 39],
'green' => ['set' => 32, 'unset' => 39],
'yellow' => ['set' => 33, 'unset' => 39],
'white' => ['set' => 37, 'unset' => 39],
'default' => ['set' => 39, 'unset' => 39],
];
public function write($message)
{
echo $message;
}
public function writeln($message)
{
$this->write($message);
$this->newLine();
}
public function title($title)
{
$underline = str_repeat('=', strlen($title));
$this->writeColored($title, 'cyan', true);
$this->writeColored($underline, 'cyan', true);
}
public function newLine($count = 1)
{
$this->write(str_repeat(PHP_EOL, $count));
}
public function success($message)
{
$this->newLine();
$this->writeColored("✔ $message", 'green', true);
}
public function error($message)
{
$this->newLine();
$this->writeColored("✘ $message", 'red', true);
}
public function note($message)
{
$this->writeColored("- $message", 'yellow', true);
}
private function writeColored($message, $color, $newLine = false)
{
if ($this->hasColorSupport()) {
$color = self::$availableForegroundColors[$color];
$message = sprintf("\e[%sm%s\e[%sm", $color['set'], $message, $color['unset']);
}
$newLine ? $this->writeln($message) : $this->write($message);
}
/**
* @see \Symfony\Component\Console\Output\StreamOuput
*/
protected function hasColorSupport()
{
if ('\\' === DIRECTORY_SEPARATOR) {
return
'10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
|| 'xterm' === getenv('TERM');
}
return function_exists('posix_isatty') && @posix_isatty(STDOUT);
}
}
if (!defined('STDOUT')) {
define('STDOUT', fopen('php://stdout', 'w'));
}
set_error_handler(
function ($code, $message, $file, $line) {
if ($code & error_reporting()) {
$n = PHP_EOL;
echo "Error: $message - on line $line$n$n";
exit(1);
}
}
);
$dir = isset($argv[1]) ? $argv[1] : Installer::DEFAULT_DIR;
(new Installer())->install($dir);