generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmain.ts
473 lines (414 loc) · 15.1 KB
/
main.ts
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import * as crypto from "crypto";
import * as os from "os";
import * as path from "path";
import * as process from "process";
import * as cache from "@actions/cache";
import * as core from "@actions/core";
import { exec, getExecOutput } from "@actions/exec";
import * as glob from "glob";
import { compare, CompareOperator } from "compare-versions";
const nativePath = process.platform === "win32" ? path.win32.normalize : path.normalize;
const compareVersions = (v1: string, op: CompareOperator, v2: string): boolean => {
return compare(v1, v2, op);
};
const setOrAppendEnvVar = (name: string, value: string): void => {
const oldValue = process.env[name];
let newValue = value;
if (oldValue) {
newValue = `${oldValue}:${newValue}`;
}
core.exportVariable(name, newValue);
};
const toolsPaths = (installDir: string): string[] =>
["Tools/**/bin", "*.app/Contents/MacOS", "*.app/**/bin"].flatMap((p: string): string[] =>
glob.sync(`${installDir}/${p}`)
);
const pythonCommand = (command: string, args: readonly string[]): string => {
const python = process.platform === "win32" ? "python" : "python3";
return `${python} -m ${command} ${args.join(" ")}`;
};
const execPython = async (command: string, args: readonly string[]): Promise<number> => {
return exec(pythonCommand(command, args));
};
const getPythonOutput = async (command: string, args: readonly string[]): Promise<string> => {
// Aqtinstall prints to both stderr and stdout, depending on the command.
// This function assumes we don't care which is which, and we want to see it all.
const out = await getExecOutput(pythonCommand(command, args));
return out.stdout + out.stderr;
};
const flaggedList = (flag: string, listArgs: readonly string[]): string[] => {
return listArgs.length ? [flag, ...listArgs] : [];
};
const locateQtArchDir = (installDir: string): string => {
// For 6.4.2/gcc, qmake is at 'installDir/6.4.2/gcc_64/bin/qmake'.
// This makes a list of all the viable arch directories that contain a qmake file.
const qtArchDirs = glob
.sync(`${installDir}/[0-9]*/*/bin/qmake*`)
.map((s) => s.replace(/\/bin\/qmake[^/]*$/, ""));
// For Qt6 mobile and wasm installations, and Qt6 Windows on ARM installations,
// a standard desktop Qt installation must exist alongside the requested architecture.
// In these cases, we must select the first item that ends with 'android*', 'ios', 'wasm*' or 'msvc*_arm64'.
const requiresParallelDesktop = qtArchDirs.filter((p) =>
p.match(/6\.\d+\.\d+\/(android[^/]*|ios|wasm[^/]*|msvc[^/]*_arm64)$/)
);
if (requiresParallelDesktop.length) {
// NOTE: if multiple mobile/wasm installations coexist, this may not select the desired directory
return requiresParallelDesktop[0];
} else if (!qtArchDirs.length) {
throw Error(`Failed to locate a Qt installation directory in ${installDir}`);
} else {
// NOTE: if multiple Qt installations exist, this may not select the desired directory
return qtArchDirs[0];
}
};
const isAutodesktopSupported = async (): Promise<boolean> => {
const rawOutput = await getPythonOutput("aqt", ["version"]);
const match = rawOutput.match(/aqtinstall\(aqt\)\s+v(\d+\.\d+\.\d+)/);
return match ? compareVersions(match[1], ">=", "3.0.0") : false;
};
class Inputs {
readonly host: "windows" | "mac" | "linux";
readonly target: "desktop" | "android" | "ios";
readonly version: string;
readonly arch: string;
readonly dir: string;
readonly modules: string[];
readonly archives: string[];
readonly tools: string[];
readonly addToolsToPath: boolean;
readonly extra: string[];
readonly src: boolean;
readonly srcArchives: string[];
readonly doc: boolean;
readonly docArchives: string[];
readonly docModules: string[];
readonly example: boolean;
readonly exampleArchives: string[];
readonly exampleModules: string[];
readonly installDeps: boolean | "nosudo";
readonly cache: boolean;
readonly cacheKeyPrefix: string;
readonly isInstallQtBinaries: boolean;
readonly setEnv: boolean;
readonly aqtSource: string;
readonly aqtVersion: string;
readonly py7zrVersion: string;
constructor() {
const host = core.getInput("host");
// Set host automatically if omitted
if (!host) {
switch (process.platform) {
case "win32": {
this.host = "windows";
break;
}
case "darwin": {
this.host = "mac";
break;
}
default: {
this.host = "linux";
break;
}
}
} else {
// Make sure host is one of the allowed values
if (host === "windows" || host === "mac" || host === "linux") {
this.host = host;
} else {
throw TypeError(`host: "${host}" is not one of "windows" | "mac" | "linux"`);
}
}
const target = core.getInput("target");
// Make sure target is one of the allowed values
if (target === "desktop" || target === "android" || target === "ios") {
this.target = target;
} else {
throw TypeError(`target: "${target}" is not one of "desktop" | "android" | "ios"`);
}
// An attempt to sanitize non-straightforward version number input
this.version = core.getInput("version");
this.arch = core.getInput("arch");
// Set arch automatically if omitted
if (!this.arch) {
if (this.target === "android") {
if (
compareVersions(this.version, ">=", "5.14.0") &&
compareVersions(this.version, "<", "6.0.0")
) {
this.arch = "android";
} else {
this.arch = "android_armv7";
}
} else if (this.host === "windows") {
if (compareVersions(this.version, ">=", "5.15.0")) {
this.arch = "win64_msvc2019_64";
} else if (compareVersions(this.version, "<", "5.6.0")) {
this.arch = "win64_msvc2013_64";
} else if (compareVersions(this.version, "<", "5.9.0")) {
this.arch = "win64_msvc2015_64";
} else {
this.arch = "win64_msvc2017_64";
}
}
}
const dir = core.getInput("dir") || process.env.RUNNER_WORKSPACE;
if (!dir) {
throw TypeError(`"dir" input may not be empty`);
}
this.dir = `${dir}/Qt`;
this.modules = Inputs.getStringArrayInput("modules");
this.archives = Inputs.getStringArrayInput("archives");
this.tools = Inputs.getStringArrayInput("tools").map(
// The tools inputs have the tool name, variant, and arch delimited by a comma
// aqt expects spaces instead
(tool: string): string => tool.replace(/,/g, " ")
);
this.addToolsToPath = Inputs.getBoolInput("add-tools-to-path");
this.extra = Inputs.getStringArrayInput("extra");
const installDeps = core.getInput("install-deps").toLowerCase();
if (installDeps === "nosudo") {
this.installDeps = "nosudo";
} else {
this.installDeps = installDeps === "true";
}
this.cache = Inputs.getBoolInput("cache");
this.cacheKeyPrefix = core.getInput("cache-key-prefix");
this.isInstallQtBinaries =
!Inputs.getBoolInput("tools-only") && !Inputs.getBoolInput("no-qt-binaries");
this.setEnv = Inputs.getBoolInput("set-env");
this.aqtSource = core.getInput("aqtsource");
this.aqtVersion = core.getInput("aqtversion");
this.py7zrVersion = core.getInput("py7zrversion");
this.src = Inputs.getBoolInput("source");
this.srcArchives = Inputs.getStringArrayInput("src-archives");
this.doc = Inputs.getBoolInput("documentation");
this.docModules = Inputs.getStringArrayInput("doc-modules");
this.docArchives = Inputs.getStringArrayInput("doc-archives");
this.example = Inputs.getBoolInput("examples");
this.exampleModules = Inputs.getStringArrayInput("example-modules");
this.exampleArchives = Inputs.getStringArrayInput("example-archives");
}
public get cacheKey(): string {
let cacheKey = this.cacheKeyPrefix;
for (const keyStringArray of [
[
this.host,
os.release(),
this.target,
this.arch,
this.version,
this.dir,
this.py7zrVersion,
this.aqtSource,
this.aqtVersion,
],
this.modules,
this.archives,
this.extra,
this.tools,
this.src ? "src" : "",
this.srcArchives,
this.doc ? "doc" : "",
this.docArchives,
this.docModules,
this.example ? "example" : "",
this.exampleArchives,
this.exampleModules,
]) {
for (const keyString of keyStringArray) {
if (keyString) {
cacheKey += `-${keyString}`;
}
}
}
// Cache keys cannot contain commas
cacheKey = cacheKey.replace(/,/g, "-");
// Cache keys cannot be larger than 512 characters
const maxKeyLength = 512;
if (cacheKey.length > maxKeyLength) {
const hashedCacheKey = crypto.createHash("sha256").update(cacheKey).digest("hex");
cacheKey = `${this.cacheKeyPrefix}-${hashedCacheKey}`;
}
return cacheKey;
}
private static getBoolInput(name: string): boolean {
return core.getInput(name).toLowerCase() === "true";
}
private static getStringArrayInput(name: string): string[] {
const content = core.getInput(name);
return content ? content.split(" ") : [];
}
}
const run = async (): Promise<void> => {
const inputs = new Inputs();
// Qt installer assumes basic requirements that are not installed by
// default on Ubuntu.
if (process.platform === "linux") {
if (inputs.installDeps) {
const dependencies = [
"build-essential",
"libgl1-mesa-dev",
"libgstreamer-gl1.0-0",
"libpulse-dev",
"libxcb-glx0",
"libxcb-icccm4",
"libxcb-image0",
"libxcb-keysyms1",
"libxcb-randr0",
"libxcb-render-util0",
"libxcb-render0",
"libxcb-shape0",
"libxcb-shm0",
"libxcb-sync1",
"libxcb-util1",
"libxcb-xfixes0",
"libxcb-xinerama0",
"libxcb1",
"libxkbcommon-dev",
"libxkbcommon-x11-0",
"libxcb-xkb-dev",
];
// Qt 6.5.0 adds this requirement:
// https://code.qt.io/cgit/qt/qtreleasenotes.git/about/qt/6.5.0/release-note.md
if (compareVersions(inputs.version, ">=", "6.5.0")) {
dependencies.push("libxcb-cursor0");
}
const updateCommand = "apt-get update";
const installCommand = `apt-get install ${dependencies.join(" ")} -y`;
if (inputs.installDeps === "nosudo") {
await exec(updateCommand);
await exec(installCommand);
} else {
await exec(`sudo ${updateCommand}`);
await exec(`sudo ${installCommand}`);
}
}
}
// Restore internal cache
let internalCacheHit = false;
if (inputs.cache) {
const cacheHitKey = await cache.restoreCache([inputs.dir], inputs.cacheKey);
if (cacheHitKey) {
core.info(`Automatic cache hit with key "${cacheHitKey}"`);
internalCacheHit = true;
} else {
core.info("Automatic cache miss, will cache this run");
}
}
// Install Qt and tools if not cached
if (!internalCacheHit) {
// 7-zip is required, and not included on macOS
if (process.platform === "darwin") {
await exec("brew install p7zip");
}
// Install dependencies via pip
await execPython("pip install", ["setuptools", "wheel", `"py7zr${inputs.py7zrVersion}"`]);
// Install aqtinstall separately: allows aqtinstall to override py7zr if required
if (inputs.aqtSource.length > 0) {
await execPython("pip install", [`"${inputs.aqtSource}"`]);
} else {
await execPython("pip install", [`"aqtinstall${inputs.aqtVersion}"`]);
}
// This flag will install a parallel desktop version of Qt, only where required.
// aqtinstall will automatically determine if this is necessary.
const autodesktop = (await isAutodesktopSupported()) ? ["--autodesktop"] : [];
// Install Qt
if (inputs.isInstallQtBinaries) {
const qtArgs = [
inputs.host,
inputs.target,
inputs.version,
...(inputs.arch ? [inputs.arch] : []),
...autodesktop,
...["--outputdir", inputs.dir],
...flaggedList("--modules", inputs.modules),
...flaggedList("--archives", inputs.archives),
...inputs.extra,
];
await execPython("aqt install-qt", qtArgs);
}
const installSrcDocExamples = async (
flavor: "src" | "doc" | "example",
archives: readonly string[],
modules: readonly string[]
): Promise<void> => {
const qtArgs = [
inputs.host,
// Aqtinstall < 2.0.4 requires `inputs.target` here, but that's deprecated
inputs.version,
...["--outputdir", inputs.dir],
...flaggedList("--archives", archives),
...flaggedList("--modules", modules),
...inputs.extra,
];
await execPython(`aqt install-${flavor}`, qtArgs);
};
// Install source, docs, & examples
if (inputs.src) {
await installSrcDocExamples("src", inputs.srcArchives, []);
}
if (inputs.doc) {
await installSrcDocExamples("doc", inputs.docArchives, inputs.docModules);
}
if (inputs.example) {
await installSrcDocExamples("example", inputs.exampleArchives, inputs.exampleModules);
}
// Install tools
for (const tool of inputs.tools) {
const toolArgs = [inputs.host, inputs.target, tool];
toolArgs.push("--outputdir", inputs.dir);
toolArgs.push(...inputs.extra);
await execPython("aqt install-tool", toolArgs);
}
}
// Save automatic cache
if (!internalCacheHit && inputs.cache) {
const cacheId = await cache.saveCache([inputs.dir], inputs.cacheKey);
core.info(`Automatic cache saved with id ${cacheId}`);
}
// Add tools to path
if (inputs.addToolsToPath && inputs.tools.length) {
toolsPaths(inputs.dir).map(nativePath).forEach(core.addPath);
}
// Set environment variables/outputs for tools
if (inputs.tools.length && inputs.setEnv) {
core.exportVariable("IQTA_TOOLS", nativePath(`${inputs.dir}/Tools`));
}
// Set environment variables/outputs for binaries
if (inputs.isInstallQtBinaries) {
const qtPath = nativePath(locateQtArchDir(inputs.dir));
// Set outputs
core.setOutput("qtPath", qtPath);
// Set env variables
if (inputs.setEnv) {
if (process.platform === "linux") {
setOrAppendEnvVar("LD_LIBRARY_PATH", nativePath(`${qtPath}/lib`));
}
if (process.platform !== "win32") {
setOrAppendEnvVar("PKG_CONFIG_PATH", nativePath(`${qtPath}/lib/pkgconfig`));
}
// If less than qt6, set Qt5_DIR variable
if (compareVersions(inputs.version, "<", "6.0.0")) {
core.exportVariable("Qt5_DIR", nativePath(`${qtPath}/lib/cmake`));
}
core.exportVariable("QT_ROOT_DIR", qtPath);
core.exportVariable("QT_PLUGIN_PATH", nativePath(`${qtPath}/plugins`));
core.exportVariable("QML2_IMPORT_PATH", nativePath(`${qtPath}/qml`));
core.addPath(nativePath(`${qtPath}/bin`));
}
}
};
void run()
.catch((err) => {
if (err instanceof Error) {
core.setFailed(err);
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
core.setFailed(`unknown error: ${err}`);
}
process.exit(1);
})
.then(() => {
process.exit(0);
});