-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.ps1
More file actions
360 lines (321 loc) · 13.5 KB
/
Copy pathinstall.ps1
File metadata and controls
360 lines (321 loc) · 13.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# LayaAir CLI installer for Windows (PowerShell)
# Usage: iwr https://<URL>/install.ps1 | iex
# $env:LAYAAIR_INSTALL_DIR="C:\tools\layaair"; iwr https://<URL>/install.ps1 | iex
#
# Install a specific version in one go:
# iwr https://<URL>/install.ps1 | iex; & "$env:USERPROFILE\.layaair\layaair.cmd" install 3.4.0
#
# Installs the `layaair` dispatcher only. After install, manage CLI versions:
# layaair install [version] # default: latest
# layaair list
# layaair uninstall <version>
# layaair --version # print active version and exit
# layaair --version=3.4.0 <args> # select a specific installed version
#Requires -Version 5.1
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$InstallDir = if ($env:LAYAAIR_INSTALL_DIR) { $env:LAYAAIR_INSTALL_DIR } else { "$env:USERPROFILE\.layaair" }
# ── node check ────────────────────────────────────────────────
$NodeVer = $null
try { $NodeVer = (& node --version 2>$null) } catch {}
if (-not $NodeVer) {
Write-Error "[layaair] Node.js v20+ required. Install from https://nodejs.org/"; exit 1
}
$Major = [int](($NodeVer -replace '^v','') -split '\.' | Select-Object -First 1)
if ($Major -lt 20) {
Write-Error "[layaair] Node.js v20+ required (found: $NodeVer)"; exit 1
}
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
# ── dispatcher.js content (literal here-string, no interpolation) ─
$DispatcherContent = @'
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const cp = require('child_process');
const https = require('https');
const http = require('http');
const DOWNLOAD_ROOT = 'https://ldc-1251285021.file.myqcloud.com/layaair3';
const LATEST_VERSION_URL = DOWNLOAD_ROOT + '/latest.txt';
const INSTALL_DIR = __dirname;
const VERSIONS_FILE = path.join(INSTALL_DIR, 'versions.json');
function getArg(argv, name) {
for (let i = 0; i < argv.length; i++) {
const eqM = argv[i].match(new RegExp('^--' + name + '=(.+)$'));
if (eqM) return eqM[1];
if (argv[i] === '--' + name && i + 1 < argv.length && !argv[i + 1].startsWith('--'))
return argv[i + 1];
}
return null;
}
function loadVersions() {
try { return JSON.parse(fs.readFileSync(VERSIONS_FILE, 'utf8')).versions || []; }
catch (_) { return []; }
}
function saveVersions(versions) {
fs.writeFileSync(VERSIONS_FILE, JSON.stringify({ versions }, null, 2) + '\n');
}
function semverParts(v) {
const main = v.split('-')[0];
const parts = main.split('.').map(Number);
return { ma: parts[0] || 0, mi: parts[1] || 0, pa: parts[2] || 0, pre: v.includes('-') ? v.slice(v.indexOf('-') + 1) : '' };
}
function semverCompare(a, b) {
const p = semverParts(a), q = semverParts(b);
if (p.ma !== q.ma) return p.ma - q.ma;
if (p.mi !== q.mi) return p.mi - q.mi;
if (p.pa !== q.pa) return p.pa - q.pa;
if (!p.pre && q.pre) return 1;
if (p.pre && !q.pre) return -1;
return p.pre < q.pre ? -1 : p.pre > q.pre ? 1 : 0;
}
function downloadBaseForVersion(version) {
const parts = String(version).split('.');
if (parts.length < 2 || !parts[0] || !parts[1]) {
console.error('[layaair] Invalid version: ' + version);
process.exit(1);
}
return DOWNLOAD_ROOT + '/layaair-' + parts[0] + '.' + parts[1] + '/cli';
}
function fuzzyMatch(hint, versions) {
const sorted = [...versions].sort((a, b) => semverCompare(b.version, a.version));
const exact = sorted.find(v => v.version === hint);
if (exact) return exact;
const hintParts = String(hint).split('.');
if (hintParts.length >= 2) {
const hintMinor = hintParts[0] + '.' + hintParts[1];
const sameMinor = sorted.find(function (v) { const p = semverParts(v.version); return p.ma + '.' + p.mi === hintMinor; });
if (sameMinor) return sameMinor;
}
const dotHint = hint + '.';
const prefixed = sorted.find(v => v.version.startsWith(dotHint));
if (prefixed) return prefixed;
return sorted.find(v => v.version.startsWith(hint));
}
function getVersionHint(argv) {
const ver = getArg(argv, 'version');
if (ver) return ver;
const proj = getArg(argv, 'project');
if (proj) {
try {
const dir = path.resolve(proj);
const entries = fs.readdirSync(dir);
const laya = entries.find(f => f.endsWith('.laya'));
if (laya) {
const info = JSON.parse(fs.readFileSync(path.join(dir, laya), 'utf8'));
if (info.version) return String(info.version);
}
} catch (_) {}
}
return null;
}
function platformInfo() {
const p = process.platform;
const osName = p === 'win32' ? 'win32' : p === 'darwin' ? 'darwin' : 'linux';
const a = process.arch;
if (a !== 'x64' && a !== 'arm64') {
console.error('[layaair] Unsupported arch: ' + a);
process.exit(1);
}
return { osName, arch: a };
}
function httpGet(url, onResponse, redirects) {
redirects = redirects || 0;
if (redirects > 5) { onResponse(new Error('Too many redirects'), null); return; }
const mod = url.indexOf('https:') === 0 ? https : http;
const req = mod.get(url, function (res) {
const code = res.statusCode;
if ((code === 301 || code === 302 || code === 303 || code === 307 || code === 308) && res.headers.location) {
res.resume();
httpGet(res.headers.location, onResponse, redirects + 1);
return;
}
if (code !== 200) {
onResponse(new Error('HTTP ' + code + ' for ' + url), null);
return;
}
onResponse(null, res);
});
req.on('error', function (e) { onResponse(e, null); });
}
function httpGetText(url) {
return new Promise(function (resolve, reject) {
httpGet(url, function (err, res) {
if (err) return reject(err);
let data = '';
res.setEncoding('utf8');
res.on('data', function (c) { data += c; });
res.on('end', function () { resolve(data); });
res.on('error', reject);
});
});
}
function httpDownload(url, dest) {
return new Promise(function (resolve, reject) {
httpGet(url, function (err, res) {
if (err) return reject(err);
const out = fs.createWriteStream(dest);
res.pipe(out);
out.on('finish', function () { out.close(function () { resolve(); }); });
out.on('error', reject);
});
});
}
function extractZip(zipFile, destDir) {
if (process.platform === 'win32') {
const cmd = 'Expand-Archive -LiteralPath ' + JSON.stringify(zipFile) +
' -DestinationPath ' + JSON.stringify(destDir) + ' -Force';
cp.execFileSync('powershell', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', cmd], { stdio: 'inherit' });
} else {
cp.execFileSync('unzip', ['-q', zipFile, '-d', destDir], { stdio: 'inherit' });
}
}
async function cmdInstall(versionArg) {
const info = platformInfo();
const osName = info.osName, arch = info.arch;
let version = versionArg;
if (!version || version === 'latest') {
process.stdout.write('[layaair] Fetching latest version... ');
try {
version = (await httpGetText(LATEST_VERSION_URL)).trim();
} catch (e) {
console.error('failed: ' + e.message);
process.exit(1);
}
if (!version) { console.error('failed.'); process.exit(1); }
console.log(version);
}
const downloadBase = downloadBaseForVersion(version);
const zipName = 'layaair-cli-' + version + '-' + osName + '-' + arch + '.zip';
const zipUrl = downloadBase + '/' + zipName;
const tmpZip = path.join(os.tmpdir(), 'layaair-cli-install-' + process.pid + '.zip');
const tmpDir = path.join(INSTALL_DIR, '.tmp-extract-' + process.pid);
console.log('Installing LayaAir CLI ' + version + ' for ' + osName + '-' + arch + '...');
try {
await httpDownload(zipUrl, tmpZip);
} catch (e) {
console.error('[layaair] Download failed: ' + e.message);
process.exit(1);
}
if (fs.existsSync(tmpDir)) fs.rmSync(tmpDir, { recursive: true, force: true });
fs.mkdirSync(tmpDir, { recursive: true });
extractZip(tmpZip, tmpDir);
try { fs.unlinkSync(tmpZip); } catch (_) {}
const versionDir = path.join(INSTALL_DIR, version);
if (fs.existsSync(versionDir)) fs.rmSync(versionDir, { recursive: true, force: true });
fs.renameSync(tmpDir, versionDir);
if (osName !== 'win32') {
try { fs.chmodSync(path.join(versionDir, 'layaair'), 0o755); } catch (_) {}
}
const versions = loadVersions().filter(function (v) { return v.version !== version; });
versions.push({ version: version, path: version });
saveVersions(versions);
console.log('');
console.log('✓ LayaAir CLI ' + version + ' installed to: ' + versionDir);
}
function cmdUninstall(version) {
if (!version) {
console.error('[layaair] Usage: layaair uninstall <version>');
process.exit(1);
}
const versions = loadVersions();
const entry = versions.find(function (v) { return v.version === version; });
if (!entry) {
console.error('[layaair] Not installed: ' + version);
process.exit(1);
}
const dir = path.join(INSTALL_DIR, entry.path);
if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
saveVersions(versions.filter(function (v) { return v.version !== version; }));
console.log('✓ Uninstalled ' + version);
}
function cmdList() {
const versions = loadVersions();
if (!versions.length) {
console.log('[layaair] No versions installed. Run: layaair install');
return;
}
const sorted = [...versions].sort(function (a, b) { return semverCompare(b.version, a.version); });
console.log('Installed versions:');
sorted.forEach(function (v, i) {
console.log(' ' + (i === 0 ? '* ' : ' ') + v.version);
});
console.log(' (* = newest)');
}
(async function () {
const argv = process.argv.slice(2);
const sub = argv[0];
if (sub === 'install') { await cmdInstall(argv[1]); return; }
if (sub === 'uninstall') { cmdUninstall(argv[1]); return; }
if (sub === 'list') { cmdList(); return; }
if (argv.includes('--version') && !argv.find(function (a) { return a.startsWith('--version='); })) {
const vers = loadVersions().sort(function (a, b) { return semverCompare(b.version, a.version); });
console.log(vers.length ? vers[0].version : '(no CLI versions installed — run: layaair install)');
return;
}
const versions = loadVersions();
if (!versions.length) {
console.error('[layaair] No versions installed. Run: layaair install');
process.exit(1);
}
const hint = getVersionHint(argv);
const sorted = [...versions].sort(function (a, b) { return semverCompare(b.version, a.version); });
let chosen = hint ? fuzzyMatch(hint, versions) : sorted[0];
if (!chosen) {
chosen = sorted[0];
console.warn('[layaair] Version ' + hint + ' not installed, opening with ' + chosen.version + '.');
}
const cliMain = path.join(INSTALL_DIR, chosen.path, 'Resources', 'cli-main.js');
if (!fs.existsSync(cliMain)) {
console.error('[layaair] Broken install for ' + chosen.version + ': ' + cliMain + ' not found');
process.exit(1);
}
const result = cp.spawnSync(process.execPath, [cliMain, ...argv], { stdio: 'inherit' });
process.exit(result.status != null ? result.status : 1);
})().catch(function (e) {
console.error('[layaair] ' + (e && e.message || e));
process.exit(1);
});
'@
[System.IO.File]::WriteAllText((Join-Path $InstallDir "dispatcher.js"), $DispatcherContent, [System.Text.Encoding]::UTF8)
# ── write layaair.cmd shim ────────────────────────────────────
$ShimCmdContent = @'
@echo off
setlocal EnableDelayedExpansion
set "DIR=%~dp0"
for /f "tokens=1 delims=." %%v in ('node --version 2^>nul') do (
set "MAJOR=%%v"
set "MAJOR=!MAJOR:v=!"
)
if "!MAJOR!"=="" (
echo [layaair] ERROR: Node.js not found. Install from https://nodejs.org/
exit /b 1
)
if !MAJOR! LSS 20 (
echo [layaair] ERROR: Node.js v20+ required ^(found v!MAJOR!^)
exit /b 1
)
node "%DIR%dispatcher.js" %*
'@
[System.IO.File]::WriteAllText((Join-Path $InstallDir "layaair.cmd"), $ShimCmdContent, [System.Text.Encoding]::ASCII)
# ── ensure versions.json exists ───────────────────────────────
$VersionsFile = Join-Path $InstallDir "versions.json"
if (-not (Test-Path $VersionsFile)) {
[System.IO.File]::WriteAllText($VersionsFile, '{"versions":[]}' + "`n", [System.Text.Encoding]::UTF8)
}
# ── PATH setup (User scope + current session) ─────────────────
$UserPath = [System.Environment]::GetEnvironmentVariable('Path', 'User')
if (-not $UserPath) { $UserPath = '' }
if ($UserPath -notlike "*$InstallDir*") {
[System.Environment]::SetEnvironmentVariable('Path', "$UserPath;$InstallDir", 'User')
Write-Host " Added $InstallDir to User PATH"
}
if ($env:Path -notlike "*$InstallDir*") {
$env:Path = "$env:Path;$InstallDir"
}
Write-Host ""
Write-Host "✓ LayaAir CLI dispatcher installed to: $InstallDir"
Write-Host ""
Write-Host " Next: install a CLI version"
Write-Host " layaair install # latest"
Write-Host " layaair install 3.4.0 # specific"