-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNodeInstaller.php
More file actions
238 lines (205 loc) · 6.5 KB
/
Copy pathNodeInstaller.php
File metadata and controls
238 lines (205 loc) · 6.5 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
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace MariusBuescher\NodeComposer\Installer;
use Composer\Installer\BinaryInstaller;
use Composer\IO\IOInterface;
use Composer\Package\Package;
use Composer\Util\RemoteFilesystem;
use InvalidArgumentException;
use MariusBuescher\NodeComposer\ArchitectureMap;
use MariusBuescher\NodeComposer\BinLinker;
use MariusBuescher\NodeComposer\InstallerInterface;
use MariusBuescher\NodeComposer\NodeContext;
use Symfony\Component\Process\Process;
class NodeInstaller implements InstallerInterface
{
/**
* @var IOInterface
*/
private $io;
/**
* @var RemoteFilesystem
*/
private $remoteFs;
/**
* @var NodeContext
*/
private $context;
/**
* @var string
*/
private $downloadUriTemplate;
/**
* NodeDownloader constructor.
* @param IOInterface $io
* @param RemoteFilesystem $remoteFs
* @param NodeContext $context
* @param string $downloadUriTemplate
*/
public function __construct(
IOInterface $io,
RemoteFilesystem $remoteFs,
NodeContext $context,
$downloadUriTemplate = null
) {
$this->io = $io;
$this->remoteFs = $remoteFs;
$this->context = $context;
$this->downloadUriTemplate = is_string($downloadUriTemplate) ? $downloadUriTemplate :
'https://nodejs.org/dist/v${version}/node-v${version}-${osType}-${architecture}.${format}';
}
/**
* @param string $version
* @throws InvalidArgumentException
* @return bool
*/
public function install($version)
{
if (!is_string($version)) {
throw new InvalidArgumentException(
sprintf('Version must be a string, %s given', gettype($version))
);
}
$this->downloadExecutable($version);
return true;
}
/**
* @return string|false
*/
public function isInstalled()
{
$process = new Process(["node --version"], $this->context->getBinDir());
$process->setIdleTimeout(null);
$process->setTimeout(null);
$process->run();
if ($process->isSuccessful()) {
$output = explode("\n", $process->getIncrementalOutput());
return $output[0];
} else {
return false;
}
}
/**
* @param string $version
*/
private function downloadExecutable($version)
{
$downloadUri = $this->buildDownloadLink($version);
$fileName = $this->context->getVendorDir() . DIRECTORY_SEPARATOR .
pathinfo(parse_url($downloadUri, PHP_URL_PATH), PATHINFO_BASENAME);
$this->remoteFs->copy(
parse_url($downloadUri, PHP_URL_HOST),
$downloadUri,
$fileName,
true
);
$targetPath = $this->context->getVendorDir() . DIRECTORY_SEPARATOR .
pathinfo(parse_url($downloadUri, PHP_URL_PATH), PATHINFO_BASENAME);
$targetPath = preg_replace('/\.(tar\.gz|zip)$/', '', $targetPath);
$this->unpackExecutable($fileName, $targetPath);
$realNodeInstalledPath = is_dir($targetPath . DIRECTORY_SEPARATOR . basename($targetPath)) ?
$targetPath . DIRECTORY_SEPARATOR . basename($targetPath) :
$targetPath;
$this->linkExecutables($realNodeInstalledPath, $this->context->getBinDir());
}
/**
* @param string $version
* @return string
*/
private function buildDownloadLink($version)
{
return preg_replace(
array(
'/\$\{version\}/',
'/\$\{osType\}/',
'/\$\{architecture\}/',
'/\$\{format\}/'
),
array(
$version,
strtolower($this->context->getOsType()),
ArchitectureMap::getNodeArchitecture($this->context->getSystemArchitecture()),
$this->context->getOsType() === 'win' ? 'zip' : 'tar.gz'
),
$this->downloadUriTemplate
);
}
/**
* @param string $source
* @param string $targetDir
*/
private function unpackExecutable($source, $targetDir)
{
if (realpath($targetDir)) {
$files = glob($targetDir . DIRECTORY_SEPARATOR . '**' . DIRECTORY_SEPARATOR . '*');
foreach ($files as $file) {
unlink($file);
}
} else {
mkdir($targetDir);
}
if (preg_match('/\.zip$/', $source) === 1) {
$this->unzip($source, $targetDir);
} else {
$this->untar($source, $targetDir);
}
}
/**
* @param string $source
* @param string $targetDir
*/
private function unzip($source, $targetDir)
{
$zip = new \ZipArchive();
$res = $zip->open($source);
if ($res === true) {
// extract it to the path we determined above
$zip->extractTo($targetDir);
$zip->close();
} else {
throw new \RuntimeException(sprintf('Unable to extract file %s', $source));
}
unlink($source);
}
/**
* @param string $source
* @param string $targetDir
*/
private function untar($source, $targetDir)
{
$process = new Process(
"tar -xvf ".$source." -C ".escapeshellarg($targetDir)." --strip 1"
);
$process->run();
if (!$process->isSuccessful()) {
throw new \RuntimeException(sprintf(
'An error occurred while untaring NodeJS (%s) to %s',
$source,
$targetDir
));
}
unlink($source);
}
/**
* @param string $sourceDir
* @param string $targetDir
*/
private function linkExecutables($sourceDir, $targetDir)
{
$nodePath = $this->context->getOsType() === 'win' ?
realpath($sourceDir . DIRECTORY_SEPARATOR . 'node.exe') :
realpath($sourceDir . DIRECTORY_SEPARATOR . 'bin/node');
$nodeLink = $targetDir . DIRECTORY_SEPARATOR . 'node';
$fs = new BinLinker(
$this->context->getBinDir(),
$this->context->getOsType()
);
$fs->unlinkBin($nodeLink);
$fs->linkBin($nodePath, $nodeLink);
$npmPath = $this->context->getOsType() === 'win' ?
realpath($sourceDir . DIRECTORY_SEPARATOR . 'npm.cmd') :
realpath($sourceDir . DIRECTORY_SEPARATOR . 'bin/npm');
$npmLink = $targetDir . DIRECTORY_SEPARATOR . 'npm';
$fs->unlinkBin($npmLink);
$fs->linkBin($npmPath, $npmLink);
}
}